我试图通过servlet访问MySql
数据库,但它显示
java.lang.ClassNotFoundException: con.mysql.jdbc.Driver
和
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/aptitude
我正在使用xampp
来实现程序,我已经设置了mysql
连接器的类路径,并且还将mysql-connector
设置为web-inf\lib
但我还是面临同样的问题。
建议?
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class aptservlet1 extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
String correct=2+"";
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String user,pass;
user=req.getParameter("user");
pass=req.getParameter("pass");
//apt1 o1=new apt1();
user=user.trim();
pass=pass.trim();
try
{
Class.forName("con.mysql.jdbc.Driver");
}
catch(ClassNotFoundException ce)
{
out.println(ce+"<br>");
}
try
{
String url="jdbc:mysql://localhost:3306/" + "aptitude";
Connection con=DriverManager.getConnection(url,"root","");
Statement s=con.createStatement();
ResultSet rs=s.executeQuery("select a_pass from admin_database where a_user='"+user+"'");
rs.next();
String s1=rs.getString(1);
if(s1.equals(pass))
{
correct=1+"";
}
else
{
correct=0+"";
}
rs.close();
s.close();
con.close();
}
catch(SQLException se)
{
out.println(se+"<br>");
}
out.println("value of correct="+correct);
ServletContext sc=this.getServletContext();
}
}
答案 0 :(得分:1)
你拼错了这个类,你说CON.myspl.jdbc.Driver而不是com.mysql.jdbc.Driver