什么是JDBC
JDBC全称Java DataBase Connection,中文意思是Java数据库连接。那么JDBC的作用是什么呢,通过使用JDBC,开发者能够非常方便地将SQL语句传送给各种数据库,对数据库进行操作。
实际操作
PreparedStatement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public class Demo3 { public static void main(String[] args) { Connection conn = null; PreparedStatement ps =null;
try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","Jq576163960");
String sql = "insert into t_user (username,salary) values (?,?)"; ps = conn.prepareStatement(sql);
ps.setObject(1, "金奇2"); ps.setObject(2, "234567");
System.out.println("插入一行记录");
int count = ps.executeUpdate(); System.out.println(count);
} catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if(ps!=null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
|
通常我们使用PreparedStatement,因为使用Statement会有SQL注入的风险,不推荐使用Statement。
事务
为了让多个SQL语句作为一个事务运行,基本步骤为:先调用Connection对象的setAutoCommit(false),以取消自己主动提交事务,然后在全部的SQL语句都成功运行后,调用commit(),假设在出现异常时,能够调用rollback()使方法回滚事务。事务的特性:执行多条SQL语句,要么全部成功,要么全部失败,如果失败回滚到执行第一条SQL语句之前的状态。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| public class Demo6 { public static void main(String[] args) { Connection conn = null; PreparedStatement ps1 =null; PreparedStatement ps2 =null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=utf-8&useSSL=false","root","Jq576163960");
conn.setAutoCommit(false);
ps1 = conn.prepareStatement("insert into t_user (username,salary) values (?,?)"); ps1.setObject(1, "金奇"); ps1.setObject(2, "123456"); ps1.execute(); System.out.println("插入一个用户");
try { Thread.sleep(6000); } catch (InterruptedException e) { e.printStackTrace(); }
ps2 = conn.prepareStatement("insert into t_user (username,salary) values (?,?)"); ps2.setObject(1, "jinqi"); ps2.setObject(2, "123456"); ps2.execute(); System.out.println("插入一个用户");
conn.commit();
} catch (ClassNotFoundException e) { e.printStackTrace(); try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } catch (SQLException e) { e.printStackTrace(); }finally {
if(ps1!=null) { try { ps1.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
|
封装连接
在实际开发过程中,我们不可能每次都要手动连接数据库,因此我们需要将连接和关闭资源的过程封装成一个工具类,连接信息则写入db.properties文件中,下一次修改连接信息时,只需要修改db.properties文件即可。
db.properties
1 2 3 4
| mysqlDriver=com.mysql.jdbc.Driver mysqlURL=jdbc:mysql: mysqlUser=root mysqlPwd=Jq576163960
|
JDBC连接类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| public class JDBCUtil {
static Properties pros = null;
static { pros = new Properties(); try { pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties")); } catch (IOException e) { e.printStackTrace(); } }
public static Connection getMysqlConn() { try { Class.forName(pros.getProperty("mysqlDriver")); return DriverManager.getConnection(pros.getProperty("mysqlURL"), pros.getProperty("mysqlUser"),pros.getProperty("mysqlPwd")); } catch (Exception e) { e.printStackTrace(); return null; } }
public static void close(ResultSet rs,Statement ps,Connection conn) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(ps!=null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
public static void close(Statement ps,Connection conn) { if(ps!=null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
public static void close(Connection conn) { if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
|
测试连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class Demo11 {
public static void main(String[] args) { Connection conn = null; PreparedStatement ps =null; ResultSet rs = null;
try { conn = JDBCUtil.getMysqlConn(); ps = conn.prepareStatement("insert into t_user (username) values (?)"); ps.setString(1, "金奇2"); ps.execute();
} catch (Exception e) { e.printStackTrace(); }finally { JDBCUtil.close(rs, ps, conn); } } }
|