First acquainted with JDBC programming in Java

Java JDBC programming

  • JDBC , namely Java Database Connectivity, java database connection. It is a Java API for executing SQL statements. It is a database connection specification in Java. This API is composed of some classes and interfaces in the java.sql. and javax.sql. packages. It provides a standard API for Java developers to manipulate databases, and can provide unified access to a variety of relational databases.

1. JDBC working principle

  • JDBC access database hierarchy:
    Insert picture description here
  • JDBC advantage
  1. Java language access to database operations is completely oriented to abstract interface programming;
  2. The development of database applications is not limited to the API of a specific database vendor;
  3. The portability of the program is greatly enhanced;

2. Use of JDBC

  • JDBC use steps
  1. Create a database connection Connection;
  2. Create Operation Command Statement;
  3. Use operation commands to execute SQL;
  4. Processing result set ResultSet;
  5. Release resources;

2.1 Establish a database connection

  • The URL parameter format of MySQL data connection is as follows:
    jdbc:mysql://server address:port/database name? parameter name=parameter value
/* 加载JDBC驱动程序:反射,这样调用初始化com.mysql.jdbc.Driver类,即将该类加载到JVM方法
区,并执行该类的静态方法块、静态属性。*/
Class.forName("com.mysql.jdbc.Driver");
// 创建数据库连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/students?user=root&password=zg0718&useSSL=false&useUnicode=true&characterEncoding=UTF-8");

2.2 Create Operation Command (Statement)

Statement statement = connection.createStatement();

2.3 execute SQL statement

  • Here executeQuery is the query method, which is the same as the Mysql statement;
ResultSet resultSet= statement.executeQuery("select id, name, sex, age from student");

2.4 Processing result set

while(resultSet.next()){
    
    //遍历每一天记录
            String id = resultSet.getString("id");
            String name = resultSet.getString("name");
            String sex = resultSet.getNString("sex");
            int age = resultSet.getInt("age");
            System.out.printf("id = %s,name = %s,sex = %s,age = %s%n",id,name,sex,age);
        }

2.5 Release resources (close the result set, command, connection)

//逆序关闭
if(resultSet != null){
    
    
            try {
    
    
                resultSet.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
        }
        if (statement != null){
    
    
            try {
    
    
                statement.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
        }
        if (connection != null){
    
    
            try {
    
    
                connection.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
        }

3. All the above operation codes

package test.jdbc;

import java.sql.*;

public class Main {
    
    
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    
    
        //1.加载jdbc驱动程序:反射,这样调用初始化com.mysql.jdbc.Driver类,即将该类加载到JVM方法区,并执行该类的静态方法块、静态属性。
        Class.forName("com.mysql.jdbc.Driver");
        //2.创建数据库连接
        Connection connection =
                DriverManager.getConnection("jdbc:mysql://localhost:3306/students?user=root&password=zg0718&useSSL=false&useUnicode=true&characterEncoding=UTF-8");

        Statement statement = connection.createStatement();
        //查询
        ResultSet resultSet= statement.executeQuery("select id, name, sex, age from student");

        //3.处理结果集
        while(resultSet.next()){
    
    
            String id = resultSet.getString("id");
            String name = resultSet.getString("name");
            String sex = resultSet.getNString("sex");
            int age = resultSet.getInt("age");
            System.out.printf("id = %s,name = %s,sex = %s,age = %s%n",id,name,sex,age);
        }

        //关闭结果集
        if(resultSet != null){
    
    
            try {
    
    
                resultSet.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
        }
        if (statement != null){
    
    
            try {
    
    
                statement.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
        }
        if (connection != null){
    
    
            try {
    
    
                connection.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
        }
    }
  • Here is the output of the queried student table (the student table here was created in Mysql before);
    Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45665172/article/details/109784974