导入maven依赖
<!-- mysql-Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
示例查询代码
package xyz.deepmez.mezbatis.v0;
import java.sql.*;
public class Main {
/**
* 链接数据库相关参数
*/
static final String DB_URL = "jdbc:mysql://****:3306/lwj?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false";
static final String user = "***";
static final String pass = "***";
public static void main(String[] args) {
Connection conn = null;
try {
// 注册驱动,执行静态代码
Class.forName("com.mysql.jdbc.Driver");
System.out.println("-----连接数据库----");
// 连接,之后会交给连接池去处理
conn = DriverManager.getConnection(DB_URL, user, pass);
// 这个连接去做相应的操作
String sql1 = "select * from student where id=10001";
Statement statement = conn.createStatement();
ResultSet rs1 = statement.executeQuery(sql1);
//数据输出
while (rs1.next()) {
System.out.printf("id=%d,age=%d,name=%s%n", rs1.getInt("id"), rs1.getInt("age"), rs1.getString("name"));
}
rs1.close();
statement.close();
//完成并关闭数据库
conn.close();
System.out.println("查询结束关闭数据库");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
DriverManager.registerDriver(new Driver());
,所以只要类加载了,这段逻辑就会走,jdbc的驱动就会实例化一个对象注册到DriverManager;用Connection做事务控制;
public class Main {
public static void main(String[] args) {
Connection conn = null;
try {
// 注册驱动,执行静态代码
Class.forName("com.mysql.jdbc.Driver");
System.out.println("-----连接数据库----");
// 连接,之后会交给连接池去处理
conn = DriverManager.getConnection(DB_URL, user, pass);
// 这个连接去做相应的操作
String sql1 = "update student set name='李维佳' where id = 10004";
String sql2 = "update student set name='李维佳' where id = 10006";
Statement statement = conn.createStatement();
conn.setAutoCommit(false);
try {
int count = statement.executeUpdate(sql1);
System.out.println("成功条数:" + count);
count = statement.executeUpdate(sql2);
System.out.println("成功条数:" + count);
conn.commit();
} catch (Exception e) {
conn.rollback();
e.printStackTrace();
}
statement.close();
//完成并关闭数据库
conn.close();
System.out.println("查询结束关闭数据库");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
事务的控制在connection层面;
针对查询操作,Statement的执行结果存放的对象,在下一次SQL执行后覆盖关闭
public class Main {
public static void main(String[] args) {
Connection conn = null;
try {
// 注册驱动,执行静态代码
Class.forName("com.mysql.jdbc.Driver");
System.out.println("-----连接数据库----");
// 连接,之后会交给连接池去处理
conn = DriverManager.getConnection(DB_URL, user, pass);
// 这个连接去做相应的操作
String sql = "select * from student where id = ?";
// 获得执行对象
PreparedStatement statement = conn.prepareStatement(sql);
statement.setInt(1, 10006);
ResultSet resultSet = statement.executeQuery();
//数据输出
while (resultSet.next()) {
System.out.printf("id=%d,age=%d,name=%s%n", resultSet.getInt("id"), resultSet.getInt("age"), resultSet.getString("name"));
}
statement.close();
conn.close();
System.out.println("查询结束关闭数据库");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
防止SQL注入,提前把SQL准备好,只填入变量,如果变量里面有可能改变的SQL的字符,会转义;
mysql支持预编译;
-- 定义一个预编译语句
prepare statement_1 from 'select * from user where id=?';
-- 设置变量
set @id=1;
-- 执行
execute statement_1 using @id;
-- 释放
deallocate prepare statement_1;
实际上在conn.prepareStatement(sql)
就和数据库交互了在预编译SQL了;
SUN官方定义好了数据源连接池接口DataSource
,各业务方实现即可;
使用方式
# -数据库连接相关-
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://000.000.000.00:3306/lwj?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&useServerPrepStmts=true
username=name
password=123456
# -数据源连接池相关-
# 初始化连接数量
initialSize=1
# 最大连接池个数
maxActive=2
# 最大等待时间(单位毫秒)
maxWait=3000
public class Main {
public static void main(String[] args) throws Exception {
// 从数据源连接池获得连接
Properties properties = new Properties();
properties.load(new FileInputStream("src/main/resources/druid.properties"));
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
// 多线程获得三个连接
for (int i = 0; i < 3; i++) {
int finalI = i;
new Thread(() -> {
try {
System.out.println("请求第" + finalI + "个连接");
Connection connection = dataSource.getConnection();
if (Objects.nonNull(connection)) {
System.out.println("获得第" + finalI + "个连接成功");
}
} catch (SQLException e) {
e.printStackTrace();
}
}).start();
}
}
}
注意:从连接池获得的连接,在使用后需要关闭close,关闭是放回连接池,不是真的断开TCP连接
;
在应用服务器中,对于数据库的访问往往是并发的,这时候需要系统做并发的兼容,考虑从查询粒度依次增加:
所以可行的方案是单独开一个Connection处理当前的SQL任务