JDBC URL: jdbc:mysql://mysql.sqlpub.com:3306/huangjin
Username: XXXXXX
Password: fc12f7a5215e8e0a
请输入要查找的id:1 or 1=1
1, ml5 michael@email.com gmail 22
2, bb44 bob@email.com facebook 1
3, je22 jane@email.com github 49
4, ae5 alice@email.com facebook 64
5, ls5 lisa@email.com twitter 10
6, ay0 anthony@email.com facebook 1
7, je1 joe@email.com github 99
8, mk57 mark@email.com github 15
9, tt34 tim@email.com github 90
10, cc100 colton@email.com github 10
JDBC配置
config.properties
db.url=jdbc:mysql://mysql.sqlpub.com:3306/huangjin
db.username=XXXXXX
db.password=fc12f7a5215e8e0a
java
package com.abc.project3;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
public class D {
public static void main(String[] args) throws IOException, SQLException {
String dbUrl = null;
String dbUsername = null;
String dbPassword = null;
Properties properties = new Properties();
FileInputStream fis = null;
String fName = "config.properties";
File file = new File(fName);
if (!file.isFile()) {
System.err.println(fName + " 文件不存在");
System.exit(0);
}
// 读取配置文件
fis = new FileInputStream(fName);
properties.load(fis);
// 获取 JDBC 连接信息
dbUrl = properties.getProperty("db.url");
dbUsername = properties.getProperty("db.username");
dbPassword = properties.getProperty("db.password");
if(fis!=null)
fis.close();
// 打印 JDBC 连接信息
System.out.println("JDBC URL: " + dbUrl);
System.out.println("Username: " + dbUsername);
System.out.println("Password: " + dbPassword);
Connection con = DriverManager.getConnection(dbUrl,dbUsername,dbPassword);
//4.获取执行者对象
Statement stat = con.createStatement();
String sql ="SELECT * FROM user WHERE id = ";
ResultSet resultSet = null;
Scanner sc = new Scanner(System.in);
System.out.print("请输入要查找的id:");
String str =sc.nextLine(); // 注入攻击 1 or 1=1
sc.close();
resultSet = stat.executeQuery(sql + str);
while(resultSet.next()){
String res =resultSet.getInt("ID")+", ";
res += resultSet.getString("username")+" ";
res += resultSet.getString("email")+" ";
res += resultSet.getString("authType")+" ";
res += resultSet.getInt("reputation");
System.out.println(res);
}
if (resultSet != null)
resultSet.close();
stat.close();
con.close();
}
}