简体   繁体   中英

Not able to connect to AWS RDS MySql DB from AWS Lambda. ClassNotFoundException: com.mysql.jdbc

I am using maven project and added mysql connector dependency in pom.xml file and hence I am able to hit the RDS MySql db correctly. But when I am deploying the maven build jar to AWS Lambda and trying to hit the function using API Gateway call, it's showing below error in cloudwatch.

*Error :*
com.mysql.jdbc.Driver: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.project.Apigateway.App.handleRequest(App.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)

I don't understand why the dependency is not resolved when I am deploying the project jar to AWS Lambda. Please help me with your valuable suggestion.

    public Connection makeJDBCConnection() throws ClassNotFoundException {

            try {
                Class.forName("com.mysql.jdbc.Driver");
                String userName = "*****";
                String password = "*****";
                String dbName = "user";
                String jdbcUrl = "jdbc:mysql://savetemp.cga2ptefm4rq.ap-south-1.rds.amazonaws.com:3306/" + dbName + "?user="
                        + userName + "&password=" + password;
                crunchifyConn = DriverManager.getConnection(jdbcUrl);
                if (crunchifyConn != null) {
                    connected = true;
                }
            } catch (Exception ex) {
                System.out.println("MySQL Connection Failed!" + ex);
            }
            return crunchifyConn;

        }

确保已安装软件包。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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