JDBC:java database connectivity SUN公司提供的一套操作数据库的标准规范。
JDBC与数据库驱动的关系:接口与实现的关系。
JDBC规范(掌握四个核心对象):
DriverManager:用于注册驱动
Connection: 表示与数据库创建的连接
Statement: 操作数据库sql语句的对象
ResultSet: 结果集或一张虚拟表
实体类
package com.mf.domain; import java.util.Date; public class User { private int id; private String name; private int age; private Date createtime; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } }
CRUD
package com.mf.jdbc; import java.sql.DriverManager; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.mf.domain.User; public class CRUD { public static void main(String[] args) throws Exception { //3种连接方式 Connection conn=null; //得到执行sequel语句的对象Statement Statement stmt=null; //查 执行sql语句,并返回结果 ResultSet rs=null; try { //注册驱动 //DriverManager.registerDriver(new com.mysql.jdbc.Driver());//不建议使用原因有2个:> 导致驱动被注册2次。> 强烈依赖数据库的驱动jar Class.forName("com.mysql.jdbc.Driver");//此处采用反射机制加载 //获取连接Connection //Connection 接口 多态 父类的壳子接收子类的实现,有利于后期代码的扩展 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); /*Properties info=new Properties(); info.setProperty("user", "root"); info.setProperty("password", "root"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", info);*/ //Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=root"); stmt = conn.createStatement(); //增 /*stmt = conn.prepareStatement("INSERT INTO users VALUES(?,?)"); ((PreparedStatement) stmt).setString(1, "tom"); ((PreparedStatement) stmt).setInt(2, 5);*/ /*int i = stmt.executeUpdate("INSERT INTO USER (`Name`, `Age`) VALUES('mf',20)"); if(i>0){ System.out.println("success"); } //删 int j = stmt.executeUpdate("UPDATE USER SET Age=25 WHERE ID=1"); if(j>0){ System.out.println("success"); } //改 int k = stmt.executeUpdate("DELETE FROM USER WHERE ID = 1 "); if(k>0){ System.out.println("success"); }*/ rs = stmt.executeQuery("select * from user"); //处理结果 List<User> list=new ArrayList<User>(); while(rs.next()){ User user=new User(); user.setName(rs.getString("Name")); user.setAge(rs.getInt("Age")); user.setCreatetime(rs.getDate("CreatTime")); list.add(user); System.out.println(list.size()); System.out.println("---------------------------------------------------"); System.out.println(rs.getObject("Name")+":"+rs.getObject("Age")+":"+rs.getObject("CreatTime")); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ //关闭资源 if(rs!=null) { try { rs.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } rs=null; } if(stmt!=null) { try { stmt.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } stmt=null; } if(conn!=null) { try { conn.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } conn=null; } } } }