我问同样的问题,因为我没有找到答案。这是我的问题。 我一直在尝试使用 jdbc 驱动程序连接到 mysql 数据库。 这是我的主要 sql 处理程序类:
package com.arslanjava.model;
import java.sql.*;
public class SqlHandler {
private Connection myConnection;
// final private String serverName = "localhost" ;
//final private String port = "3306" ;
//final private String user = "root" ;
// final private String password = "password" ;
public SqlHandler ( String username , String password , String fname , String lName ) throws SQLException {
if ( username != null && password != null && fname != null && lName != null ) {
//Establish connection to the server.
this.establishConnection();
//execute the addUser method
this.addUser(username,password,fname,lName);
//close this connection
this.close();
} else {
throw new NullPointerException () ;
}
}
public SqlHandler() {
}
public void addUser (String us , String pas , String fName , String lName ) throws SQLException {
PreparedStatement statement = myConnection.prepareStatement("INSERT INTO user_info (username, password, first_name, last_name) VALUES (?,?,?,?)");
statement.setString(1,us);
statement.setString(2,pas);
statement.setString(3,fName);
statement.setString(4,lName);
statement.executeUpdate();
}
public void establishConnection () throws SQLException {
myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/app1","root","password") ; ;
}
public void close() {
if (myConnection != null) {
try {
myConnection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
一切正常,当我在纯 Java 代码中使用它时。例如,这工作正常:
package com.arslanjava.model;
import com.arslanjava.model.SqlHandler;
import java.io.IOException;
import java.net.ServerSocket;
import java.sql.SQLException;
public class Test {
public static void main(String[] args) {
SqlHandler handler = new SqlHandler() ;
try {
handler.establishConnection();
handler.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
但是,当我尝试在我的 servlet 类中使用它时,我收到一条错误消息:
找不到适合 jdbc:mysql://localhost/name_of_my_database 的驱动程序。
我检查了上一个问题的答案,它说这是由两件事引起的:
- 错误的 url,我检查过,没问题。(jdbc:mysql://localhost:3306/app1)
- 我没有添加 jar 文件
在 tomcat 的 lib 文件夹中,我这样做了,但我仍然得到相同的结果
错误。(截图:https://prnt.sc/k2qlxa,https://prnt.sc/k2qm1e,http://prntscr.com/k2qm5h)
我还把这个 jar 文件添加到 WEB-INF/lib 文件夹,但也没有用。
还有什么我可以尝试的吗?请帮我理解这里的问题,因为我无法解决这个问题....
这是我的 servlet 类:
package com.arslanjava.web;
import com.arslanjava.model.SqlHandler;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;
public class MyRegServlet extends HttpServlet {
@Override
public void doPost (HttpServletRequest request , HttpServletResponse response) {
String username = request.getParameter("us") ;
String password = request.getParameter("psw") ;
String first_name = request.getParameter("fname");
String last_name = request.getParameter("lname") ;
try {
SqlHandler handler = new SqlHandler(username,password,first_name,last_name) ;
} catch (SQLException e) {
request.setAttribute("errorCode","5xx");
request.setAttribute("errorMessage",e.toString());
e.printStackTrace();
RequestDispatcher dispatcher = request.getRequestDispatcher("customErroPage.jsp") ;
try {
dispatcher.forward(request,response);
} catch (ServletException | IOException e1) {
e1.printStackTrace();
}
}
}
}
最佳答案
你试过用这条线吗?
Class.forName("com.mysql.jdbc.Driver");//在您的代码中包含这一行。
如果没有尝试在此函数中添加这一行。
public void establishConnection() throws SQLException {
myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/app1","root","password");
}
关于java - 找不到适合 jdbc :mysql://localhost/database 的驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51184824/