https://docs.qq.com/sheet/DTHNxSUt1aU9xUWtx
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3307/yanyu
user=root
password=root
package com.yanyu;
import java.sql.*;
import java.util.ResourceBundle;
public class Demo03 {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("com\\yanyu\\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;
// 可以防止SQL注入的
ResultSet rs = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
con.setAutoCommit(false);
// 预编译的 SQL
String sql = "insert into user(name,password) values(?,?)";
// 1 2
// 预编译
ps = con.prepareStatement(sql);
// 没有具体的 数据 ,只有 ???
// 告诉系统 这两个 ? 是什么意思
ps.setString(1,"烟雨");
ps.setString(2,"123");
boolean execute = ps.execute();
System.out.println(!execute?"数据插入成功":"数据插入失败");
con.commit();
} catch (ClassNotFoundException | SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
e.printStackTrace();
}finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}