如何用Java連線Aurora MySQL

光環雲社群發表於2019-05-16

作者:光環雲  馬立剛


以下是使用mysql-connector 連線Aurora for mysql參考示例:

Workbench 訪問Aurora,跟mysql方式一致,如: Aurora.example.rds.cn-northwest-1.amazonaws.com.cn

使用者名稱:root

密碼:root

如何用Java連線Aurora MySQL

Eclipse 開發首先在maven新增依賴,如下:  

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

原始碼:  

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
                        //宣告Connection物件
                        Connection con;
                        String driver = "com.mysql.cj.jdbc.Driver";
                        //URL指向要訪問的資料庫名mydata
                        String url = "jdbc:mysql:// Aurora.example.rds.cn-northwest-1.amazonaws.com.cn:3306/db3";
                        //MySQL配置時的使用者名稱
                        String user = "root";
                        //MySQL配置時的密碼
                        String password = "root";
                        //遍歷查詢結果集
                        try {
                            //載入驅動程式
                            Class.forName(driver);
                            //1.getConnection()方法,連線MySQL資料庫!!
                            con = DriverManager.getConnection(url,user,password);
                            if(!con.isClosed())
                                System.out.println("Succeeded connecting to the Database!");
                            //2.建立statement類物件,用來執行SQL語句!!
                            Statement statement = con.createStatement();
                            //要執行的SQL語句
                            String sql = "select * from fileindex";
                            //3.ResultSet類,用來存放獲取的結果集!!
                            ResultSet rs = statement.executeQuery(sql);
                            System.out.println("-----------------------------------------------------------------------------------------------");               
                            System.out.println("------------------------------------------------------------------------------------------------");  
                            System.out.println("| "+ "id" + "\t" +" | "+ "app_id"+  "\t" +" | "+  "檔名字"+ "\t" +" | " + "檔案地址"+ "\t" +" | ");  
                            System.out.println("------------------------------------------------------------------------------------------------");                          
                            String APP_ID = null;
                            String id = null;
                            String address = null;
                            String school = null;
                            while(rs.next()){
                                //獲取stuname這列資料
                                id = rs.getString("ID");
                                //獲取stuid這列資料
                                APP_ID = rs.getString("APP_ID");
                                address= rs.getString("File_Name");
                                school=rs.getString("File_url");
                                //輸出結果
                                System.out.println(" | "+id + "\t" +" | "+ APP_ID +" | "+ "\t" +" | "+address +" | "+ "\t" +" | "+school +" | " );
                                System.out.println(" ------------------------------------------------------------------");  
                            }
                            rs.close();
                            con.close();
                        } catch(ClassNotFoundException e) {   
                            //資料庫驅動類異常處理
                            System.out.println("Sorry,can`t find the Driver!");   
                            e.printStackTrace();   
                            } catch(SQLException e) {
                            //資料庫連線失敗異常處理
                            e.printStackTrace();  
                            }catch (Exception e) {
                            // TODO: handle exception
                            e.printStackTrace();
                        }finally{
                           System.out.println("資料庫資料成功獲取!");
                        }
                    }
}

Workbench 查詢:

如何用Java連線Aurora MySQL


Java 查詢結果: 

如何用Java連線Aurora MySQL


關於光環雲

光環雲是AWS服務在華推廣機構,是一家由光環新網組建的獨立業務部門公司。它開發、組建、運營一個全國的雲生態系統,以促進、支援AWS服務廣大客戶,包括開發者、初創公司、網際網路企業、社會與政府機構,以及ICT服務商。


關於光環雲社群

光環雲社群是光環雲生態夥伴計劃,致力於與推廣者實現普惠科技,以社會化營銷驅動雲端生態。 加入光環雲社群后將獲得AWS產品推廣返現、AWS認證優惠、技術文章獲取等特權,並得到光環雲多項權益賦能支援。


微信搜尋並關注“光環雲社群”獲取更多資訊。


最終解釋權歸光環雲資料有限公司!


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69917754/viewspace-2644638/,如需轉載,請註明出處,否則將追究法律責任。

相關文章