文章目录
class PrepareStatementInsertPrepareStatementUpdatePrepareStatementDeleteclass PrepareStatementInsert
import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class PrepareStatementInsert {public static void main(String[] args) {Connection connection =null;PreparedStatement preparedStatement=null;try {//第一步:注册驱动Class.forName("com.mysql.jdbc.Driver");//第二步:创建连接connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mobilemallsystem","root","123456");//第三步:获取SQL语句预编译对象String sql="insert into usermessage(uname,upassword) values(?,?)";preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1,"jack");preparedStatement.setString(2,"123456");//第四步:执行SQL语句int i = preparedStatement.executeUpdate();if(i==1){System.out.println("数据插入成功");}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException throwables) {throwables.printStackTrace();}}}
PrepareStatementUpdate
import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class PrepareStatementUpdate {public static void main(String[] args) {Connection connection =null;PreparedStatement preparedStatement=null;try {//第一步:注册驱动Class.forName("com.mysql.jdbc.Driver");//第二步:创建连接connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mobilemallsystem","root","123456");//第三步:获取SQL语句预编译对象String sql="update usermessage set uname=?,upassword=? where uid=?";preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1,"jieke");preparedStatement.setString(2,"123456");preparedStatement.setInt(3,31);//第四步:执行SQL语句int i = preparedStatement.executeUpdate();if(i==1){System.out.println("数据插入成功");}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException throwables) {throwables.printStackTrace();}}}
PrepareStatementDelete
import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class PrepareStatementDelete {public static void main(String[] args) {Connection connection =null;PreparedStatement preparedStatement=null;try {//第一步:注册驱动Class.forName("com.mysql.jdbc.Driver");//第二步:创建连接connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mobilemallsystem","root","123456");//第三步:获取SQL语句预编译对象String sql="delete from usermessage where uid=?";preparedStatement = connection.prepareStatement(sql);preparedStatement.setInt(1,31);//第四步:执行SQL语句int i = preparedStatement.executeUpdate();if(i==1){System.out.println("数据插入成功");}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException throwables) {throwables.printStackTrace();}}}