标准JDBC操作五步骤
package jdbc.sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 标准JDBC操作五步骤
*/
public class StandardJDBCSample {
public static void main(String[] args) {
Connection conn = null;
try {
//1. 加载并注册JDBC驱动
//8.0
Class.forName("com.mysql.cj.jdbc.Driver");
//5.0
Class.forName("com.mysql.jdbc.Driver");
//2. 创建数据库连接
//8.0
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&&allowPublicKeyRetrieval=true",
"root", "root"
);
//5.0
DriverManager.getConnection("jdbc:mysql://localhost:3306...","root","root");
//3. 创建Statement对象
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from employee where dname='研发部'");
//4. 遍历查询结果
while (rs.next()) {
Integer eno = rs.getInt(1); //eno
String ename = rs.getString("ename");
Float salary = rs.getFloat("salary");
String dname = rs.getString("dname");
System.out.println(dname + "-" + eno + "-" + ename + "-" + salary);
}
}catch(Exception e){
e.printStackTrace();
}finally {
try {//5. 关闭连接,释放资源
if (conn != null && conn.isClosed() == false) {
conn.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
自动提交事务
package jdbc.sample;
import jdbc.common.DbUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
/**
* JDBC批处理
*/
public class BatchSample {
//标准方式插入若干数据
private static void tc1(){
Connection conn = null;
PreparedStatement pstmt = null;
try {
long startTime = new Date().getTime();
conn = DbUtils.getConnection();
//JDBC默认使用自动提交模式
conn.setAutoCommit(false);//关闭自动提交
String sql = "insert into employee(eno,ename,salary,dname) values(?,?,?,?)";
for (int i=100000;i<200000;i++){
// if (i == 10) {
//// throw new RuntimeException("插入失败");
// }
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, i);
pstmt.setString(2, "员工" + i);
pstmt.setFloat(3, 4000f);
pstmt.setString(4, "市场部");
pstmt.executeUpdate();
}
conn.commit();//提交数据
long endTime = new Date().getTime();
System.out.println("tc1()执行时长:" + (endTime-startTime));
} catch (Exception e) {
e.printStackTrace();
try {
if(conn != null && !conn.isClosed()) {
conn.rollback();//回滚数据
}
}catch (SQLException ex){
ex.printStackTrace();
}
} finally {
DbUtils.closeConnection(null, pstmt, conn);
}
}
//使用批处理插入若干数据
private static void tc2(){
Connection conn = null;
PreparedStatement pstmt = null;
try {
long startTime = new Date().getTime();
conn = DbUtils.getConnection();
//JDBC默认使用自动提交模式
conn.setAutoCommit(false);//关闭自动提交
String sql = "insert into employee(eno,ename,salary,dname) values(?,?,?,?)";
pstmt = conn.prepareStatement(sql);
for (int i=200000;i<300000;i++){
// if (i == 10) {
//// throw new RuntimeException("插入失败");
// }
pstmt.setInt(1, i);
pstmt.setString(2, "员工" + i);
pstmt.setFloat(3, 4000f);
pstmt.setString(4, "市场部");
pstmt.addBatch();//将参数加入批处理任务
// pstmt.executeUpdate();对同一个对象进行操作所以注释
}
pstmt.executeBatch();//执行批处理任务(一次性打包 一下子全部发给mysql)
conn.commit();//提交数据
long endTime = new Date().getTime();
System.out.println("tc2()执行时长:" + (endTime-startTime));
} catch (Exception e) {
e.printStackTrace();
try {
if(conn != null && !conn.isClosed()) {
conn.rollback();//回滚数据
}
}catch (SQLException ex){
ex.printStackTrace();
}
} finally {
DbUtils.closeConnection(null, pstmt, conn);
}
}
public static void main(String[] args) {
tc1();
tc2();
}
}