一、概念
JDBC:Java database connectivity (Java数据库连接技术)
作用:(1)建立与数据库之间的额访问链接
(2)将编写好的sql语句发送到数据库执行
(3)对数据库返回的结果进行处理
二、JDBC连接数据库五步
(1)加载驱动
(2)通过DiverManager连接的对象
(3)创建preparedstateMent对象用来执行sql语句
(4)处理sql语句执行结果
(5)释放资源
三、JDBC连接代码
ps.URL中的hospital是你所使用的数据库
package dao;
import java.sql.*;
public class PatientDao {
public static void main(String[] args) {
ResultSet resultSet = null;
Connection conn = null;
Statement statement = null;
try {
//1.加载驱动(固定写法)
Class.forName("com.mysql.cj.jdbc.Driver");
//2.用户信息、URL(固定写法)//**这个hospital是你所要使用的数据库
String url = "jdbc:mysql://localhost:3306/hospital?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
String username = "root";
String password = "123456";
//3.进行连接,数据库对象(固定写法)
conn = DriverManager.getConnection(url, username, password);
//4.执行sql的对象(固定写法)
statement = conn.createStatement();
//5.执行sql语句,返回结果集
String sql = "select p.* from patient p";
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("pname");
int age = resultSet.getInt("page");
String pcase = resultSet.getString("pcase");
System.out.println(id + "," + name + "," + age + "," + pcase);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
resultSet.close();
statement.close();
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
四、使用dao模式开发,四块功能封装BaseDao
package cn.kgc.dao;
import java.sql.*;
public class BaseDao {
protected Connection conn=null;
protected Statement stmt=null;
protected ResultSet rs=null;
protected PreparedStatement pstmt=null;
//1、连接数据库
public boolean getConnection(){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//2.用户信息、URL(固定写法)
String url = "jdbc:mysql://localhost:3306/hospital";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return true;
}
//2、关闭流
public void closeAll() {
try {
if (rs != null) {
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//3.执行增删改语句
public int executeUpdate(String sql, Object[] params){
int count = 0;
try {
if(getConnection()){
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1,params[i]);
}
count = pstmt.executeUpdate();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return count;
}
//4.执行查询语句
public ResultSet executeQuery(String sql,Object[] params){
try {
if(getConnection()){
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1,params[i]);
}
rs = pstmt.executeQuery();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return rs;
}
}
五、分层开发:
将项目的.Java文件分门别类的放在各个文件夹里面,每个文件夹放置相同功能类型的一组Java文件
src中{
pojo:实体类层 实体类 和数据库表是一 一对应的关系
dao: 数据访问层 放置的是所有操作数据库的接口和BaseDao
dao.imp: 放置的是所有接口的实现类
utils:工具类包 放置的是所有的工具类(与数据库无关)
test:测试类包
service:业务逻辑层
web:servlet 服务器小程序(获取前台请求调用后台方法)查询get请求,增删改post请求
:filters 过滤器
:listeners 整理器
}
lib:添加web框架包,在包中的WEB-INF,新建lib目录,里面放所需要的jar包。
数据源:(使用数据源去获取连接,连接JDBC(URL,))
频繁的链接数据库会导致数据库链接不稳定
ps:可以根据数据源,生成连接池,连接池里面放置已经创建好的链接对象
六、使用dbcp2数据源的步骤
1.引入3个jar包
commons-dbcp2-2.9.0-bin.zip
commons-logging-1.2.jar
commons-pool2-2.11.1-bin.zip
2.编写配置文件
3.编写工具类读取配置文件生成连接池
提供获取连接的方法