------------------------------------------ Java数据库连接技术(jdbc)----------------------------------
jdbc访问数据库步骤:
1.加载一个Driver驱动;
2.创建数据库连接(connection);
3.创建SQL命令发送器(Statement);
4.通过Statement发送SQL命令并得到结果;
5.处理结果;
6.关闭数据库资源(resultset、statement、connection);
----------------------------------------------------------------------
注意:在项目中添加jar包:mysql-connector-java-8.0.11.jar
案例1:
public class TestInsert {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url="jdbc:mysql://127.0.0.1:3306/junxie?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
String user="root";
String password="root";
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
Connection connection = DriverManager.getConnection(url, user, password);
//System.out.println(connection);
//3.创建发射器
Statement statement = connection.createStatement();
//4.发送sql并返回结果
String sql="insert into user values(4,'薛六','123456');";
int n = statement.executeUpdate(sql);
//5.处理结果
if (n > 0) {
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
//6.关闭资源
statement.close();
connection.close();
}
}
----------------------------------------------------------------------
案例:(上段代码异常捕获)
public class TestInsert {
public static void main(String[] args) {
String url="jdbc:mysql://127.0.0.1:3306/junxie?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
String user="root";
String password="root";
Connection connection=null;
Statement statement=null;
int n=0;
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
connection = DriverManager.getConnection(url, user, password);
//System.out.println(connection);
//3.创建发射器
statement = connection.createStatement();
//4.发送sql并返回结果
String sql="insert into user values(5,'马七','123456');";
n = statement.executeUpdate(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//6.关闭资源
try {
if (statement !=null){
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//5.处理结果
if (n > 0) {
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
}
}
----------------------------------------------------------------------
prepareStatement()和createStatement()的区别:
1.prepareStatement()安全,避免了SQL注入的风险;
2.prepareStatement()可读性强,避免了拼接字符串的繁琐;
3.prepareStatement()速度更快;
-------------------------------------------------------事务的使用-------------------------------------------
1.在JDBC中,事务操作缺省是自动提交。
2.一条对数据库的DML(insert、update、delete)代表一项事务操作,操作成功后,
系统将自动调用commit()提交,否则自动调用rollback()回滚
3.在JDBC中,事务操作方法都位于接口java.sql.Connection中可以通过调用setAutoCommit(false)来禁止自动提交。
之后就可以把多个数据库操作的表达式作为一个事务,在操作完成后调用commit()来进行整体提交,
倘若其中一个表达式操作失败,都不会执行到commit(),并且将产生响应的异常;
此时就可以在异常捕获时调用rollback()进行回滚,回复至数据初始状态
4.事务开始的边界则不是那么明显了,它会开始于组成当前事务的所有statement中的第一个被执行的时候。
5.事务结束的边界是commit或者rollback方法的调用
-------------------------------------------------------连接池-------------------------------------------
建立数据库连接的两种方式:
1.传统连接方式:
首先调用Class.forName()方法加载数据库驱动,然后调用DriverManager.getConnection()方法建立连接.
2连接池技术:
连接池解决方案是在应用程序启动时就预先建立多个数据库连接对象,然后将连接对象保存到连接池中。
当客户请求到来时,从池中取出一个连接对象为客户服务。
当请求完成时,客户程序调用close()方法,将连接对象放回池中.
对于多于连接池中连接数的请求,排队等待。
应用程序还可根据连接池中连接的使用率,动态增加或减少池中的连接数。
----------------------------------------------------------------------
简单连接池实现:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
/*
* 连接池
* */
public class ConnectionPool {
//定义一个集合,存储连接。连接池操作提供连接,收回连接,用链表;
private List<Connection> list=new LinkedList<Connection>();
private int size;//连接数量
public ConnectionPool(){
this.size=10;
}
public ConnectionPool(int size){
//指定数量
this.size=size;
init();
}
private void init() {
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://127.0.0.1:3306/junxie?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
String user="root";
String password="root";
//1.加载驱动
try {
Class.forName(driver);
//2.创建连接
for (int i = 0; i < size; i++) {
Connection connection = DriverManager.getConnection(url, user, password);
list.add(connection);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public synchronized Connection getConnction(){
while (list.isEmpty()){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Connection connection = list.remove(0);
this.notifyAll();
return connection;
}
public synchronized void returnConnection(Connection conn){
while (list.size()==size){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.add(conn);
this.notifyAll();
}
}