我正在开发一个应用程序,其中用户以表格形式输入他/她的信息,并将该表格的信息插入到Google Cloud MySQL数据库中。我已经编写了实现以下任务的代码: `
package com.example.dao;
import java.sql.Connection;
import java.sql.DriverManager;
public class dao {
private static Connection con;
public static Connection getcon(){
try{
String instanceConnectionName = "edu-vitae1";
String databaseName = "eduvitae";
String username = "root";
String password = "root";
Class.forName("com.mysql.jdbc.Driver");
String jdbcUrl = String.format(
"jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae-211606:asia-south1:edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=root&password=root&useSSL=false",
databaseName,
instanceConnectionName);
//Connection con=DriverManager.getConnection("jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae-211606:asia-south1:edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=root&password=root");
Connection con = DriverManager.getConnection(jdbcUrl, username, password);
//con=DriverManager.getConnection("jdbc:mysql://35.200.134.221/edu","root","root");
}
catch (Exception e) {
System.out.println(e);
}
return con;
}
}
执行上述代码后,出现以下错误:
java.sql.SQLException: java.lang.NoClassDefFoundError: com/mysql/cj/protocol/SocketFactory
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:430)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.example.dao.dao.getcon(dao.java:20)
at com.example.model.register.doPost(register.java:105)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.googlecode.objectify.ObjectifyFilter.doFilter(ObjectifyFilter.java:48)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:764)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1388)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
我正在使用Google Cloud Engine,并且我的应用程序正在JDK 1.8.0_131上运行。我包括“ mysql-socket-factory-connector-j-8-1.0.10.jar”,“ mysql-connector-java” -8.0.11.jar”和“ jetty-unixsocket-9.4.8.v20171121.jar”在我的应用程序的构建路径中。
请帮助我理解和解决此问题。
预先谢谢你。
答案 0 :(得分:0)
有关错误Why am I getting a NoClassDefFoundError in Java?
的一些信息对于我来说,我已经通过将mylib.jar添加到服务器上的“ tomcat 9 dir / lib”文件夹以及本地的类路径中来解决了这个问题(在Eclipse Properties> Java Build Path> Libraries-> Add External JARs中) ...)。您能描述一下如何添加罐子吗?
答案 1 :(得分:0)
我通过在WEB-INF / lib文件夹中插入指定的jar文件解决了此错误。我已经在项目的构建路径中添加了jar文件,但是因为它不在WEB-INF / lib文件夹中,所以jvm无法加载课程。
因此,概括起来,无论何时在项目的构建路径中添加文件,都请记住将同一文件复制到WEB-INF / lib文件夹中。