Class. ForName (” dm.jdbc.driver.dmdriver “); Class. ForName (” dm.jdbc.driver.dmDriver “); In order to access the data normally, my test code is as follows :(I use dameng database test)

package org.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/ * * *@Description TODO
 * @Author Harry
 * @Date2020/12/25 "* * /
public class TestDmJdbc {

    public static void main(String[] args) {
        Connection con = null;// Create a database connection
        PreparedStatement pre = null;// Create a precompiled Statement object
        ResultSet result = null;// Create a result set object
        try
        {
// Class.forName("dm.jdbc.driver.DmDriver"); // Load the driver
            System.out.println("Start trying to connect to database!");
            String url = "jdbc:dm://localhost:5236/GEN? useUnicode=true&characterEncoding=UTF-8";// 127.0.0.1 is the local address
            String user = "GEN";// User name: the default user name
            String password = "123456789";// The password you chose to set when installing
            con = DriverManager.getConnection(url, user, password);// Get the connection
            System.out.println("Connection successful!");
            String sql = "select * from gen_test1";// precompiled statement, "?" On behalf of the parameter
            pre = con.prepareStatement(sql);// instantiate the precompiled statement
            //pre.setString(1, "303"); // Set the parameter, the preceding 1 indicates the index of the parameter, not the index of the column name in the table
            result = pre.executeQuery();// Execute the query. Note that no arguments are required in parentheses
            while (result.next())
                // When the result set is not empty
                System.out.println("Student number." + result.getInt("id") + "Name:"
                        + result.getString("testName"));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                // Close each of the above objects one by one, as not closing them will affect performance and consume resources
                // Pay attention to the closing order, the last use of the first to close
                if(result ! =null)
                    result.close();
                if(pre ! =null)
                    pre.close();
                if(con ! =null)
                    con.close();
                System.out.println("Database connection closed!");
            }
            catch(Exception e) { e.printStackTrace(); }}}}Copy the code

So out of curiosity to check the source code, in the process of DriverManager class found a comment like thisSee the following loading source code at the same timeThe original implementation of the driver of the third party manufacturers as long as the Java SPI support, will automatically register in the driver, so I see the source code implementation is also to use a list to store all driversThis is a different vendor implementationTo solve the doubts in my heart!

This article is published by OpenWrite!