gpt4 book ai didi

java - Servlet 错误 JDBC ClassNotFoundException

转载 作者:行者123 更新时间:2023-11-28 22:56:56 25 4
gpt4 key购买 nike

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

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 数据库

最佳答案

您正在加载错误的 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();
}
}
}

关于java - Servlet 错误 JDBC ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24828020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com