PreparedStatement增删改
增加 :
package com.tospo.test;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class JdbcTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/firstdatabase","root","root");
//3.获取预编译的数据库操作对象
String sql = "insert into t_user(username,password) values (?,?)";
ps=conn.prepareStatement(sql);
ps.setString(1,"小明");
ps.setInt(2,123);
//4.执行sql语句
int count =ps.executeUpdate();
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(ps!=null){
ps.close();}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
异常错误:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
错误原因:没有启动mysql服务器。启动后成功运行。
修改:
package com.tospo.test;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class JdbcTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/firstdatabase","root","root");
//3.获取预编译的数据库操作对象
String sql = "update t_user set password= ? where username= ?";
ps=conn.prepareStatement(sql);
ps.setString(2,"小明");
ps.setInt(1,12345678);
//4.执行sql语句
int count =ps.executeUpdate();
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(ps!=null){
ps.close();}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```java
在这里插入代码片
删除:
package com.tospo.test;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class JdbcTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/firstdatabase","root","root");
//3.获取预编译的数据库操作对象
String sql = "delete from t_user where username= ?";
ps=conn.prepareStatement(sql);
ps.setString(1,"小明");
//4.执行sql语句
int count =ps.executeUpdate();
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(ps!=null){
ps.close();}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
事务 (jdbc 为自动提交机制)
Connection conn = null; //将自动提交机制修改为手动提交 //开启事务
conn.setAutoCommit(false);
·(sql语句) ·(sql语句) ·(sql语句)…
//提交事务
conn.commit(); 若出现异常,在catch语句块中
回滚事务
if(conn !=null){
try{
conn.rollback();
}catch(SQLException e1){
e1.printStackTrace(); }
JDBC工具类封装
工具类中构造方法都是私有的。
因为工具类当中的方法都是静态的,不需要new对象,直接采用类名调用。
package com.tospo.test.utils;
import java.sql.*;
public class DBUtil {
private DBUtil(){}
//静态代码块在类加载时执行,并且只执行一次
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接对象
* @return 连接对象
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/firstdatabase","root","root");
}
public static void close(Connection conn, Statement ps , ResultSet rs){
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();
}
}
}
}
调用工具包实现模糊查询
package com.tospo.test;
import com.tospo.test.utils.DBUtil;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class JdbcTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
//获取连接
try {
conn = DBUtil.getConnection();
//获取预编译的数据库操作对象
String sql = "select username from t_User where username like ?";
ps=conn.prepareStatement(sql);
ps.setString(1,"_e%");
rs = ps.executeQuery();
while (rs.next()){
System.out.println(rs.getString("username"),rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,rs);
}
}
}
报错:java.sql.SQLException: Column ‘password’ not found.
错误原因:rs.getString()方法中的东西只能找到Select 之前选择显示的东西
把
String sql = “select username from t_User where username like ?”;
改成
String sql = “select username,password from t_User where username like ?”;
即可
行级锁(悲观锁):
事务必须排队执行。数据锁住了,不允许并发。
表中job为MANAGER的各行数据都不能再被修改
乐观锁:支持并发,事务也不需要排队,只不过需要一个版本号。