【JDBC】Java程序的数据库初体验

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

【JDBC】Java程序的数据库初体验

User_340   2020-01-11 我要评论

JDBC是什么

JDBC是一种能够用来执行SQL语句的Java API【接口】。

它是Java提供的一种规范,让各大数据库厂商遵循此规范完成自己的数据库连接驱动【实现接口】。

JDBC的入门程序(这里我们以操作MySQL数据库为例)

步骤:

  • 导入相关数据库驱动包

  • 加载驱动类

  • 创建数据库连接对象

  • 创建可执行sql语句的对象

  • 执行sql语句,得到返回结果

  • 关闭连接

首先必须导入MySQL的驱动包(如果没有导入驱动包,加载驱动类时会抛出ClassNotFoundException)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Jdbc {

  public static void main(String[] args) {

    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    try {
      // 加载数据库驱动类
      Class.forName("com.mysql.cj.jdbc.Driver");
      // 通过数据库地址、数据库用户名、数据库密码三个参数创建数据库连接
      connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8", 
          "root", "root");
      // 创建可执行SQL语句的statement对象
      statement = connection.createStatement();
      // 编写SQL语句
      String sql = "select * from user";
      // 执行SQL语句,得到查询结果集对象
      resultSet = statement.executeQuery(sql);
      // 迭代遍历结果集对象,获取数据
      while (resultSet.next()) {
        System.out.print(resultSet.getInt(1)+":");
        System.out.println(resultSet.getString(2));
      }
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      // 关闭资源,关闭顺序根据调用自下而上
      if (resultSet != null) {
        try {
          resultSet.close();
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      if (statement != null) {
        try {
          statement.close();
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      if (connection != null) {
        try {
          connection.close();
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
}

上面我们已经简单使用了jdbc完成了对数据库的查询,下面我们介绍JDBC中的几个核心对象。

核心对象

  • Connection

Java程序与数据库之间的连接接口。

常用方法:

// 创建可执行sql语句的statement对象
Statement createStatement() throws SQLException;
// 创建可执行预编译sql语句的prepareStatement对象
PreparedStatement prepareStatement(String sql) throws SQLException;
// 设置事务是否自动提交
void setAutoCommit(boolean autoCommit) throws SQLException;
// 提交事务
void commit() throws SQLException;
// 回滚事务
void rollback() throws SQLException;
// 关闭连接
void close() throws SQLException;
  • Statement

可执行sql语句的对象,能够对数据库进行增删改查操作。

常用方法:

// 向数据库发送查询sql语句,返回查询结果集对象
ResultSet executeQuery(String sql) throws SQLException;
// 向数据库发送更新(增删改)sql语句,返回数据库受影响行数
int executeUpdate(String sql) throws SQLException;
// 关闭资源
void close() throws SQLException;
  • PreparedStatement

PreparedStatement对象在我们的入门程序中并没有出现,但也是JDBC中的常用对象。它继承自Statement对象,比Statement对象更强大。

Statement对象编译sql语句时,如果sql语句有参数变量,就需要进行拼接,如果参数变量较多,就会使sql变得非常复杂。和Statement对象相同,PreparedStatement也是由Connection对象创建的,不同的是PreparedStatement对象的创建需要一条sql语句作为参数,sql语句中的参数变量用占位符'?'表示,它能够调用set方法动态设置sql语句中的参数变量,而不是Statement那样使用字符串拼接。

常用方法:

// 根据sql语句中的变量的数据类型设置变量
// 该方法有两个参数,第一个参数是指定sql语句中变量的位置,第二个是设置的值
void setString(int parameterIndex, String x) throws SQLException;
// 查询
ResultSet executeQuery() throws SQLException;
// 更新(增删改)
int executeUpdate() throws SQLException;
  • ResultSet

执行查询语句返回的数据结果集。当执行了查询sql语句时,会返回一个结果集对象。可通过调用next()方法迭代遍历结果集获取我们所需的数据。

常用方法:

// 迭代结果集,查看是否有下一条数据
boolean next() throws SQLException;
// 访问器,根据数据类型调用指定访问器。
// 每种数据类型的访问器都有数字、字符串两种参数,
// 数字参数表示第几列,字符串参数表示列名。
String getString(int columnIndex) throws SQLException;
String getString(String columnLabel) throws SQLException;
// 关闭结果集对象资源
void close() throws SQLException;

——END——

如果文章有错的地方欢迎指正,大家互相交流。欢迎关注公众号developerLeo!会更新一些个人学习的文章。

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们