本篇文章給大家分享的是有關(guān)如何用Java連接Aurora MySQL,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
創(chuàng)新互聯(lián)長(zhǎng)期為上千多家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為鯉城企業(yè)提供專(zhuān)業(yè)的成都網(wǎng)站建設(shè)、成都做網(wǎng)站,鯉城網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
以下是使用mysql-connector 連接Aurora for mysql:
Workbench 訪問(wèn)Aurora,跟mysql方式一致,如: Aurora.example.rds.cn-northwest-1.amazonaws.com.cn
用戶名:root
密碼:root
Eclipse 開(kāi)發(fā)首先在maven添加依賴,如下:
mysql mysql-connector-java 8.0.15
源代碼:
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對(duì)象 Connection con; String driver = "com.mysql.cj.jdbc.Driver"; //URL指向要訪問(wèn)的數(shù)據(jù)庫(kù)名mydata String url = "jdbc:mysql:// Aurora.example.rds.cn-northwest-1.amazonaws.com.cn:3306/db3"; //MySQL配置時(shí)的用戶名 String user = "root"; //MySQL配置時(shí)的密碼 String password = "root"; //遍歷查詢結(jié)果集 try { //加載驅(qū)動(dòng)程序 Class.forName(driver); //1.getConnection()方法,連接MySQL數(shù)據(jù)庫(kù)!! con = DriverManager.getConnection(url,user,password); if(!con.isClosed()) System.out.println("Succeeded connecting to the Database!"); //2.創(chuàng)建statement類(lèi)對(duì)象,用來(lái)執(zhí)行SQL語(yǔ)句!! Statement statement = con.createStatement(); //要執(zhí)行的SQL語(yǔ)句 String sql = "select * from fileindex"; //3.ResultSet類(lèi),用來(lái)存放獲取的結(jié)果集!! 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這列數(shù)據(jù) id = rs.getString("ID"); //獲取stuid這列數(shù)據(jù) APP_ID = rs.getString("APP_ID"); address= rs.getString("File_Name"); school=rs.getString("File_url"); //輸出結(jié)果 System.out.println(" | "+id + "\t" +" | "+ APP_ID +" | "+ "\t" +" | "+address +" | "+ "\t" +" | "+school +" | " ); System.out.println(" ------------------------------------------------------------------"); } rs.close(); con.close(); } catch(ClassNotFoundException e) { //數(shù)據(jù)庫(kù)驅(qū)動(dòng)類(lèi)異常處理 System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { //數(shù)據(jù)庫(kù)連接失敗異常處理 e.printStackTrace(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ System.out.println("數(shù)據(jù)庫(kù)數(shù)據(jù)成功獲取!"); } } }
Workbench 查詢:
Java 查詢結(jié)果:
以上就是如何用Java連接Aurora MySQL,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。