JAVA program operation MYSQL database

Keywords: SQL Database Java MySQL

Preface

Before learning to operate MYSQL database with JAVA program, we need to download and configure it Eclipse and jdk1.8 , and the tools most needed this time: MySQL database and JDBC driver

Download driver

jdbc 8.0.11 download website: https://dev.mysql.com/downloads/connector/j/

Load driver

Open eclipse and right-click on the left to create a new java project.

Analogous to SQLite database add JDBC , we need to right-click the new MySQL test1 project and add JDBC drivers in turn.

Connect to database

First, you need to create databases and tables, which are described in the first two articles:
https://blog.csdn.net/qq_42446456/article/details/81123299
https://blog.csdn.net/qq_42446456/article/details/81138147
Let's do it again:





java connection database code:

package MySQLTEST;

import java.sql.Connection;//Import the Connection class in the Java.sql package to connect to the database
import java.sql.DriverManager;//Import the DriverManager class in the Java.sql package to manage a set of JDBC drivers
import java.sql.ResultSet;//Import the ResultSet class in the Java.sql package to represent the results of the database for query
import java.sql.Statement;//Import the Statement class in the Java.sql package, execute the SQL Statement, and return the generated result
import java.sql.SQLException;//Import the Exception class in the Java.sql package, execute the SQL statement, and find the Exception.

public class Table { //Define a class named Table
     public static void main(String[] args) {//The main program runs from here
         Connection conn = null;  //Declare database connection object
         String driver ="com.mysql.cj.jdbc.Driver";//Load database driver
         String url ="jdbc:MySQL://localhost:3306/table1?&useSSL=false&serverTimezone=UTC";
         //url points to the database table1 to be accessed
         String username ="root";//User name when MySQL is configured
         String password ="965827";//Password for MySQL configuration
         try {  //Load database driver / / sql statement 
             Class.forName(driver);
             //getConnection() method, connect to MySQL database!
             conn=DriverManager.getConnection(url,username,password);
             if(!conn.isClosed())
                 System.out.println("Database connection succeeded!");
             //Create statement class object to execute SQL statement!
             Statement Statement=conn.createStatement();
             //SQL statement to execute
             String sql="select * from student" ;
             //ResultSet class, used to store the obtained result set!
             ResultSet rs=Statement.executeQuery(sql);
             System.out.println("-------------------------------");
             System.out.println("The results are as follows:");  
             System.out.println("-------------------------------");  
             System.out.println("Full name" + "\t" + "Sex"+"\t"+"Age"+"\t"+"wages");  
             System.out.println("-------------------------------");  
             String name=null;
             String sex=null;
             String age=null;
             String pay=null;
             while(rs.next()){
                 //Get 'name' data
                 name=rs.getString("Full name");
                 //Get 'gender' data
                 sex=rs.getString("Gender");
                 //Get 'age' data
                 age=rs.getString("Age");
                 //Get the data of "salary"
                 pay=rs.getString("wages");
                 //Output result
                 System.out.println(name+"\t"+sex+"\t"+age+"\t"+pay);
             }
             rs.close();
             conn.close();
         }
         catch(ClassNotFoundException e){
                 //Exception handling of database driver class
             System.out.println("Database driver loading failed!");
             e.printStackTrace();
         }
         catch(SQLException e1){
             //Exception handling of database connection failure
             e1.printStackTrace();
         }
         catch(Exception e2){
             e2.printStackTrace();
         }
         finally{
                System.out.println("-------------------------------");  
                System.out.println("Database data obtained successfully!");
            }
    }
}

Compilation result:

Posted by adamata on Fri, 31 Jan 2020 10:38:20 -0800