我连接数据库的程序如下:
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);