从Servlet连接到MySQL无法正常工作

时间:2017-02-02 12:15:38

标签: java mysql tomcat servlets jdbc

我正在制作一个小应用程序,因此我可以将我的笔记从我的课程发布到服务器。我在数据库端使用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应用程序中使用的数据库网址。此外,/DATASET是我的架构的正确名称。

单独注意,我知道如果许多用户使用它,我的实现会出现问题;我只想让它与单个用户合作,然后我会担心很多。

编辑:我使用tomcat作为JSP / Servlet页面的Web服务器,我也将mysql-connector放在我的项目(Eclipse)的Referenced Libraries部分。

非常感谢任何帮助!!!!

编辑:

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)

解: 如果使用eclipse IDE,将MySQL Connector JAR放入WEB-INF->lib。 此外,在建立连接之前需要Class.forName(driver);

3 个答案:

答案 0 :(得分:4)

  

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

首先 make将您的JDBC Connector放在类路径中,然后下载from here

如果你正在使用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

修改

就像@Gimby所说:确定你的jar在正确的位置,所以如果你使用Eclipse,如果Netbeans使用How to place a file on classpath in eclipse?

,请使用How to setup classpath in Netbeans?

答案 1 :(得分:0)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class Test {
    static final String DB_URL = "jdbc:mysql://localhost:3306/DATASET";
    static final String USER = "user";
    static final String PASS = "pass";
    static Connection con = null;
    static Statement s = null;
    public static void main(String[]args){
        final String query = "INSERT INTO FILE(upload_time, filename, data) VALUES(CURTIME(), 'booga33.txt', 'ooga booga dooga22222222222222!')";

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(DB_URL, USER, PASS);
            s = con.createStatement();
            //s.executeUpdate(query);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

}

答案 2 :(得分:0)

//Add MySQL jdbc jar file into project lib.

public static Connection con; 
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Database name", userid,password);