How to connect Aurora MySQL with Java

Keywords: MySQL SQL Java Database

Author: halo cloud Ma Ligang

 
 
 
The following is a reference example of using MySQL connector to connect Aurora for mysql:
 
Workbench accesses Aurora in the same way as mysql, such as:
Aurora.example.rds.cn-northwest-1.amazonaws.com.cn
User name: root
Password: root

Eclipse development first adds dependency in maven, as follows:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.15</version>
</dependency>

Source code:

package Aurora.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectAurora {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
                        //Declare Connection object
                        Connection con;
                        String driver = "com.mysql.cj.jdbc.Driver";
                        //The URL points to the database name mydata to be accessed
                        String url = "jdbc:mysql:// Aurora.example.rds.cn-northwest-1.amazonaws.com.cn:3306/db3";
                        //User name when MySQL is configured
                        String user = "root";
                        //Password for MySQL configuration
                        String password = "root";
                        //Traverse query result set
                        try {
                            //load driver
                            Class.forName(driver);
                            //1.getConnection() method, connect to MySQL database!!
                            con = DriverManager.getConnection(url,user,password);
                            if(!con.isClosed())
                                System.out.println("Succeeded connecting to the Database!");
                            //2. Create statement class object to execute SQL statement!!
                            Statement statement = con.createStatement();
                            //SQL statement to execute
                            String sql = "select * from fileindex";
                            //3.ResultSet class, used to store the obtained result set!!
                            ResultSet rs = statement.executeQuery(sql);
                            System.out.println("-----------------------------------------------------------------------------------------------");               
                            System.out.println("------------------------------------------------------------------------------------------------");  
                            System.out.println("| "+ "id" + "\t" +" | "+ "app_id"+  "\t" +" | "+  "File name"+ "\t" +" | " + "File address"+ "\t" +" | ");  
                            System.out.println("------------------------------------------------------------------------------------------------");                          
                            String APP_ID = null;
                            String id = null;
                            String address = null;
                            String school = null;
                            while(rs.next()){
                                //Get the data of stuname
                                id = rs.getString("ID");
                                //Get stuid data
                                APP_ID = rs.getString("APP_ID");
                                address= rs.getString("File_Name");
                                school=rs.getString("File_url");
                                //Output result
                                System.out.println(" | "+id + "\t" +" | "+ APP_ID +" | "+ "\t" +" | "+address +" | "+ "\t" +" | "+school +" | " );
                                System.out.println(" ------------------------------------------------------------------");  
                            }
                            rs.close();
                            con.close();
                        } catch(ClassNotFoundException e) {   
                            //Exception handling of database driver class
                            System.out.println("Sorry,can`t find the Driver!");   
                            e.printStackTrace();   
                            } catch(SQLException e) {
                            //Exception handling of database connection failure
                            e.printStackTrace();  
                            }catch (Exception e) {
                            // TODO: handle exception
                            e.printStackTrace();
                        }finally{
                           System.out.println("Database data obtained successfully!");
                        }
                    }

}

 
Workbench query:
 

 
Java query results:
 

 

  
About halo cloud
Halo cloud is an AWS service promotion organization in China. It is an independent business department company established by halo new network. It develops, sets up and operates a national cloud ecosystem to promote and support AWS to serve customers, including developers, start-ups, Internet enterprises, social and government agencies, and ICT service providers.
  
About the halo cloud community
Halo cloud community is the halo cloud ecological partnership program, which is dedicated to Realizing Inclusive technology with promoters and driving cloud ecology with social marketing.
After joining the halo cloud community, you will be granted the privileges of AWS product promotion return, AWS certification discount, technology article acquisition, and many rights and interests of halo cloud.
 
 
 
Wechat searches "halo cloud community" or scans the QR code below for more information.

  
The final interpretation right belongs to Guanghuan Cloud Data Co., Ltd!

Posted by Zup on Sat, 09 Nov 2019 08:41:23 -0800