当前位置: 代码迷 >> Eclipse >> 连接数据库时的奇怪有关问题(myeclipse+struts+sqlserver)
  详细解决方案

连接数据库时的奇怪有关问题(myeclipse+struts+sqlserver)

热度:37   发布时间:2016-04-23 14:30:09.0
连接数据库时的奇怪问题(myeclipse+struts+sqlserver)
我写了一个数据库操作类在用.jsp   中的useBean方法调用做测试时总是提示错误。

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
com.is.struts.action.Database.selectDb(Database.java:57)
org.apache.jsp.index_jsp._jspService(index_jsp.java:103)
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.28   logs.

数据库的源码如下,请高手支招:
package   com.is.struts.action;

import   java.sql.*;
//import   javax.swing.*;
//import   java.util.*;

import   java.sql.Connection;
import   java.sql.DriverManager;
import   java.sql.ResultSet;
import   java.sql.SQLException;
import   java.sql.Statement;

public   class   Database   {
Connection   conn=null;
Statement   stmt=null;
ResultSet   rs=null;
String   driverName=null;
String   url=null;
/*
public   Database()   {
    try   {
        jbInit();
    }
    catch   (Exception   ex)   {
        ex.printStackTrace();
    }
}

private   void   jbInit()   throws   Exception   {
}
*/
//连接数据库
public     void     setConnection(){//   throws   Exception{

        try   {
        driverName   =   "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
                url   =   "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test ";
                Class.forName(driverName);
                conn=DriverManager.getConnection(url,   "sa ",   "yt ");
                conn.setAutoCommit(true);
        }
        catch   (ClassNotFoundException   ex)   {
            ex.printStackTrace();
        }
        catch(SQLException   e){
        e.printStackTrace();
        }
}

//数据库查询的操作
public   ResultSet   selectDb(String   sql){//   throws   Exception{
  相关解决方案