繁体   English   中英

使用 JDBC 连接到 AWS RDS 实例

[英]Connecting to AWS RDS Instance with JDBC

我正在建立一个个人网站,并决定将 AWS RDS 用于我的数据库,并使用 JDBC 将我的后端连接到它。 我创建了一个主要方法来测试我与 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. 就好像我的驱动程序没有被我的 Tomcat 识别。 我尝试为 MySQL 创建我的连接器驱动程序的副本并将其放入我的 Tomcat 的 lib 文件夹中,但这并没有解决任何问题。

DButil class 建立与我的数据库的连接。 我的 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.");

        }

    }
}

我的道 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;
    }

}

你的项目是如何 package 的? 这是一个 AWS 教程,向您展示如何构建使用 Amazon RDS 存储应用程序数据的示例 Java WEB 应用程序(此示例应用程序是使用 Spring BOOT 开发的)来存储应用程序数据。

在此处输入图像描述

在此示例应用程序中,在 POM 文件中指定了 MySQL 驱动程序:

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

然后使用 Maven 命令打包项目并部署到Elastic Beanstalk

创建 Amazon Relational Database Service 项目跟踪器

暂无
暂无

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

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