增
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1/soft03
user=root
password=root
package com.yanyu;
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCTest01 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection con = null;
Statement st = null;
ResultSet rs = null;
// 引用类型 默认值 NULL
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);// alt enter
con.setAutoCommit(false);
st = con.createStatement();
String sql = "insert into t_user values(1,'yy')";
st.execute(sql);
con.commit();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
// rs st con
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}
改
package com.yanyu;
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCTest02 {
// 防止sql 注入
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection con = null;
// 操作对象
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
con.setAutoCommit(false);
String sql = "update t_user set name = ? where id = ?";
// String sql = "update t_user set name = yanyu where id = 1001";
// 1 2
ps = con.prepareStatement(sql);// 预编译
// 告诉系统 ? ? 什么东西
ps.setInt(2,1);
// 第 2 个 ? 是 int 1001
ps.setString(1,"yanyu");
// 之u行 SQL
ps.executeUpdate();
con.commit();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}
删除
package com.yanyu;
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCTest04 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
con.setAutoCommit(false);
String sql = "delete from t_user where id = ?";
ps = con.prepareStatement(sql);
ps.setInt(1,1);
ps.execute();
con.commit();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}
package com.yanyu;
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCTest05 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
con.setAutoCommit(false);
String sql = "select * from t_user where id = ?";
ps = con.prepareStatement(sql);
ps.setInt(1,13);
rs = ps.executeQuery();
// 遍历 结果
while (rs.next()){
String id = rs.getString("id");
String name = rs.getString("name");
System.out.println(id);
System.out.println(name);
}
con.commit();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}