急!,jsp连接SQL Server2008失败,求大神看看
以下是我的代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>通过JDBC驱动访问SQLSever 2008</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<br/><br/><br/>
<h2>使用JDBC驱动访问 SQL Server 2008数据库</h2>
<hr/>
<table border=2 bgcolor="ccceee" align="center">
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>体重</th>
</tr>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databasename=student";
String user = "sa";
String password="root";
Connection conn=DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
String sql = "select * from info";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
%>
<tr>
<td><%=rs.getString("studentNumber") %></td>
<td><%=rs.getString("studentName") %></td>
<td><%=rs.getString("studentSex") %></td>
<td><%=rs.getString("studentAge") %></td>
<td><%=rs.getString("studentWeight") %></td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</table>
<hr/>
</center>
</body>
</html>
----------------------------------------------------------------------------
报错内容:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /accessSQLServer2008.jsp at line 40
37: <th>浣撻噸</th>
38: </tr>
39: <%
40: Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
41: String url = "jdbc:sqlserver://localhost:1433;databasename=student";
42: String user = "sa";
43: String password="root";
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:519)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:410)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.ServletException: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:865)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:794)
org.apache.jsp.accessSQLServer2008_jsp._jspService(accessSQLServer2008_jsp.java:144)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1647)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1493)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:128)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
java.lang.ClassLoader.loadClassInternal(Unknown Source)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Unknown Source)
org.apache.jsp.accessSQLServer2008_jsp._jspService(accessSQLServer2008_jsp.java:98)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
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.30 logs.
Apache Tomcat/6.0.30
[解决办法]
root cause
javax.servlet.ServletException: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
数据库驱动没加,或者没有import进来
[解决办法]
class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 试一下。
[解决办法]
com.microsoft.sqlserver.jdbc.SQLServerDriver