简体   繁体   English

使用 JDBC 连接到 AWS RDS 实例

[英]Connecting to AWS RDS Instance with JDBC

I'm building a personal website and have decided to use AWS RDS for my database and connecting my back end to it using JDBC.我正在建立一个个人网站,并决定将 AWS RDS 用于我的数据库,并使用 JDBC 将我的后端连接到它。 I have a main method I have created to test my connection to AWS RDS and it returns my tables from my database into my eclipse console properly.我创建了一个主要方法来测试我与 AWS RDS 的连接,它将我的表从我的数据库正确返回到我的 eclipse 控制台。 However, when I launch Tomcat 9 and open my website locally in Google Chrome I get the error message java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver. However, when I launch Tomcat 9 and open my website locally in Google Chrome I get the error message java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver. It's as if my driver isn't being recognized my Tomcat.就好像我的驱动程序没有被我的 Tomcat 识别。 I have tried creating a copy of my Connector driver for MySQL and putting it in my Tomcat's lib folder but that didn't fix anything.我尝试为 MySQL 创建我的连接器驱动程序的副本并将其放入我的 Tomcat 的 lib 文件夹中,但这并没有解决任何问题。

DButil class that establishes connection to my DB. DButil class 建立与我的数据库的连接。 my connectionURL string has been modified to hide my real url for security.我的 connectionURL 字符串已被修改以隐藏我的真实 url 以确保安全。

public class DButil {

    private static String connectionURL = "jdbc:mysql://demo.code.us-east-1.rds.amazonaws.com/tablename?user=admin&password=password";

    public static Connection getConnection() {
        
        Connection connection = null;
        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(connectionURL);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }
    
    public static void main(String[] args) throws SQLException {
        Connection conn = DButil.getConnection();

        if (null != conn) {
            System.out.println("A Complete Connection.");

            DatabaseMetaData metaData = conn.getMetaData();
            ResultSet rs = metaData.getTables(null, null, "%", null);

            while (rs.next()) {
                System.out.println(rs.getString(3));
            }
        } else {
            System.out.println("Error, No Connection.");

        }

    }
}

my DAO class.我的道 class。

public class AgrDao {

    private String selectAllFromShows = "SELECT showId, showImage, showLocation, entryPrice, showDate, upcoming, showName FROM Shows";
    
    public List<Show> getAllShows() {
        List<Show> shows = new ArrayList<Show>();

        ResultSet result = null;
        Statement statement = null;

        Connection conn = DButil.getConnection();

        try {
            statement = conn.createStatement();
            result = statement.executeQuery(selectAllFromShows);
            while (result.next()) {
                Show show = createShow(result);
                shows.add(show);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    result.close();
                    statement.close();
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return shows;
        
    }

    private Show createShow(ResultSet result) throws SQLException {
        Show newShow = new Show(result.getInt("showId"), 
                                result.getString("showName"), 
                                result.getString("showImage"),
                                result.getString("showLocation"),
                                result.getDouble("entryPrice"),
                                result.getString("showDate"),
                                result.getBoolean("upcoming"));
        return newShow;
    }

}

How did you package your project?你的项目是如何 package 的? Here is an AWS tutorial that shows you how to build an example Java WEB app (this example app is developed by using Spring BOOT) that uses Amazon RDS to store application data.这是一个 AWS 教程,向您展示如何构建使用 Amazon RDS 存储应用程序数据的示例 Java WEB 应用程序(此示例应用程序是使用 Spring BOOT 开发的)来存储应用程序数据。

在此处输入图像描述

In this example app, the MySQL Driver is specified in the POM file:在此示例应用程序中,在 POM 文件中指定了 MySQL 驱动程序:

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
</dependency>

Then the project is packaged by using a Maven command and deployed to Elastic Beanstalk .然后使用 Maven 命令打包项目并部署到Elastic Beanstalk

Creating the Amazon Relational Database Service item tracker 创建 Amazon Relational Database Service 项目跟踪器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM