PreparedStatement完成增删改
package com.etc;
import java.sql.*;
import java.sql.PreparedStatement;
/*
PreparedStatement完成insert delete update
*/
public class JdbcTest07 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/test","root","root");
//获取编译的数据库操作对象
//插入
// String sql = "insert into t_user(id,loginName,loginPwd) values(?,?,?)";
// ps = conn.prepareStatement(sql);
// ps.setInt(1,10);
// ps.setString(2,"ma");
// ps.setString(3,"123456");
//修改
// String sql = "update t_user set loginName = ?, loginPwd = ? where id = ?";
// ps = conn.prepareStatement(sql);
// ps.setString(1,"msj");
// ps.setString(2,"1234566");
// ps.setInt(3,10);
//删除
String sql = "delete from t_user where id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,10);
//执行sql
int count = ps.executeUpdate();
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}