1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > JDBC PrepareStatement insert delete update DML

JDBC PrepareStatement insert delete update DML

时间:2023-05-04 01:46:37

相关推荐

JDBC PrepareStatement insert delete update DML

文章目录

class PrepareStatementInsertPrepareStatementUpdatePrepareStatementDelete

class 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();}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。