如何用Java连接Aurora MySQL

发布时间:2021-11-18 15:49:01 作者:柒染
来源:亿速云 阅读:297

本篇文章给大家分享的是有关如何用Java连接Aurora MySQL,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

以下是使用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

以上就是如何用Java连接Aurora MySQL,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. java连接mysql 8.0的问题
  2. AWS基于Mysql/postgres开发的Aurora的性能测试

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

aurora java mysql

上一篇:MySQL5.7 SYS Schema的性能框架视图参数有哪些

下一篇:怎么将dockerfile制作好的镜像发布到Docker hub上

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》