分析:
String sql = "select * from user where username='"+username+"' and password='"+password+"'";
String sql = "select * from user where username='tom' and password='123'";
select * from user where username='jerry' and password='abc';//這種情況是不會有問題的
//測試如果傳入的是SQL語句的的一部分
username:tom' or '1'='1
password:????????
//這時候的SQL語句為一下這種情況
select * from user where username='tom' or '1'='1' and password='?????';
//由於and的執行優先級大於Or 可以看作以下情況
select * from user where username='tom' or ('1'='1' and password='?????');
//由於是or 或者的關系這時的SQL語句可以看作是這樣的語句
select * from user where username='tom';
二、解決方案(引入: PreparedStatement對象,使SQL語句進行預編譯 )
PreparedStatement pstmt = conn.perpareStatement(String sql);
其實statement執行executeQuery內部分為兩步:
第一步:編譯sql
第二步:執行sql
優點:能預編譯sql語句
例子:數據庫客戶端服務端基本模型
登陸端
package com.heima.login.client;
import java.util.Scanner;
import com.heima.login.bean.User;
import com.heima.login.seriver.services;
public class login {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// 創建Scanner接收輸入內容
- Scanner sc= new Scanner(System.in);
System.out.println("請輸入用戶名");
String username = sc.nextLine();
System.out.println("請輸入密碼");
String password = sc.nextLine();
// 創建服務端對象使用登陸方法
services s=new services();
User user = s.login(username, password);
if(user!=null){
System.out.println(user);
}else{
System.out.println("用戶名或密碼錯誤!!!");
}
}
}
服務端:
package com.heima.login.seriver;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.heima.Tools.JdbcConnection;
import com.heima.login.bean.User;
public class services {
public User login(String username,String password) throws Exception
{
//新建user引用,默認值問null
User u=null;
//使用自定義數據庫連接工具類進行數據庫連接
Connection conn=JdbcConnection.getConnection();
//創建預編譯數據庫連接語句
String sql = "select * from user where username=? and password=?";
//創建PreparedStatement對象對數據庫語句進行預編譯
PreparedStatement stem=conn.prepareStatement(sql);
//設置預編譯語句中的查詢值
stem.setString(1, username);
stem.setString(2, password);
//執行SQL語句
ResultSet rs=stem.executeQuery();
//如果有記錄對User對象進行賦值
if(rs.next())
{
u = new User();
u.setAge(rs.getInt("age"));
u.setDept(rs.getString("Dept"));
u.setEmail(rs.getString("email"));
u.setGender(rs.getString("gender"));
u.setId(rs.getInt("id"));
u.setPassword(rs.getString("password"));
u.setRegistTime(rs.getDate("registTime"));
u.setSalary(rs.getDouble("salary"));
u.setUsername(rs.getString("username"));
}
//關閉數據庫資源
JdbcConnection.close(conn, stem, rs);
return u;
}
}
User對象:
package com.itheima.login.entity;
public class User {
private int id;
private String username;
private String password;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", email=" + email + "]";
}
}
