In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use Java to connect Aurora MySQL, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The following is to connect to Aurora for mysql using mysql-connector:
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 developers first add dependencies to maven, as follows:
Mysql mysql-connector-java 8.0.15
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" / / URL points to the database name to be accessed mydata String url = "jdbc:mysql:// Aurora.example.rds.cn-northwest-1.amazonaws.com.cn:3306/db3"; / / the user name String user = "root" when MySQL is configured / / password for MySQL configuration String password = "root"; / / traverse the query result set try {/ / load driver Class.forName (driver) / / 1.getConnection () method to connect to the MySQL database! Con = DriverManager.getConnection (url,user,password); if (! con.isClosed ()) System.out.println ("Succeeded connecting to the Database!"); / / 2. Create a statement class object to execute SQL statements! Statement statement = con.createStatement (); / / the SQL statement to be executed String sql = "select * from fileindex"; / / 3.ResultSet class, which is 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 stuname column data id = rs.getString ("ID"); / / get stuid column 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) {/ / Database driver class exception handling System.out.println ("Sorry, can`t find the Driver!"); e.printStackTrace () } catch (SQLException e) {/ / Database connection failure exception handling e.printStackTrace () } catch (Exception e) {/ / TODO: handle exception e.printStackTrace ();} finally {System.out.println ("Database data obtained successfully!") ;}
Workbench query:
Java query results:
The above is how to use Java to connect to Aurora MySQL. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.