From efadd43245a46f685b4cb81ebcb496058d936802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=81=E4=B8=9A=E6=9E=97-17050417?= <17050417@suning-sports.com> Date: Sun, 25 Sep 2022 22:32:24 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC11=E8=8A=82=201.sql=E6=B3=A8=E5=85=A5?= =?UTF-8?q?=E6=BC=94=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JDBCDemo1/src/com/msb/bean/Account.java | 62 ++++++++++++++ .../src/com/msb/test2/TestInjection.java | 80 +++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 testJDBC/JDBCDemo1/src/com/msb/bean/Account.java create mode 100644 testJDBC/JDBCDemo1/src/com/msb/test2/TestInjection.java diff --git a/testJDBC/JDBCDemo1/src/com/msb/bean/Account.java b/testJDBC/JDBCDemo1/src/com/msb/bean/Account.java new file mode 100644 index 0000000..57641de --- /dev/null +++ b/testJDBC/JDBCDemo1/src/com/msb/bean/Account.java @@ -0,0 +1,62 @@ +package com.msb.bean; + +import java.io.Serializable; + +public class Account implements Serializable { + private Integer aid; + private String username; + private String password; + private Double money; + + public Account() { + } + + public Account(Integer aid, String username, String password, Double money) { + this.aid = aid; + this.username = username; + this.password = password; + this.money = money; + } + + public Integer getAid() { + return aid; + } + + public void setAid(Integer aid) { + this.aid = aid; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Double getMoney() { + return money; + } + + public void setMoney(Double money) { + this.money = money; + } + + @Override + public String toString() { + return "Account{" + + "aid=" + aid + + ", username='" + username + '\'' + + ", password='" + password + '\'' + + ", money=" + money + + '}'; + } +} diff --git a/testJDBC/JDBCDemo1/src/com/msb/test2/TestInjection.java b/testJDBC/JDBCDemo1/src/com/msb/test2/TestInjection.java new file mode 100644 index 0000000..fdfd568 --- /dev/null +++ b/testJDBC/JDBCDemo1/src/com/msb/test2/TestInjection.java @@ -0,0 +1,80 @@ +package com.msb.test2; + +import com.msb.bean.Account; +import com.msb.bean.Emp; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class TestInjection { + 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; + Statement 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 + statement = connection.createStatement(); + String sql = "select * from account where username='" + userName + "' and password='" + pwd + "'"; + System.out.println(sql); + resultSet = statement.executeQuery(sql); + 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; + } +}