错误提示:驱动我也放到lib里面了。
源代码:
package first.second.three;import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;public class loginServlet extends HttpServlet
{
private String url;
private String user;
private String password;
public void init() throws ServletException
{
ServletContext sc=getServletContext();
String driverClass=sc.getInitParameter("driverClass");
url=sc.getInitParameter("url");
user=sc.getInitParameter("user");
password=sc.getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(ClassNotFoundException ce)
{
throw new ServletException("加载数据库驱动失败!");
}
}
public void doGet (HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
req.setCharacterEncoding("gb2312");
String condition1=req.getParameter("username");
String condition2=req.getParameter("password");
if (condition1==""||condition2=="")
{
resp.sendRedirect("loginFail.html");
return;
}
resp.setContentType("text/html;charset=gb2312");
PrintWriter out=resp.getWriter();
try
{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
if(condition1!=""&&condition2!="")
{
rs=stmt.executeQuery("select stu_pwd from stu_info where stu_id=condition1");
if(rs.next())
{
if(rs.getString("stu_pwd")==condition2)
{
resp.sendRedirect("loginSuccess.html");
}
else
{
resp.sendRedirect("loginFail1.html");
}
}
else
{
resp.sendRedirect("loginFail2.html");
}
}
}
catch (SQLException se)
{
se.printStackTrace();
}
}
}
求高手帮解决下
源代码:
package first.second.three;import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;public class loginServlet extends HttpServlet
{
private String url;
private String user;
private String password;
public void init() throws ServletException
{
ServletContext sc=getServletContext();
String driverClass=sc.getInitParameter("driverClass");
url=sc.getInitParameter("url");
user=sc.getInitParameter("user");
password=sc.getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(ClassNotFoundException ce)
{
throw new ServletException("加载数据库驱动失败!");
}
}
public void doGet (HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
req.setCharacterEncoding("gb2312");
String condition1=req.getParameter("username");
String condition2=req.getParameter("password");
if (condition1==""||condition2=="")
{
resp.sendRedirect("loginFail.html");
return;
}
resp.setContentType("text/html;charset=gb2312");
PrintWriter out=resp.getWriter();
try
{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
if(condition1!=""&&condition2!="")
{
rs=stmt.executeQuery("select stu_pwd from stu_info where stu_id=condition1");
if(rs.next())
{
if(rs.getString("stu_pwd")==condition2)
{
resp.sendRedirect("loginSuccess.html");
}
else
{
resp.sendRedirect("loginFail1.html");
}
}
else
{
resp.sendRedirect("loginFail2.html");
}
}
}
catch (SQLException se)
{
se.printStackTrace();
}
}
}
求高手帮解决下
type Exception reportmessagedescription The server encountered an internal error () that prevented it from fulfilling this request.exceptionjavax.servlet.ServletException: 加载数据库驱动失败!
first.second.three.loginServlet.init(loginServlet.java:27)
javax.servlet.GenericServlet.init(GenericServlet.java:212)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
java.lang.Thread.run(Unknown Source)
浏览器提示的错误、 谢谢你了
驱动找不到驱动是放在tomcat下面的lib里面吗?
String driverClass=sc.getInitParameter("driverClass");
2.如果正确,就是jar包没找到
F:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\sy\WEB-INF下 web.xml内容:
<?xml version="1.0" encoding="gb2312"?>
<web-app version="2.5"><context-param>
<param-name>driverClass</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param><context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/student</param-value>
</context-param><context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param><context-param>
<param-name>password</param-name>
<param-value>12345678</param-value>
</context-param><servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>first.second.three.loginServlet</servlet-class>
</servlet><servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping></web-app>
HTTP Status 500 -type Exception reportmessagedescription The server encountered an internal error () that prevented it from fulfilling this request.exceptionjavax.servlet.ServletException: Servlet execution threw an exceptionroot causejava.lang.NoClassDefFoundError: java/sql/SQLClientInfoException
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Unknown Source)
com.mysql.jdbc.ConnectionImpl.<clinit>(ConnectionImpl.java:267)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
first.second.three.loginServlet.doGet(loginServlet.java:48)
first.second.three.loginServlet.doPost(loginServlet.java:78)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.
public void service(HttpServletRequest request,
HttpServletResponse response){
try{ String driver="com.mysql.jdbc.Driver";
Class.forName(driver);
String url="jdbc:mysql://127.0.0.1:3306/student";
String user="root";
String pwd="123456789";
Connection con=DriverManager.getConnection(url,user,pwd);
Statement stt=con.createStatement();
先写个简单点的然后再测。