首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JDBC连接到AWS RDS实例

问使用JDBC连接到AWS RDS实例
EN

Stack Overflow用户
提问于 2021-03-16 01:51:04
回答 1查看 498关注 0票数 1

我正在建立一个个人网站,并决定将AWS RDS用于我的数据库,并使用JDBC将我的后端连接到它。我创建了一个main方法来测试我与AWS RDS的连接,它将我的表从我的数据库正确地返回到我的eclipse控制台。然而,当我启动Tomcat9并在Google Chrome本地打开我的网站时,我得到了错误消息java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver。好像我的司机没有被我的Tomcat认出。我曾尝试为MySQL创建连接器驱动程序的副本,并将其放入Tomcat文件夹中,但这并没有解决任何问题。

建立到我的数据库的连接的DButil类。出于安全考虑,我的connectionURL字符串已被修改为隐藏我的真实url。

代码语言:javascript
复制
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.");

        }

    }
}

我的刀课。

代码语言:javascript
复制
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;
    }

}
EN

回答 1

Stack Overflow用户

发布于 2021-03-16 02:01:17

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

​

​

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

代码语言:javascript
复制
<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
</dependency>

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

Creating the Amazon Relational Database Service item tracker

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66643043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档