1 package jdbcDome;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.util.Scanner;
8
9 import javafx.geometry.Side;
10
11
12
13 /**
14 *
15 * @author 不凡
16 *这里是使用的基础的方法,也是一开始学习的方法,
17 * 但是不能防止SQL注入,(但是可以在输入框中限制特殊字符输入来达到同样的目的)
18 * 下面的是预编译语句对象
19 */
20 public class DBText {
21 public static void main(String[] args) {
22 /*
23
24 *
25 *
26 jdbcTools db1 =jdbcTools.getDB();
27 Scanner sc1 =new Scanner(System.in);
28 System.out.println("请输入你的账号: ");
29 int Sid = sc1.nextInt();
30 System.out.println("请输入你的密码");
31 String pwd = sc1.next();
32 String sqlQuery="select * from student where sid = "+Sid+" and pwd ='"+pwd+"'";
33
34 ResultSet rs1 = db1.query(sqlQuery);
35 try {
36 if(rs1.next())
37 {
38 System.out.println("登陆成功!");
39 System.out.println(sqlQuery);
40 System.out.println(rs1.getString(2));
41 }else {
42 System.out.println("登陆失败!");
43
44 }
45
46 } catch (SQLException e) {
47 // TODO Auto-generated catch block
48 e.printStackTrace();
49 }
50
51 */
52 //--------------------------------------------------------------------------------------------
53 //这里使用的预编译语句对象 有三个特点
54 /* 1.防止SQL注入
55 * 2.简单
56 * 3.可以用(?)问号,充当占位 更加方便
57 * 4.语句只编译执行效率更高。
58 *
59 */
60 Scanner sc1 =new Scanner(System.in);
61 System.out.println("请输入你的账号: ");
62 int Sid = sc1.nextInt();
63 System.out.println("请输入你的密码");
64 String pwd = sc1.next();
65 // 问好(?) 表示占位符
66 String sqlQuery="select * from student where sid = ? and pwd =?";
67 Connection con1 = jdbcTools.getConn();
68 try {
69 //生成预编译语句对象
70 PreparedStatement pSt = con1.prepareStatement(sqlQuery);
71 //给占位符赋值
72 pSt.setInt(1, Sid);
73 pSt.setString(2, pwd);
74 //执行预编译对象
75 ResultSet rs2= pSt.executeQuery();
76 if(rs2.next()){
77 System.out.println("登陆成功");
78 rs2.getString(2);
79 }else{
80 System.out.println("登陆失败!");
81 }
82
83 } catch (SQLException e) {
84 e.printStackTrace();
85 }
86
87 }
88
89 }