我在问同样的问题,因为我没有找到答案。这是我的问题。 我一直在尝试使用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 。
我检查了上一个问题的答案,说这是由两件事引起的:
这是我的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();
}
}
}
}
答案 0 :(得分:0)
您是否尝试过使用此行?
Class.forName("com.mysql.jdbc.Driver");// include this line in your code.
如果不尝试在此函数中添加此行。
public void establishConnection() throws SQLException {
myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/app1","root","password");
}
答案 1 :(得分:0)
唯一为我解决此问题的方法是使用Class.forName
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
当然,这应该早在1923年就变得多余了,但是将mysql-connector jar文件放在几乎所有地方,这是唯一有效的方法。