Connect MySQL with JDBC

Keywords: MySQL JDBC Database SQL

Existing configuration: install IntelliJ (Java IDE) and MySQL.

Then use MySQL Installer to install Connector/J.

Open Intelli, create a new Project, and create a MySQL demo class. The code is as follows. The code comes from https://www.runoob.com/java/java-mysql-connect.html

Some of them need to be changed into the contents of their own databases. For details, see the notes.

import java.sql.*;

public class MySQL_Demo {

    //JDBC driver name has been updated to com.mysql.cj.jdbc.Driver. The old one is useless.
    //Add? serverTimezone=GMT after the DB? URL, because the version is compatible with the time zone...
    //How to write the URL: jdbc connection method / / local address / / SQL database port / / database address
    //The connection mode of jdbc is: jdbc:mysql://
    //The local address and user name can be viewed through SELECT user,host from mysql.user
    //If you don't know the SQL database port, you can use the "show global variables like 'port';" command to view it immediately. The default is 3306
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/jxgl?serverTimezone=GMT";

    // The user name and password of the database should be set according to your own settings
    static final String USER = "root";
    static final String PASS = "963852";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{
            // Register JDBC Driver
            //We need to change the driver name here
            Class.forName("com.mysql.cj.jdbc.Driver");

            // Open links
            System.out.println("Connect to database...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            // Execution query
            System.out.println(" instantiation Statement object...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT Sno, Sname, Ssex FROM student";   //Change to Table in your database
            ResultSet rs = stmt.executeQuery(sql);

            // Expand result set database
            while(rs.next()){
                // Search by field, change to own
                int id  = rs.getInt("Sno");
                String name = rs.getString("Sname");
                String url = rs.getString("Ssex");

                // Output data, change to your own
                System.out.print("Sno: " + id);
                System.out.print(", Sname: " + name);
                System.out.print(", Ssex: " + url);
                System.out.print("\n");
            }
            // Close when done
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // Handling JDBC errors
            se.printStackTrace();
        }catch(Exception e){
            // Handling Class.forName error
            e.printStackTrace();
        }finally{
            // close resource
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// Don't do anything?
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }
}

After writing, it can't run successfully. Copy mysql-connector-java-8.0.15.jar (default in C disk Program Files(x86)) under Connector/J installation directory to IntelliJ's project src, as shown in the following figure.

Right click the jar package in IntelliJ and select Add as Library.

After that, you can see the result.

Posted by 8ta8ta on Wed, 13 Nov 2019 09:49:11 -0800