首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

Jsp+SQLServer200连接数据库失败,该如何处理

2013-09-11 
Jsp+SQLServer200连接数据库失败我连接数据库的程序如下:publicConnectiongetConnection(){Stringuser s

Jsp+SQLServer200连接数据库失败
我连接数据库的程序如下:
public   Connection   getConnection(){
        String   user= "sa ";
String   pwd= " ";

String   classforname= "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
String   url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xscj ";
try{
Class.forName(classforname);
                        con=DriverManager.getConnection(url,user,pwd);
                          stmt=con.createStatement();

}catch(SQLException   e){   e.printStackTrace();}
catch(ClassNotFoundException   e){e.printStackTrace();}

return   con;
          }
说明:我的数据库名是xscj,数据库用户sa   ,没有密码。结果程序执行到   con=DriverManager.getConnection(url,user,pwd);     这句后就抛出异常:

type   Exception   report

message  

description   The   server   encountered   an   internal   error   ()   that   prevented   it   from   fulfilling   this   request.

exception  

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root   cause  

java.lang.NullPointerException
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:159)
bookshop.login_confirm.doError(login_confirm.java:78)
bookshop.login_confirm.doPost(login_confirm.java:28)
org.apache.jsp.login_005fconfirm_jsp._jspService(login_005fconfirm_jsp.java:55)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note   The   full   stack   trace   of   the   root   cause   is   available   in   the   Apache   Tomcat/5.0.27   logs.

请各位多多关照,这是什么原因?       急急急急急急

[解决办法]
你有没有将sqlserver的驱动加到lib库中啊?
[解决办法]
不知道仁兄解决没有,我出现的问题与你不同,共同学习.
我的问题:
源代码为
<%@ page contentType= "text/html;charset=gb2312 " import= "java.sql.* "%>
<head>
<meta http-equiv= "Content-Type " content= "text/html;charset=gb2312 ">
<title> 数据库连接 </title>
</head>
<%
String sql= "SELECT * FROM Test ";
String drivername= "sun.jdbc.odbc.JdbcOdbcDriver ";
String url= "jdbc:odbc:mydb ";
Connection con=null;
Statement state=null;
ResultSet rs=null;
out.print( "JDBC-ODBC桥接器连接MS SQL Server 2000 <br> ");
out.println( " <br> ");
try{
Class.forName(drivername);
con=DriverManager.getConnection(url);
state=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);


rs=state.executeQuery(sql);
while(rs.next()){
int id=rs.getInt(1);
String username=rs.getString(2);
out.print(id+ " ");
out.print(username+ " <br> ");
}
}catch(Exception e){
out.print( "Exception> > "+e.getMessage());
}finally{
if(rs!=null) rs.close();
if(rs!=null) state.close();
if(rs!=null) con.close();
}

%>
出现的错误为:
HTTP Status 500 -

--------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:867)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:800)
org.apache.jsp.test_jsp._jspService(test_jsp.java:89)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
com.microsoft.jdbc.sqlserver.tds.TDSConnection. <init> (Unknown Source)
com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
java.sql.DriverManager.getConnection(DriverManager.java:512)
java.sql.DriverManager.getConnection(DriverManager.java:171)
org.apache.jsp.test_jsp._jspService(test_jsp.java:56)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs.


--------------------------------------------

Apache Tomcat/5.0.19
已经安装sql server 2000 jdbc 驱动


[解决办法]
楼上的错误可能是你的String url= "jdbc:odbc:mydb ";这一句有错误.因为无法创建连接
楼主你确信是这一句抛异常吗?空指针异常,是你的对象没有实力化就去操作,看不出来是数据库连接的问题,如果真是这样的话,你最好再看看你的驱动
[解决办法]
WebRoot\WEB-INF\lib
将msbase.jar
mssqlserver.jar
msutil.jar这三个类包放进去,一般都是这个问题
[解决办法]
1.加驱动(3个jar包)
2.将SQLServer2000的验证模式 改为混合模式
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html

热点排行