package com.xpc.test;
import com.mysql.cj.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Test1 {
public static void main(String[] args) throws SQLException {
// 加载驱动
Driver driver = new com.mysql.cj.jdbc.Driver();
// 注册到驱动
DriverManager.registerDriver(driver);
// 获得连接
/**
* url 同一资源定位符
* 1. 协议 jdbc:mysql
* 2. IP
* 3. 端口号
* 4. 数据库名字
* 5. 参数
* name
* password
*/
String url = "jdbc:mysql://127.0.0.1:3306/mytestdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
Connection connection = DriverManager.getConnection(url, "root", "123456");
// 获得语句对象
Statement statement = connection.createStatement();
// 执行sql语句返回结果
String sql = "INSERT INTO account VALUES (NULL,'jack',1000)";
int i = statement.executeUpdate(sql);
System.out.println("影响行数"+i);
// 释放资源
statement.close();
connection.close();
}
}
在Driver中有个静态代码块,会自动new实例并注册,所以可以直接用反射加载字节码文件到内存中就可以完成驱动加载和注册
package com.xpc.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Test1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// 加载驱动 注册到驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获得连接
/**
* url 同一资源定位符
* 1. 协议 jdbc:mysql
* 2. IP
* 3. 端口号
* 4. 数据库名字
* 5. 参数
* name
* password
*/
String url = "jdbc:mysql://127.0.0.1:3306/mytestdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
Connection connection = DriverManager.getConnection(url, "root", "123456");
// 获得语句对象
Statement statement = connection.createStatement();
// 执行sql语句返回结果
String sql = "INSERT INTO account VALUES (NULL,'jack',1000)";
int i = statement.executeUpdate(sql);
System.out.println("影响行数"+i);
// 释放资源
statement.close();
connection.close();
}
}
package com.xpc.test;
import java.sql.*;
public class Test1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// 加载驱动 注册到驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获得连接
/**
* url 同一资源定位符
* 1. 协议 jdbc:mysql
* 2. IP
* 3. 端口号
* 4. 数据库名字
* 5. 参数
* name
* password
*/
String url = "jdbc:mysql://127.0.0.1:3306/mytestdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
Connection connection = DriverManager.getConnection(url, "root", "123456");
// 获得语句对象
Statement statement = connection.createStatement();
// 执行sql语句返回结果
String sql = "select * from account;";
ResultSet result = statement.executeQuery(sql);
System.out.println("影响行数"+result);
boolean next = result.next();
System.out.println(next);
while (result.next()){
int id = result.getInt("id");
System.out.println(id);
String uname = result.getString("uname");
System.out.println(uname);
}
// 释放资源
result.close();
statement.close();
connection.close();
}
}
select * from account where username = 'xxx' and password = 'xxx'
-- 注入攻击如下 密码后面拼接参数or'a'='a'
select * from account where username = 'xxx' and password = 'xxx'or'a'='a'
String sql = "select * from account where username = ? and password = ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,"XXX");
ps.setString(2,"XXX");
ResultSet resultSet = ps.executeQuery();
package com.xpc.test;
import com.xpc.untity.Account;
import java.sql.*;
import java.util.ArrayList;
public class test2 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
ArrayList<Account> accounts = new ArrayList<>();
// 加载驱动 注册到驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获得连接
String url = "jdbc:mysql://127.0.0.1:3306/mytestdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
Connection connection = DriverManager.getConnection(url, "root", "123456");
// 获得语句对象
Statement statement = connection.createStatement();
String sql = "insert into account values (default,?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
int row = 0;
for (int i = 0; i < 1000; i++) {
ps.setString(1,"name");
ps.setString(2,"password");
// int i1 = ps.executeUpdate();
// row += i1;
// 将SQL语句放入批次中
ps.addBatch();
// 如果数据过多,可以每隔100清理一次
// if(i%100==0){
// ps.executeBatch();
// ps.clearBatch();
// }
}
int[] ints = ps.executeBatch();
// 释放资源
statement.close();
connection.close();
}
}
package com.xpc.test;
import java.sql.*;
public class test3 {
public static void main(String[] args) {
// 加载驱动 注册到驱动
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// 获得连接
String url = "jdbc:mysql://127.0.0.1:3306/mytestdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
connection = DriverManager.getConnection(url, "root", "123456");
// 不主动提交事务
connection.setAutoCommit(false);
// 获得语句对象
statement = connection.createStatement();
String sql = "update account set money = money - ? where id = ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setDouble(1, 200);
ps.setInt(2, 1);
// int i=1/0;
// 不提交事务了
ps.executeUpdate();
ps.setDouble(1, -200);
ps.setInt(2, 2);
// 不提交事务了
ps.executeUpdate();
} catch (Exception e) {
System.out.println(e);
if(null!=connection){
try {
connection.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
} finally {
if(null!=connection){
try {
connection.commit();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
// 释放资源
try {
statement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
package com.xpc.com.xpc.poll;
import com.mysql.cj.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
public class MyContectionPoll {
private static String driver = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1:3306/mytestdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
private static String user = "root";
private static String password = "123456";
private static LinkedList<Connection> poll;
private static int inntSize = 5;
private static int maxSize = 10;
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
// 初始化poll
poll = new LinkedList<Connection>();
for (int i = 0; i < inntSize; i++) {
Connection connection = initConnection();
if(null != connection){
poll.add(connection);
}
}
}
private static Connection initConnection(){
try {
return DriverManager.getConnection(url,user,password);
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
// 向外界提供方法
public Connection getConnection(){
Connection connection = null;
if(poll.size()>0){
connection = poll.remove();
}else {
connection = initConnection();
}
return connection;
}
// 归还方法
public void returnConnection(Connection connection){
if(null!=connection){
try {
if(!connection.isClosed()){
try {
// 调整事务状态
connection.setAutoCommit(true);
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(poll.size()<maxSize){
poll.addLast(connection);
}else {
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
log4j.rootLogger=error,logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=e:/xpc.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %F %p %m%n