JDBC

JDBC 理论概述

JDBC概念:JDBC是使用java语言操作关系型数据库的一套API接口
JDBC驱动:各个关系型数据库厂商根据JDBC API结构做的实现类

JDBC 连接过程

注册驱动

Class.forName("com.mysql.jdbc.Driver");

获取连接

String url = "jdbc:mysql://127.0.0.1:3306/db1?useSSL = false";    
String username = "root";
String password = "1234";
Connection conn = DriverManager.getConnection(url,username,password);
//mysql数据库协议,不同数据库协议不同
//useSSL:设置安全连接的参数,false则关闭安全连接提示
//useServerPrepStmts=true:开启预编译功能,默认关闭

获取对象

普通获取对象

Statement stmt = conn.createStatement();
conn.setAutoCommit(false);//关闭自动事务并开启手动事务
conn.rollback();          //回滚事务
conn.commit();            //提交事务

预编译获取对象

String sql = "select * from tb_user where username = ? and password = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,name);    //设置?位置的值,第一个?值为name变量的值
pstmt.setString(2,password);//设置?位置的值,第二个?值为password变量的值

执行操作

普通执行

String sql = "update account set money = 2000 where id = 1";
int count =  stmt.executeUpdate(sql);
//执行DML、DDL语句,执行DML语句时返回值为语句影响行数
//执行DDL语句时,返回值为0代表执行成功
ResultSet res = stmt.executeQuery(sql);
//执行DQL语句,返回值为结果集对象 
//res.next()表示结果表中的行,读取一行自动切换下一行
//使用getInt(),getString()等get加数据类型命名的方法获取next()行中的列
//从1开始对应第一列数据,以此类推,也可以直接写列的名称

预编译执行

int count =  stmt.executeUpdate();
//执行DML、DDL语句,执行DML语句时返回值为语句影响行数
//执行DDL语句时,返回值为0代表执行成功
ResultSet res = stmt.executeQuery();
//执行DQL语句,返回值为结果集对象 

处理结果

根据业务需求处理

释放连接

普通释放连接

res .close();
stmt.close();//先生成的对象后释放
conn.close();//所有的对象均需释放

预编译释放连接

res  .close();//释放结果对象
pstmt.close();//释放操作对象
conn .close();//释放连接对象

JDBC 的连接池

配置文件

//Druid数据库连接池配置文件
driverClassName=com.mysql.jdbc.Driver  
//数据库连接池名称
url=jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true
//数据库地址
username=root//用户名
password=1234//密码
initialSize=5//数据库连接池初始连接对象线程数
maxActive=10 //数据库连接池初最大接对象线程数
maxWait=3000 //数据库连接最大等待时间

具体使用

//Druid数据库连接池具体使用
public class DruidDemo {
  public static void main(String[] args) {
    Properties prop = new Properties();  
    //创建配置文件对象
    prop.load(new FileInputStream("src/druid.properties"));
    //加在配置文件(地址为配置文件地址)
    DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    //获取数据库连接池对象
    Connection conn = dataSource.getConnection();
    //获取数据库连接对象
  }
}
//商品品牌数据的查询操作
public class GoodsSelect {
  public static void main(String[] args) {
    Properties prop = new Properties();  
    //创建配置文件对象
    prop.load(new FileInputStream("src/druid.properties"));
    //加在配置文件(地址为配置文件地址)
    DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    //获取数据库连接池对象
    Connection conn = dataSource.getConnection();
    //获取数据库连接对象
    
    String sql = "select * from goods";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    
    ResultSet res = stmt.executeQuery();
    
    while(res.next()) {
      int id = res.getInt("id");
      res.getString()
    }
    
    res  .close();//释放结果对象
    pstmt.close();//释放操作对象
    conn .close();//释放连接对象
  }
}
//商品品牌数据的增加操作
public class GoodsSelect {
  String brandName = "香飘飘";
  String companyName = "香飘飘公司"
  int ordered = 1;
  String description = "绕地球一圈";
  int status = 1;
  public static void main(String[] args) {
    Properties prop = new Properties();  
    //创建配置文件对象
    prop.load(new FileInputStream("src/druid.properties"));
    //加在配置文件(地址为配置文件地址)
    DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    //获取数据库连接池对象
    Connection conn = dataSource.getConnection();
    //获取数据库连接对象
    
    String sql = "insert into 
      						goods(brand_name,company_name,ordered,description,status)
                  values(?,?,?,?,?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
   	pstmt.setString(1,brandName);
    pstmt.setString(2,companyNname);
    pstmt.setInt(3,ordered);
    pstmt.setString(4,description);
    pstmt.setInt(5,status);
    
    int count = stmt.executeUpdate();
    
		System.out.println(count > 0);
    
    pstmt.close();//释放操作对象
    conn .close();//释放连接对象
  }
}
posted @ 2022-09-08 01:05  (应鑫)  阅读(41)  评论(0)    收藏  举报