parent
efadd43245
commit
3746442c2f
@ -0,0 +1,78 @@
|
||||
package com.msb.test2;
|
||||
|
||||
import com.msb.bean.Account;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class TestInjection2 {
|
||||
public static String driver = "com.mysql.cj.jdbc.Driver";
|
||||
public static String url = "jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
|
||||
public static String user = "root";
|
||||
public static String password = "Fnst1234";
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("请输入用户名");
|
||||
String username = scanner.next();
|
||||
System.out.println("请输入密码");
|
||||
String pwd = scanner.next();
|
||||
|
||||
Account account = queryAccount(username, pwd);
|
||||
System.out.println(account != null ? "登录成功" : "登录失败");
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
public static Account queryAccount(String userName, String pwd) {
|
||||
Account result = null;
|
||||
|
||||
Connection connection = null;
|
||||
PreparedStatement statement = null;
|
||||
ResultSet resultSet = null;
|
||||
|
||||
try {
|
||||
// 选择数据库
|
||||
// Driver driver = new com.mysql.cj.jdbc.Driver();
|
||||
// DriverManager.registerDriver(driver);
|
||||
Class.forName(driver);
|
||||
|
||||
// 建立连接
|
||||
connection = DriverManager.getConnection(url, user, password);
|
||||
|
||||
// 执行sql
|
||||
String sql = "select * from account where username = ? and password = ?";
|
||||
statement = connection.prepareStatement(sql);
|
||||
statement.setString(1, userName);
|
||||
statement.setString(2, pwd);
|
||||
resultSet = statement.executeQuery();
|
||||
if (resultSet.next()) {
|
||||
int aid = resultSet.getInt("aid");
|
||||
String usernamea = resultSet.getString("username");
|
||||
String pwda = resultSet.getString("password");
|
||||
double money = resultSet.getDouble("money");
|
||||
result = new Account(aid,usernamea,pwda,money);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (resultSet != null) {
|
||||
resultSet.close();
|
||||
}
|
||||
if (statement != null) {
|
||||
statement.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
package com.msb.test3;
|
||||
|
||||
import com.msb.bean.Account;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
public class TestPreparedStatement {
|
||||
public static String driver = "com.mysql.cj.jdbc.Driver";
|
||||
public static String url = "jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
|
||||
public static String user = "root";
|
||||
public static String password = "Fnst1234";
|
||||
|
||||
public static void main(String[] args) {
|
||||
// testAdd();
|
||||
// testUpdate();
|
||||
testDelete();
|
||||
}
|
||||
|
||||
public static void testAdd() {
|
||||
Connection connection = null;
|
||||
PreparedStatement statement = null;
|
||||
|
||||
try {
|
||||
// 选择数据库
|
||||
Class.forName(driver);
|
||||
|
||||
// 建立连接
|
||||
connection = DriverManager.getConnection(url, user, password);
|
||||
|
||||
// 执行sql
|
||||
String sql = "insert into account values (DEFAULT,?,?,?)";
|
||||
statement = connection.prepareStatement(sql);
|
||||
statement.setString(1, "dingyl");
|
||||
statement.setString(2, "123456");
|
||||
statement.setDouble(3, 10000000);
|
||||
int rows = statement.executeUpdate();
|
||||
System.out.println("影响行数据:" + rows);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (statement != null) {
|
||||
statement.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void testUpdate() {
|
||||
Connection connection = null;
|
||||
PreparedStatement statement = null;
|
||||
|
||||
try {
|
||||
// 选择数据库
|
||||
Class.forName(driver);
|
||||
|
||||
// 建立连接
|
||||
connection = DriverManager.getConnection(url, user, password);
|
||||
|
||||
// 执行sql
|
||||
String sql = "update account set password = ? where aid = 3";
|
||||
statement = connection.prepareStatement(sql);
|
||||
statement.setString(1, "654321");
|
||||
int rows = statement.executeUpdate();
|
||||
System.out.println("影响行数据:" + rows);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (statement != null) {
|
||||
statement.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void testDelete() {
|
||||
Connection connection = null;
|
||||
PreparedStatement statement = null;
|
||||
|
||||
try {
|
||||
// 选择数据库
|
||||
Class.forName(driver);
|
||||
|
||||
// 建立连接
|
||||
connection = DriverManager.getConnection(url, user, password);
|
||||
|
||||
// 执行sql
|
||||
String sql = "delete from account where aid = ?";
|
||||
statement = connection.prepareStatement(sql);
|
||||
statement.setInt(1, 3);
|
||||
int rows = statement.executeUpdate();
|
||||
System.out.println("影响行数据:" + rows);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (statement != null) {
|
||||
statement.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.msb.test4;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class TestBatch {
|
||||
public static String driver = "com.mysql.cj.jdbc.Driver";
|
||||
public static String url = "jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useServerPrepStmts=true&cachePrepStmts=true&&rewriteBatchedStatements=true";
|
||||
public static String user = "root";
|
||||
public static String password = "Fnst1234";
|
||||
|
||||
public static void main(String[] args) {
|
||||
testAddBatch();
|
||||
}
|
||||
|
||||
public static void testAddBatch() {
|
||||
Connection connection = null;
|
||||
PreparedStatement statement = null;
|
||||
|
||||
try {
|
||||
// 选择数据库
|
||||
Class.forName(driver);
|
||||
|
||||
// 建立连接
|
||||
connection = DriverManager.getConnection(url, user, password);
|
||||
|
||||
// 执行sql
|
||||
String sql = "insert into account values (DEFAULT,?,?,?)";
|
||||
statement = connection.prepareStatement(sql);
|
||||
int rows = 0;
|
||||
for (int i = 1; i <= 10000; i++) {
|
||||
statement.setString(1, "name");
|
||||
statement.setString(2, "pwd");
|
||||
statement.setDouble(3, 1000);
|
||||
statement.addBatch();
|
||||
if (i % 100 == 0) {
|
||||
int[] ss = statement.executeBatch();
|
||||
rows += ss.length;
|
||||
}
|
||||
}
|
||||
System.out.println("影响行数据:" + rows);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (statement != null) {
|
||||
statement.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue