CRUD即为增删改查
增加的代码实现
public class updatreTest {
public static void main(String[] args)
throws SQLException, IOException, ClassNotFoundException, ParseException {
Connection conn = null;
PreparedStatement ps = null;
try {//ctrl+alt+t 自动创建try-catch语句
conn = getconnection();
//预编译sql语句,返回preparedstatement实例,这里面插入的值用占位符表示
String sql = "insert into customers(name,email,birth) values(?,?,?)";
ps = conn.prepareStatement(sql);
//填充占位符
ps.setString(1,"哪吒");
ps.setString(2,"nezha@gmail.com");
SimpleDateFormat si = new SimpleDateFormat("yyyy-MM-dd");
Date date = si.parse("1000-01-01");
ps.setDate(3,new java.sql.Date(date.getTime()));
//执行sql
ps.execute();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
//资源关闭
try {
if(ps!=null) {ps.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if(conn!=null) {conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//获取连接
public static Connection getconnection() throws IOException, ClassNotFoundException, SQLException {
InputStream res = ClassLoader.getSystemClassLoader() //获取系统类加载器
.getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(res);
String drivers = pros.getProperty("driverclass");
String url = pros.getProperty("url");
String user = pros.getProperty("user");
String password =pros.getProperty("password");
Class cl = Class.forName(drivers);
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
}
修改记录的代码实现:
public class updatreTest {
public static void main(String[] args)
throws SQLException, IOException, ClassNotFoundException, ParseException {
PreparedStatement ps = null;
Connection conn = null;
try {//ctrl+alt+t 自动创建try-catch语句
conn = getconnection();
//预编译sql语句,返回preparedstatement实例,这里面插入的值用占位符表示
String sql = "update customers set name = ? where id = ?";
ps = conn.prepareStatement(sql);
//填充占位符,此处统一使用object
ps.setObject(1,"莫扎特");
ps.setObject(2,18);
//执行sql
ps.execute();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
//资源关闭
closeresource(conn, ps);
}
}
//获取连接
public static Connection getconnection() throws IOException, ClassNotFoundException, SQLException {
InputStream res = ClassLoader.getSystemClassLoader() //获取系统类加载器
.getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(res);
String drivers = pros.getProperty("driverclass");
String url = pros.getProperty("url");
String user = pros.getProperty("user");
String password =pros.getProperty("password");
Class cl = Class.forName(drivers);
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
public static void closeresource(Connection conn, Statement ps){
try{if(ps!=null) {ps.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if(conn!=null) {conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
通用方法代码,即增删改
//通用的增改改操作的方法,即修改表
//Object...args 表示可变个数的参数,类型统一为object,此处用来表示占位符,args表示一个数组
public void update(String sql,Object...args) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
//1、获取连接
conn = getconnection();
//2、预编译
ps = conn.prepareStatement(sql);
//3、填充占位符
for(int i =0;i<args.length;i++){
ps.setObject(i+1,args[i]);//小心参数声明操作
}
//4、执行
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
//5、关闭资源
closeresource(conn, ps);
} }
//获取连接
public static Connection getconnection() throws IOException, ClassNotFoundException, SQLException {
InputStream res = ClassLoader.getSystemClassLoader() //获取系统类加载器
.getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(res);
String drivers = pros.getProperty("driverclass");
String url = pros.getProperty("url");
String user = pros.getProperty("user");
String password =pros.getProperty("password");
Class cl = Class.forName(drivers);
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
public static void closeresource(Connection conn, Statement ps){
try{if(ps!=null) {ps.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if(conn!=null) {conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}