Servlet ERROR JDBC ClassNotFoundException

时间:2014-07-18 14:57:02

标签: java mysql tomcat servlets jdbc

我有一个登录页面。 用户正在提供用户名和通行证。 然后登录sevlet检查用户是否存在于Db中,如果没有,则用户名和密码被发送到ldap服务器,如果用户存在于ldap服务器上,则在本地mySql DB上创建新用户。

Servlet工作正常我可以看到pass和usename被发送到TOMCAT。问题是当我尝试连接到mySQL数据库时。 这是代码:

package database;
import java.sql.*;


public class DbConnect {

    /**
     * Static method for user authentication
     * Test method
     * @param username - Username already striped from illigal chars (XSS safe)
     * @param pass - password
     */
    public static void connect(String username,String pass)//Consider changing void to boolean
    {
        Connection con;
        PreparedStatement st;
        ResultSet rs;
        try {
            //Register JDBC driver
            Class.forName("oracle.jdbc.OracleDriver");//Loading DB Driver
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //Open a connection
        //  Database credentials
        final String USER = "root";
        final String PASS = "";
        final String DB_URL = "jdbc:mysql://localhost:3306/imf_jsp";
        try {
             con = DriverManager.getConnection(DB_URL,USER,PASS);
             st = con.prepareStatement("SELECT * FROM user WHERE signum = ? AND pass_hash = ?"); //Find user with given username & pass
             st.setString(1, username);//Bind param (Should prevent SQL Injection? Not sure but it is working in PHP with PDO)
             st.setString(2, pass);//Bind param (Should prevent SQL Injection? Not sure but it is working in PHP with PDO)
             st.execute();//statemant execution
             System.out.println("In Database?: " + st);//If not then check in LDAP
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }

这是StackTrace:

java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at database.DbConnect.connect(DbConnect.java:20)
at servlets.loginServlet.doPost(loginServlet.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

我正在使用ojdbc5.jar它是在WEB-INF / lib中添加到bildpath 可能有什么问题? 使用PHP,我连接到mySQL数据库没有问题

2 个答案:

答案 0 :(得分:1)

您正在加载错误的JDBC驱动程序。 MySQLs JDBC驱动程序是com.mysql.jdbc.Driver。您的代码应如下所示:

package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class DbConnect {

    /**
     * Static method for user authentication
     * Test method
     * @param username - Username already striped from illigal chars (XSS safe)
     * @param pass - password
     */
    public static void connect(String username,String pass)//Consider changing void to boolean
    {
        Connection con;
        PreparedStatement st;
        ResultSet rs;
        try {
            //Register JDBC driver
            Class.forName("com.mysql.jdbc.Driver");//Loading DB Driver
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //Open a connection
        //  Database credentials
        final String USER = "root";
        final String PASS = "";
        final String DB_URL = "jdbc:mysql://localhost:3306/imf_jsp";
        try {
             con = DriverManager.getConnection(DB_URL,USER,PASS);
             st = con.prepareStatement("SELECT * FROM user WHERE signum = ? AND pass_hash = ?"); //Find user with given username & pass
             st.setString(1, username);//Bind param (Should prevent SQL Injection? Not sure but it is working in PHP with PDO)
             st.setString(2, pass);//Bind param (Should prevent SQL Injection? Not sure but it is working in PHP with PDO)
             st.execute();//statemant execution
             System.out.println("In Database?: " + st.toString());//If not then check in LDAP
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }

答案 1 :(得分:0)

我本可以评论,但我没有权利,所以我写这个作为答案。 您说您正在尝试连接mySql数据库。但jar名称和类名是oracle的。你需要mysql jdbc jar。