文章目录
*.properties
文件的对象,在properties文件中是以键值对形式保存的数据jdbc.properties
配置文件driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc
username=root
password=root
@Test
public void testPro(){
Properties properties=new Properties(); //创建Properties对象
//使用类加载器生成输入流(读取) 前提是该配置文件必须在src目录下
InputStream ips=TestProperties.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
properties.load(ips);
String s1=properties.getProperty("url"); //直接读取,以键值
System.out.println(s1);
} catch (IOException e) {
e.printStackTrace();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* 数据库封装类
* @author chenjiabing
*/
public class DBUtils {
private static String driver; //驱动
private static String url; //url
private static String username; //用户名
private static String password; //密码
//静态语句块,只在使用类加载的时候加载一次,因为其中的数据不用每次都加载,所以只需要加载一次
static{
Properties properties=new Properties(); //创建对象
//使用类加载器读取文件输入流
InputStream ips=DBUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
properties.load(ips);
//读取属性值
driver=properties.getProperty("driver");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取连接对象
* @param user 数据库用户名
* @param password 密码
* @param database : 数据库名称
*/
public static Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver"); // 注册驱动
// 链接数据库
Connection connection = DriverManager.getConnection(
url, username, password);
return connection;
}
/**
* 关闭数据库资源
* @param connection 连接对象
* @param statement Statement对象
* @param resultSet 结果集
*/
public static void close(Connection connection, Statement statement,
ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
//创建数据源对象
BasicDataSource dataSource=new BasicDataSource();
//设置连接信息 driver url username password
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/jdbc");
dataSource.setUsername("root");
dataSource.setPassword("root");
//设置连接池策略信息
dataSource.setInitialSize(3); //设置初始连接数量
dataSource.setMaxActive(5); //设置最大连接数量
//获取连接
Connection connection=dataSource.getConnection();
System.out.println(connection);
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc
username=root
password=root
initSize=3
maxSize=5
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
/**
* 数据库封装类 : 终极版
* @author chenjiabing
*/
public class DBUtils {
private static String driver; //驱动
private static String url; //url
private static String username; //用户名
private static String password; //密码
private static String maxSize; //最大连接数量
private static String initSize; //初始化连接数量
private static BasicDataSource dataSource; //数据源
//静态语句块,只在使用类加载的时候加载一次,因为其中的数据是不会经常改变的,所以只需要加载一次
static{
Properties properties=new Properties(); //创建对象
//使用类加载器读取文件输入流
InputStream ips=DBUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
properties.load(ips);
//读取属性值
driver=properties.getProperty("driver");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
initSize=properties.getProperty("initSize");
maxSize=properties.getProperty("maxSize");
dataSource=new BasicDataSource(); //获取数据源
//设置数据源的属性-----数据库的配置信息
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setInitialSize(Integer.parseInt(initSize));
dataSource.setMaxActive(Integer.parseInt(maxSize));
} catch (IOException e) {
System.out.println("配置文件jdbc.properties读取失败!!!");
e.printStackTrace();
}
}
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception {
Connection connection=dataSource.getConnection();
return connection;
}
/**
* 关闭数据库资源
* @param connection 连接对象
* @param statement Statement对象
* @param resultSet 结果集
*/
public static void close(Connection connection, Statement statement,
ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.junit.Test;
import com.jsnu.db.DBUtils;
public class TestDBUtils {
@Test
public void test() {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = DBUtils.getConnection(); //获取连接
statement = connection.createStatement(); //创建Statement语句对象
String select_sql = "select * from t";
resultSet = statement.executeQuery(select_sql); //执行查询方法
while (resultSet.next()) {
int id = resultSet.getInt("id"); //获取属性
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println(id + "---" + age + " ----" + name);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtils.close(connection, statement, resultSet); //关闭资源
}
}
}
public class TestDButils2 extends Thread {
@Override
public void run() {
try {
Connection connection=DBUtils.getConnection();
System.out.println(connection); //获取连接
System.out.println(this.getName()+ ": 正在运行");
Thread.sleep(5000); //睡眠5s
connection.close(); //关闭连接,相当于释放连接,归还到连接池中
System.out.println(this.getName()+":连接已经归还");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
for(int i=0;i<=6;i++){
TestDButils2 testDButils2=new TestDButils2();
testDButils2.start(); //线程启动
}
}
select count(*) from user where username='xds' and password='' or '1'='1'
那么会直接登录成功,无论用户名和密码是多少。这种是使用Statement才会生效,因为其中的sql是拼接的。我们只需要输入密码为 ' or '1'=1
即可sql注入