JDBC基础

以前操做Mysql数据库都是使用客户端工具登陆数据库,而后再客户端编写SQL语句,发送到数据库服务器执行,例如Mysql数据库带的mysql客户端工具,能够在命令行执行mysql -uUSERNAME -pPASSWORD来登陆本机数据库java

那么在Java程序代码中操做数据库,可使用JDBC技术。mysql

一,什么是JDBC

JDBC(Java DataBase Connectivity,java数据库链接)是一种用于执行SQL语句的Java API,能够为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。直白讲就是使用Java代码发送SQL语句的技术。sql

使用JDBC能够链接不一样的数据库,只须要提供相应的驱动程序,驱动程序由数据库厂商提供,就是一堆实现了JDBC接口的类。数据库

这样作的好处:api

  • 开发者不须要关心数据库的驱动内部的原理,只须要维护Java部分的接口
  • 数据库厂商若是修改了数据库的底层原理,也要提供对应的数据库驱动,但不影响Java程序部分

二,使用JDBC技术连接Mysql数据库服务器

链接数据库咱们须要知道数据库的地址,端口号,正确的用户名和对应的密码服务器

JDBC 核心API:

JDBC的核心接口和类位于Java标准库的java.sqljavax.sql中,经常使用的主要位于java.sql中工具

核心类或者接口介绍:url

  • Driver接口:表示Java驱动程序接口,数据库的驱动须要实现此接口
    • connect(url,properties)方法,能够链接url到指定的数据库
  • Connection接口:表示与数据库的链接对象
    • createStatement():建立一个Statement对象
    • PreparedStatement(String sql):建立一个预编译的Statement对象
    • CallableStatement(String sql):建立CallableStatement对象
  • DriverManager类:驱动管理类,用于管理全部的注册的驱动程序
    • registerDriver(Driver driver):注册驱动程序
    • getConnection(url,user,password):返回一个对应的Connection对象
  • Statement接口:用于执行静态 SQL 语句并返回它所生成结果的对象
    • executeUpdate(String sql):用于执行静态的更新SQL语句
    • executeQuery(String sql):用于执行静态的查询SQL语句
  • PreparedStatement接口:Statement的子接口,表示预编译的 SQL 语句的对象
    • executeUpdate():用于执行预编译的更新SQL语句
    • executeQuery(): 用于执行预编译的查询SQL语句
  • CallableStatement接口:Statement和PreparedStatement的子接口,调用储存过程的对象

JDBC 驱动的注册

在注册驱动以前,须要下载mysql数据库的驱动程序jar包添加到项目中命令行

第一种方式
//1.建立数据库驱动对象
Driver driver = new com.mysql.jdbc.Driver();

Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "root");

//2. 链接数据库
Connection connection = driver.connect(url, info);
第二种方式(使用DriverManager(驱动管理)类来获取链接)
//1.建立一个驱动对象
Driver driver = new com.mysql.jdbc.Driver();//这句代码中已经注册了驱动
// Driver driver2 = new com.orace.jdbc.Driver();

//2.注册驱动程序(能够注册多个)
DriverManager.registerDriver(driver);
// DriverManager.registerDriver(driver2);

//3. 获取链接对象
Connection connection = DriverManager.getConnection(url, user, password);

上面使用DriverManager来注册的方式实际会注册两次驱动3d

第三种方式
Class.forName("com.mysql.jdbc.Driver");

Connection connection = DriverManager.getConnection(url,user,password);

System.out.println(connection);

推荐使用第三种方式

下面用一个实例展现JDBC技术发送SQL语句的通常步骤

事先已经在本地mysql服务器上建立了一个mydb的数据库

/*
 * 执行DML(数据库操纵语言)(insert update delete)
 * */
public class Demo2 {
    private String url="jdbc:mysql://localhost:3306/mydb";
    private String user="root";
    private String password="root";
    
    
    
    @Test
    public void testInsert(){
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url,user,password);
            statement = connection.createStatement();
            
            String sql = "insert into student(name,gender) values('云溪','女')";
            
            int count = statement.executeUpdate(sql);
            
            System.out.println("插入影响了"+count+"行");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
        
        
    }
能够看出JDBC操做数据库的通常步骤:
  1. 注册驱动(只作一次)
  2. 创建链接(Connection)
  3. 建立执行SQL的语句(Statement)
  4. 执行语句
  5. 处理执行结果(ResultSet)
  6. 释放资源
相关文章
相关标签/搜索