我有以下代码,其中我找不到com的类异常。 mysql.jdbc.Driver。我该怎么办?
try {
String name = request.getParameter("name");
String pass = request.getParameter("pass");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dimple?zeroDateTimeBehavior=convertToNull", "root", "password");
PreparedStatement ps = con.prepareStatement("Insert Into data Values(? ,? )");
ps.setString(1, name);
ps.setString(2, pass);
int f = ps.executeUpdate();
if(f>0)
{
out.println("Successfuly inserted");
RequestDispatcher rd = request.getRequestDispatcher("index.html");
rd.forward(request, response);
}
else
out.println("Not inserted");
}
catch(Exception e)
{
out.println(e);
}
答案 0 :(得分:1)
答案 1 :(得分:0)
首先应该将驱动程序jar文件下载并添加到项目类路径中,然后加载该类。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!
public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
}
}
完成这些步骤后,您可以像现在一样使用驱动程序。
更多信息: