JDBC编程简介
JDBC编程简介
jdbc的定义:JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
jdbc编程步骤:
[注意]:需要先将mysql提供的对应jar包导入项目
- 加载驱动:
* Class.forName(“com.mysql.jdbc.Driver”); //加载MySql数据库(或者通过new)
* new com.mysql.jdbc.Driver();
获取Connection对象:
- Connection conn = DriverManger.getConntection(“jdbc
//localhost:8080/db_name”);
- Connection conn = DriverManager.getConnection(“jdbc
//localhost:8080/db_name”,”username”,”password”);
- Connection conn = DriverManger.getConntection(“jdbc
获取状态
- Statement stmt = conn.createStatement();
- Statement stmt = conn.createStatement(resultSetType,resultSetConcurrency);
[注]resultSetType:结果集类型,有三种ResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE(对滚动不敏感),ResultSet.TYPE_SCROLL_SENSITIVE(对滚动敏感);resultSetConcurrency:并发类型,有2种ResultSet.CONCUR_READ_ONLY(并发操作是只读,对所有数据库支持),ResultSet.CONCUR_UPDATABLE(并发操作时可更新,有些 有些数据库不支持)
执行SQL语句
- stmt.execute(“”);
- stmt.executeQuery(“”);//执行查询语句
- stmt.executeUpdate(“”);//执行更新语句
获取ResultSet结果集
- ResultSet result = stmt.executeQuery(“”);
- while (result.next()) { } //循环获取
- 关闭 (最后的最先关)
* result.close();
* stmt.close();
* conn.close();
Statement与PreparedStatement的区别
PreparedStatement继承Statement。
- Statement:由方法createStatement所创建。Statement对象用于发送简单的SQL语句。
- PreparedStatement:由方法prepareStatement所创建。PreparedStatement对象用于发送带有一个或多个输入参数(IN参数)的SQL语句。PreparedStatement拥有一组方法,用于设置IN参数的值。
执行语句的区别
- Statement stmt = conn.createStatement();
- stmt.execute(“insert into table_name values (2, 14, ‘LISI’)”
- //
- PreparedStatement pstmt = conn.prepareStatement(“insert into table_name values (?, ?, ?)”);
- pstmt.setInt(1, 2);
- pstmt.setInt(2, 14);
- pstmt.setString(3, “LISI”);
- 批处理及其区别
* stmt.addBatch(“insert into table\_name values (2, 14, ‘LISI’)”);
* stmt.addBatch(“insert into table\_name values (3, 16, ‘ZHANGSAN’)”);
* stmt.executeBatch();
* //
* pstmt.addBatch(“insert into table\_name values (?, ?, ?)”);
* pstmt.setInt(1, 2);
* pstmt.setInt(2, 14);
* pstmt.setString(3, “LISI”);
* pstmt.addBatch(“insert into table\_name values (?, ?, ?)”);
* pstmt.setInt(1, 3);
* pstmt.setInt(2, 16);
* pstmt.setString(3, “ZHANGSAN”);
事务
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:8080/db_name", "username", "password");
conn.setAutoCommit(false);//设置不自动提交(默认自动提交sql语句)
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);//params1:设置ResultSet的TYPE;params2:设置并发
stmt.execute("insert into table_name values (2, 14, 'LISI')");
conn.commit();
conn.setAutoCommit(true);//恢复现场
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
if (conn != null){
try {
conn.rollback();//事务回滚
conn.setAutoCommit(true);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
还没有评论,来说两句吧...