执行曾加
String url="jdbc:mysql://127.0.0.1:3306/test?useSSL=false";
String username="root";
String password="123456";
String sql="INSERT INTO USER (id,username,PASSWORD,age,sex,STATUS) VALUES (?,?,?,?,?,?)";
Connection conn=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,username,password);
if(conn!=null && !conn.isClosed()){
System.out.println("数据库连接成功!");
}
PreparedStatement ps=conn.prepareStatement(sql);
ps.setObject(1,"1");
ps.setObject(2,"lucy");
ps.setObject(3,"123456");
ps.setObject(4,20);
ps.setObject(5,"0");
ps.setObject(6,"1");
int num=ps.executeUpdate();
System.out.println(num);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
if(conn!=null && !conn.isClosed()){
conn.close();
System.out.println("关闭数据库连接!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
执行删除
String url="jdbc:mysql://127.0.0.1:3306/test?useSSL=false";
String username="root";
String password="123456";
String sql="delete from user where id=?";
Connection conn=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,username,password);
if(conn!=null && !conn.isClosed()){
System.out.println("数据库连接成功!");
}
PreparedStatement ps=conn.prepareStatement(sql);
ps.setObject(1,"10003");
int num=ps.executeUpdate();
System.out.println(num);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
if(conn!=null && !conn.isClosed()){
conn.close();
System.out.println("关闭数据库连接!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
执行更新
String url="jdbc:mysql://127.0.0.1:3306/test?useSSL=false";
String username="root";
String password="123456";
String sql="update user set password=? where id=?";
Connection conn=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,username,password);
if(conn!=null && !conn.isClosed()){
System.out.println("数据库连接成功!");
}
PreparedStatement ps=conn.prepareStatement(sql);
ps.setObject(1,"12345678");
ps.setObject(2,"1000");
int num=ps.executeUpdate();
System.out.println(num);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
if(conn!=null && !conn.isClosed()){
conn.close();
System.out.println("关闭数据库连接!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
原文出处:/show_1EF63mPOljfR.html