jsp数据库连接出错
用jsp写网页,用了 javabean servlet,连接数据库,为什么回出现这个错误呢?
java.sql.SQLException: No suitable driver found for jdbc:sqlservler://localhost:1433;DatabaseName=servlet
检查了好几遍,代码正确,jar包 是有用的,但是不知道为什么回出现这个错误!求高手提示啊!
这个是代码(servlet)
package myservlet.control;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
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 mybean.data.Login;
public class HandleLogin extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public String handleString(String s) {
byte bb[];
try {
bb = s.getBytes("gb2312");
s = new String(bb);
} catch (UnsupportedEncodingException e) {
}
return s;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
Connection con;
PreparedStatement sql;
Login loginBean = null;
String backNews = "";
HttpSession session = request.getSession(true);
loginBean = (Login) session.getAttribute("login");
try {
if (loginBean == null) {
loginBean = new Login();
session.setAttribute("login", loginBean);
}
} catch (Exception e) {
loginBean = new Login();
session.setAttribute("login", loginBean);
}
String logname = request.getParameter("logname").trim();
String password = request.getParameter("password").trim();
boolean ok = loginBean.getSuccess();
logname = handleString(logname);
password = handleString(password);
if (ok == true && logname.equals(loginBean.getLogname())) {
backNews = logname + "已经登录了!";
loginBean.setBackNews(backNews);
} else {
String url = "jdbc:sqlservler://localhost:1433;DatabaseName=servlet";
boolean boo = (logname.length() > 0) && (password.length() > 0);
try {
con = DriverManager.getConnection(url, "sa", "123456");
String condition = "select * from member where logname=? and password=?";
sql = con.prepareStatement(condition);
if (boo) {
sql.setString(1, logname);
sql.setString(2, password);
ResultSet rs = sql.executeQuery();
boolean m = rs.next();
if (m == true) {
loginBean.setBackNews(backNews);
loginBean.setSuccess(true);
loginBean.setLogname(logname);
} else {
backNews = "您输入的用户不存在或密码不匹配";
loginBean.setBackNews(backNews);
loginBean.setSuccess(false);
loginBean.setLogname(logname);
loginBean.setPassword(password);
}
rs.close();
con.close();
}
} catch (SQLException e) {
backNews = "" + e;
loginBean.setBackNews(backNews);
loginBean.setSuccess(false);
}
}
RequestDispatcher dispatcher = request
.getRequestDispatcher("welcome.jsp");
dispatcher.forward(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
[解决办法]
String url = "jdbc:sqlservler://localhost:1433;DatabaseName=servlet";
你这URL 写错了吧
[解决办法]
CLASS.FORNAME("com.microsoft.sqlserver.jdbc.SQLServerDriver") 忘了吧?