日期:2014-05-18  浏览次数:20959 次

问一下关于jsp连接sqlserver的问题
我是才开始自学这个的     所以问题比较菜       见谅:)
先说一下用的工具   eclipse   +   myeclipse   +   sql   server   2000(sp4)   +   tomcat   6   +   windows   XP     (因为是自己学,所以用的XP)
首先,tomcat是配置好的,jsp页面可以访问,jdbc也是安装了的,jar包也分别放到了tomcat/lib,%java_home%/jre/lib/ext以及项目的web-inf目录下,1433端口已经打开,问题就在于jsp连数据库。安装,建立数据库时全是默认设置。
一下是我的代码:(照书上写的)
<%@   page   contentType= "text/html;charset=gb2312 "%>  
<%@   page   import= "java.sql.* "%>  
<html>  
<body>  
<%Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();  
String   url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testdb ";  
//pubs为你的数据库的  
String   user= "sa ";  
String   password= " ";  
Connection   conn=   DriverManager.getConnection(url,user,password);  
Statement   stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
String   sql= "select   *   from   test ";  
ResultSet   rs=stmt.executeQuery(sql);  
while(rs.next())   {%>  
您的第一个字段内容为: <%=rs.getString(1)%>  
您的第二个字段内容为: <%=rs.getString(2)%>  
<%}%>  
<%out.print( "数据库操作成功,恭喜你 ");%>  
<%rs.close();  
stmt.close();  
conn.close();  
%>  
</body>  
</html>  

访问jsp页面时报错:
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   /testsql.jsp   at   line   10

7:   //pubs为你的数据库的  
8:   String   user= "sa ";  
9:   String   password= " ";  
10:   Connection   conn=   DriverManager.getConnection(url,user,password);  
11:   Statement   stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
12:   String   sql= "select   *   from   test ";  
13:   ResultSet   rs=stmt.executeQuery(sql);  


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:515)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:408)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root   cause  

javax.servlet.ServletException:   java.sql.SQLException:   [Microsoft][SQLServer   2000   Driver   for   JDBC][SQLServer]用户   'sa '   登录失败。
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:855)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:784)
org.apache.jsp.testsql_jsp._jspService(testsql_jsp.java:98)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
j