/*
* JDBC操作步骤
*
*/
public class Test01 {
public static void main(String[] args) {
try {
//1.加载并注册驱动
Class.forName("com.mysql.jdbc.Driver");
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
System.out.println("注册驱动成功!");
//2.获取数据库连接Connection
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="123456";
Connection conn=DriverManager.getConnection(url, user, password);
System.out.println("连接数据库成功!");
//3.创建Statement,并执行sql语句
Statement stmt=conn.createStatement();
String sql="insert into user values (null,‘蒋输‘,‘111111‘,21,‘2012-1-11‘)";
int num=stmt.executeUpdate(sql);
//4.处理执行结果
if(num>0){
System.out.println("插入成功!");
}else{
System.out.println("插入失败!");
}
//5.关闭资源
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("驱动没找到!");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("数据库连接失败!");
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
/*
* JDBC实现查询操作
*/
public class Test02 {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
// 查询用户信息
public void queryUsers() {
try {
//1.加载并注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "123456");
//3.创建Statement对象并执行SQL语句
stmt = conn.createStatement();
String sql = "select * from user";
//调用executeQuery()方法执行查询操作,返回一个ResultSet结果集,包含查询的结果
rs = stmt.executeQuery(sql);
//4.操作ResultSet结果集
while (rs.next()) { //将指针向前移动一行,判断是否有记录
int id = rs.getInt("id"); //根据列名获取值
String name = rs.getString("name");
String password = rs.getString(3);//根据列的索引获取值
java.sql.Date birthday=rs.getDate(5);
System.out.println("编号:" + id + ",姓名:" + name + ",密码:"
+ password+" 生日:"+birthday);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//5.关闭资源
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//查询用户数量
public void queryUserCount() {
try {
//1.加载并注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "123456");
//3.创建Statement对象并执行SQL语句
stmt = conn.createStatement();
String sql = "select count(id) from user";
//调用executeQuery()方法执行查询操作,返回一个ResultSet结果集,包含查询的结果
rs = stmt.executeQuery(sql);
//4.操作ResultSet结果集
int count=0;
if (rs.next()) { //将指针向前移动一行,判断是否有记录
count=rs.getInt(1);
}
System.out.println("共有"+count+"个用户!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//5.关闭资源
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Test02().queryUserCount();
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
/*
* 将数据库连接信息保存在文件中
*/
public class Test03 {
public static void main(String[] args) throws Exception {
Properties p=new Properties();
p.load(new FileInputStream("jdbc.properties"));
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", p);
System.out.println("连接数据库成功! ");
}
}
原文地址:http://blog.csdn.net/wangzi11322/article/details/44876065