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");
String sql="insert into usermessage(uname,upassword) values(?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,"jack");
preparedStatement.setString(2,"123456");
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");
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);
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");
String sql="delete from usermessage where uid=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,31);
int i = preparedStatement.executeUpdate();
if(i==1){
System.out.println("数据插入成功");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}