JDBC执行增删改查
insert
insert用的执行sql语句的方法是executeUpdate()
,返回影响的记录数量
package com.jdbctest;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Insert {
@Test
public void demo() throws ClassNotFoundException, SQLException {
//开头声明变量
Connection conn = null;
Statement stmt = null;
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获得连接
conn = DriverManager.getConnection("jdbc:mysql:///java", "root", "");
// 获取执行sql语句的对象
stmt = conn.createStatement();
// 编写sql语句
String sql = "insert into test values (4,111,'111')";
//执行插入语句,使用executeUpdate方法
int num = stmt.executeUpdate(sql);
//返回的数字是收到影响的记录数量
if (num > 0) {
System.out.println("数据添加成功");
}
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
//手动设空
stmt.close();
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
//手动设空
conn = null;
}
}
成功插入!
删除、修改
与插入同理,用executeUpdate()
就可以了
查询
查询用executeQuery()
,基本不用execute()
JDBC的工具类提取
在使用JDBC的时候有很多的重复操作,如:注册驱动、创建连接、释放资源。
我们可以通过提取工具类,把这些操作放到一个类里面来简化我们的操作
创建一个包,在里面创建一个JDBCtool类用来作为提取的工具类
package tool;
//注册驱动
// 获得连接
// 释放资源
import org.junit.Test;
import javax.xml.transform.Result;
import java.sql.*;
public class JDBCtool {
// 先声明好变量
private static final String driverClassName;
private static final String url;
private static final String username;
private static final String password;
// 为变量赋值,以便后期的引用
static {
driverClassName="com.mysql.jdbc.Driver";
url="jdbc:mysql:///java";
username="root";
password="";
}
@Test
// 注册驱动的方法
public static void loadDriver(){
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获得连接,一并注册驱动
public static Connection getconn() throws SQLException {
// 先定义变量,设空值
Connection conn = null;
loadDriver();
conn = DriverManager.getConnection(url,username,password);
return conn;
}
// 释放资源,由于查询语句会多一个资源result要释放,所以使用重载来做
public static void release (Statement stmt,Connection conn){
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
stmt = null;
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
conn = null;
}
//重载方法
public static void release (ResultSet res, Statement stmt, Connection conn){
try {
res.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
res = null;
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
stmt = null;
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
conn = null;
}
}
工具类的测试
package com.jdbctest;
import org.junit.Test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static tool.JDBCtool.getConnection;
import static tool.JDBCtool.release;
public class ToolTest {
@Test
public void demo(){
ResultSet res = null;
Statement stmt = null;
Connection conn = null;
try {
// 注册驱动并获得连接
conn = getConnection();
//获得执行sql语句的对象
stmt = conn.createStatement();
//编写sql语句并执行
String sql = "select * from test";
res = stmt.executeQuery(sql);
//遍历结果集
while (res.next()){
System.out.print(res.getString("id")+' ');
System.out.print(res.getString("username")+' ');
System.out.println(res.getString("passwd"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
//释放资源
release(res,stmt,conn);
}
}
}
查询成功
批处理
进行批处理有两个步骤
1、添加到批处理
2、执行批处理
//先写好几个sql语句
// 添加到批处理
stmt.addBatch(sql1);
stmt.addBatch(sql2);
stmt.addBatch(sql3);
stmt.addBatch(sql4);
stmt.addBatch(sql5);
// 执行批处理:
stmt.executeBatch();
预处理
预处理是用到了JDBC的PreparedStatement接口
这个接口用来动态的执行sql语句,通过这个接口实例执行的动态sql语句将被预编译并保存到PreparedStatement实例中,从而可以反复的执行这个sql语句
给多次改变的位置写上
?
,再用setString()
给?赋值
public void demo2(){
Connection conn = null;
PreparedStatement pstmt = null;
try{
// 获得连接:
conn = JDBCtool.getConnection();
// 编写SQL语句:
String sql = "insert into user values (null,?)";
// 预编译SQL:
pstmt = conn.prepareStatement(sql);
for(int i=1;i<=10000;i++){
pstmt.setString(1, "name"+i);
// 添加到批处理
pstmt.addBatch();
// 执行批处理
if(i % 1000 == 0){
// 执行批处理:
pstmt.executeBatch();
// 清空批处理:
pstmt.clearBatch();
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCtool.release(pstmt, conn);
}
}