-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'SQL_Injection_and_prevention' into development
SQL注入与防范相关介绍和实例
- Loading branch information
Showing
5 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
Navicat Premium Data Transfer | ||
Source Server : local | ||
Source Server Type : MySQL | ||
Source Server Version : 50719 | ||
Source Host : localhost:3306 | ||
Source Schema : cloud_study | ||
Target Server Type : MySQL | ||
Target Server Version : 50719 | ||
File Encoding : 65001 | ||
Date: 28/08/2017 22:15:23 | ||
*/ | ||
|
||
SET NAMES utf8mb4; | ||
SET FOREIGN_KEY_CHECKS = 0; | ||
|
||
-- ---------------------------- | ||
-- Table structure for user | ||
-- ---------------------------- | ||
DROP TABLE IF EXISTS `user`; | ||
CREATE TABLE `user` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT, | ||
`userName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, | ||
`sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, | ||
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, | ||
PRIMARY KEY (`id`) USING BTREE | ||
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; | ||
|
||
-- ---------------------------- | ||
-- Records of user | ||
-- ---------------------------- | ||
INSERT INTO `user` VALUES (1, 'ZhangSi', '0', '123456'); | ||
INSERT INTO `user` VALUES (2, 'LiSan', '0', '123456'); | ||
INSERT INTO `user` VALUES (3, 'GuoYi', '0', '123456'); | ||
INSERT INTO `user` VALUES (4, 'ZhangSi', '0', '123456'); | ||
INSERT INTO `user` VALUES (5, 'LiSan', '0', '123456'); | ||
INSERT INTO `user` VALUES (6, 'GuoYi', '0', '123456'); | ||
|
||
SET FOREIGN_KEY_CHECKS = 1; |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/micro/profession/jdbc/practice/Login.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.micro.profession.jdbc.practice; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class Login { | ||
|
||
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; | ||
static String DB_URL = "jdbc:mysql://localhost/cloud_study?useSSL=true"; | ||
static final String USER = "root"; | ||
static final String PASSWORD = "root"; | ||
|
||
public static User login(String userName, String password) | ||
throws ClassNotFoundException { | ||
Connection conn = null; | ||
PreparedStatement ptmt = null; | ||
ResultSet rs = null; | ||
User user = null; | ||
|
||
// 1. 装载驱动程序 | ||
Class.forName(JDBC_DRIVER); | ||
// 2. 建立数据库连接 | ||
try { | ||
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD); | ||
// 3. 执行SQL语句 | ||
ptmt = conn.prepareStatement("select * from user where userName = ? and password = ?"); | ||
ptmt.setString(1, userName); | ||
ptmt.setString(2, password); | ||
rs = ptmt.executeQuery(); | ||
// 4. 获取执行结果 | ||
while(rs.next()) { | ||
user = new User(); | ||
user.setUserName(rs.getString("userName")); | ||
user.setSex(rs.getBoolean("sex")); | ||
} | ||
} catch (SQLException e) { | ||
// 异常处理 | ||
e.printStackTrace(); | ||
} finally { | ||
//5. 清理环境 | ||
try { | ||
if(conn != null) | ||
conn.close(); | ||
if(ptmt != null) | ||
ptmt.close(); | ||
if(rs != null) | ||
rs.close(); | ||
} catch (SQLException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} | ||
return user; | ||
} | ||
|
||
public static void main(String[] args) throws ClassNotFoundException { | ||
// TODO Auto-generated method stub | ||
System.out.println(login("ZhangSi", "123456")!= null); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/micro/profession/jdbc/practice/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.micro.profession.jdbc.practice; | ||
|
||
public class User { | ||
|
||
// 用户id | ||
private int id; | ||
// 用户名 | ||
private String userName; | ||
// 性别 | ||
private Boolean sex; | ||
// 密码 | ||
private String password; | ||
|
||
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 Boolean getSex() { | ||
return sex; | ||
} | ||
|
||
public void setSex(Boolean sex) { | ||
this.sex = sex; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
} |