一尘不染

从Servlet与MySQL的连接无法正常工作

tomcat

我正在制作一个小型应用程序,因此可以将我的笔记从课堂上发布到服务器上。我在数据库端使用JDBC和MySQL,在应用程序端使用JSP / Servlet。

我编写了一个独立的Java应用程序,该应用程序与servlet代码非常相似,并且运行良好。我的servlet代码无法正常工作。

属性定义:

static final String DB_URL = "jdbc:mysql://localhost/DATASET";
static final String USER = "user";
static final String PASS = "pass";
Connection con = null;
Statement s = null;

doPost方法:

    final String STATEMENT = "INSERT INTO FILE(upload_time, filename, class, data) VALUES(CURTIME(), '" + request.getParameter("file") + "', '" + request.getParameter("cls") + "', '" + request.getParameter("comm") + "')";
    final String STATEMENT2 = "INSERT INTO FILE(upload_time, filename, data) VALUES(CURTIME(), 'booga33.txt', 'ooga booga dooga22222222222222!')";

    try {
        con = DriverManager.getConnection(DB_URL, USER, PASS);
        s = con.createStatement();
        s.executeUpdate(STATEMENT2);
    }
    catch(SQLException i)
    {
        PrintWriter w = response.getWriter();
        w.println(i.getErrorCode());
    }
    catch(Exception e)
    {
        PrintWriter w = response.getWriter();
        w.println("error occured.");
    }

它抛出,SQLException并给我一个错误代码0,这是无效的登录名。用户名/密码是正确的,但我认为URL格式错误(即使这恰恰是我在Java应用程序中使用的数据库URL。也是/DATASET正确的架构名称)。

另外,我知道如果很多用户使用我的实现,就会遇到问题。我只想让它与单个用户一起使用,然后我会担心很多。

编辑:我将tomcat用作JSP / Servlet页面的Web服务器,我还将mysql-
connector放置在项目(Eclipse)的“引用的库”部分中。

对此的任何帮助将不胜感激!

编辑:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/DATASET
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DoSubmit.doPost(DoSubmit.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
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:474)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:495)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:767)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1347)
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)

解决方案:WEB-INF->lib如果使用Eclipse IDE,则将MySQL Connector
JAR放入其中。同样,Class.forName(driver);在建立连接之前需要。


阅读 262

收藏
2020-06-16

共1个答案

一尘不染

找不到适用于jdbc:mysql:// localhost:3306 / DATASET的驱动程序

首先
将JDBC连接器放在类路径中,您可以从此处下载它

如果您正在使用maven,请使用以下命令:

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.13</version>
</dependency>

其次, 确保您的连接信息正确:

public class CreateConnection {
    String driver = "com.mysql.jdbc.Driver";
    String DB_username = "root";
    String DB_password = "pass";
    String DB_URL = "jdbc:mysql://localhost:3306/DATABASE";

    public Connection getConnection() {
        try {
            Class.forName(driver);
            java.sql.Connection con = DriverManager.getConnection(DB_URL, DB_username, DB_password);
            return con;

        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Exception " + e);
            return null;
        }
    }
}

我建议您改用Prepared Statement,这是一种安全的做法,您可以在此处了解此Prepared
Statement

2020-06-16