我用的是纯驱动连接SQL server数据库,运行的时候显示第128行错误,不知道怎么改。求帮助。

package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.*;
import toolbean.*;
public class regservlet extends HttpServlet {
/**
*
/
private static final long serialVersionUID = 1L;
/
*
* Destruction of the servlet.

/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/
*
* The doGet method of the servlet.

*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);}
/
*
* The doPost method of the servlet.

*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
ValueBean vb=new ValueBean();
vb.setUsername(MyTools.toChinese(request.getParameter("username")));
vb.setUserpass(request.getParameter("userpass"));
vb.setBirthdate(request.getParameter("birthdate"));
vb.setGender(MyTools.toChinese(request.getParameter("gender")));
vb.setNativeplace(MyTools.toChinese(request.getParameter("nativeplace")));
vb.settype(MyTools.toChinese(request.getParameter("type")));

String url="jdbc:sqlserver://127.0.0.1:1433;DataBaseName=lkk";
String user="root";
String pwd="123";
String sql="";

Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
sql="select username from user where username='"+vb.getUsername()+"'";
conn=DriverManager.getConnection(url, user, pwd);
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
HttpSession session=request.getSession();
if(rs.next()){
PrintWriter pw=response.getWriter();
pw.write("alert('用户名已存在,请重新注册')");
response.sendRedirect("reg.jsp?err=1");
}else{
StringBuffer newsql=new StringBuffer();
newsql.append("insert into user (username,userpass,gender,birthdate,nativeplace,type) values ('");
newsql.append(vb.getUsername());
newsql.append("','");
newsql.append(vb.getUserpass());
newsql.append("','");
newsql.append(vb.getGender());
newsql.append("','");
newsql.append(vb.getBirthdate());
newsql.append("','");
newsql.append(vb.getNativeplace());
newsql.append("','");
newsql.append("会员");
newsql.append("')");
stmt.executeUpdate(newsql.toString());

session.setAttribute("msg_ok","注册成功!");
session.setAttribute("username",vb.getUsername());
response.sendRedirect("regMsg.jsp"); }

}catch(Exception e){
out.print(e.toString());
}finally{
try{rs.close();}catch(Exception ee){}
try{stmt.close();}catch(Exception ee){}
try{conn.close();}catch(Exception ee){} } }
/
*
* Initialization of the servlet.

*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {

try{
Class.forName("com.microsoft.jdbc.SQLServerDriver");

}catch (Exception e){
throw new ServletException("加载数据库驱动失败!");//这是第128行
} }}
错误描述:
javax.servlet.ServletException: 加载数据库驱动失败!
servlets.regservlet.init(regservlet.java:128)
javax.servlet.GenericServlet.init(GenericServlet.java:212)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
java.lang.Thread.run(Thread.java:619)

2个回答

sinat_36953548
一只喵喵怪 谢谢
大约 3 年之前 回复

很明显是加载数据库驱动失败

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问