码迷,mamicode.com
首页 > 数据库 > 详细

jdbc

时间:2016-05-10 16:43:48      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

调用程序:

package com.fabao.test;

import java.sql.*;

public class MySQLConnection {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String url = null;
String user = null;
String password = null;
String sql = null;
try {
Class.forName("com.mysql.jdbc.Driver"); //加载mysq驱动
} catch (ClassNotFoundException e) {
System.out.println("驱动加载错误");
e.printStackTrace();//打印出错详细信息
}
try {
url =
"jdbc:mysql://192.168.8.88:3309/test?user=stdlaw&password=stdlaw&useUnicode=true&&characterEncoding=gb2312&autoReconnect = true";//简单写法:url = "jdbc:myqsl://localhost/test(数据库名)? user=root(用户)&password=yqs2602555(密码)";
user = "stdlaw";
password = "stdlaw";
conn = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
System.out.println("数据库链接错误");
e.printStackTrace();
}
try {
stmt = conn.createStatement();
sql = "select * from ali_suit_people";
rs = stmt.executeQuery(sql);//执行sql语句
while(rs.next()) {
System.out.print(rs.getInt("id") + " ");
System.out.print(rs.getString("name") + " ");
System.out.println(rs.getString("lawyer") + " ");
}
} catch (SQLException e) {
System.out.println("数据操作错误");
e.printStackTrace();
}
//关闭数据库
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch(Exception e) {
System.out.println("数据库关闭错误");
e.printStackTrace();
}
}
}

jdbc

标签:

原文地址:http://www.cnblogs.com/tonjocelyn/p/5478272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!