javabean的源文件voteconn.java如下:
package vote1;
import java.sql.*;
public class voteconn 
{

public static final String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
public static final String sConnStr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=school";
    public static final String user="sa";
public static final String password="123456";
Connection connect=null;
ResultSet rs=null;
public voteconn()
{
try
{
Class.forName(sDBDriver);
}
catch (java.lang.ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
try
{
connect=DriverManager.getConnection(sConnStr,user,password);
Statement stmt=connect.createStatement();
rs=stmt.executeQuery(sql);
System.out.println("连接数据库成功");
return rs;
}
catch (SQLException ex)
{
System.err.println(ex.getMessage());
System.out.println("发生异常啦");
return null;
}
}
public int executeUpdate(String sql)
{
int result=0;
try
{
    connect=DriverManager.getConnection(sConnStr);
    Statement stmt=connect.createStatement();
    result=stmt.executeUpdate(sql);
return result;
}
catch (SQLException ex)
{
System.err.println(ex.getMessage());
return result;
}
}
}
将以上的源码编译成功后,把voteconn.class放到Tomcat 5.5\webapps\ROOT\WEB-INF\classes\vote1\voteconn.class目录,而我的jsp文件是放在Tomcat 5.5\webapps\ROOT\vote1\index.jsp目录的index.jsp如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<jsp:useBean id="ConnDbBean" scope="page" class="vote.voteconn"/><!--注意最后的一斜线要补上-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>网上调查表</title>
<style type="text/css">
<!--
.style1 {
font-size: 18px;
font-weight: bold;
}
-->
</style>
</head><body>
 <div align="center">
   <p><span class="style1">请您投票</span>:</p>
   <%ResultSet rs=ConnDbBean.executeQuery("select * from vote1");%><!--sql语句注意不要写错,否则老是返回空值-->
   <table width="150" border="1">
   <form action="vote.jsp" >
   <%
     while(rs.next()){%>
     <tr>
       <td><input type="radio" name="type" value=<%=rs.getString("id")%>><%=rs.getString("note")%></td>
     </tr>
 <%}
   rs.close();
 %>
   <tr align="center"><td><input type="submit" value="投票"></td></tr>
 </form>
 <tr><td><a href="details.jsp">查看投票</a></td></tr>
   </table>
 </div>
</body>
</html>在浏览器中执行jsp文件后报错,错误如下:
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.lang.NullPointerException
org.apache.jsp.vote1.index_jsp._jspService(org.apache.jsp.vote1.index_jsp:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.15 logs.
--------------------------------------------------------------------------------Apache Tomcat/5.5.15
实在搞不懂是什么原因,而我用一另外一段代码却可以正然访问数据库,
并有返回值,代码Test1.java如下:
// Java Document
import java.sql.*;
public class Test1
{
public static final String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
public static final String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=school";
public static final String user="sa";
public static final String password="123456";
public static void main(String[] args)
{
String queryString="SELECT * FROM vote1";//SQL语句
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try
{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(url,user,password);
st=conn.createStatement();
rs=st.executeQuery(queryString);
while(rs.next())
{
System.out.println("ID:"+rs.getString("id")+"company:"+rs.getString("note")+"  vote:"+rs.getString("c_num"));
}
rs.close();
st.close();
conn.close();
}
catch (Throwable t)
{
t.printStackTrace(System.out);
}
}
}既然是这样,那么我的数据库的设置方面都应该没有错的吧,那代码又错在哪里呢,我反复看过了,
实在不知道呀,怎么返回经常为空呢,

解决方案 »

  1.   

    那就是有的时候数据库连不上,把数据库重新装一下,必须打上补丁
    在javabean中加一个main看是不是好了,好了以后再放连JSP文件
      

  2.   

    还没搞定呀,javabean的源文件voteconn.java如下:
    package vote1;    //你这里是vote1,而你在jsp中用了vote.voteconn,而应该是vote1.voteconn
    import java.sql.*;index.jsp
    <jsp:useBean id="ConnDbBean" scope="page" class="vote.voteconn"/>
    如果建了vote1项目:  //建包并不代表你建项目。建与不建项目,文件放的路径是不一样的
       目录结构:
            webapps
               |
               |--ROOT  //如果没有建vote1项目:
                    |
                    |--web.xml
                    |--index.jsp
                    |--WEB-INF
                          |-classes
                                |-vote1
                                    |- voteconn.class
               |--vote1  //如果建了vote1项目:
                    |
                    |--web.xml
                    |--index.jsp
                    |--WEB-INF
                          |-classes
                                |-vote1
                                    |- voteconn.class
      

  3.   

    我在javabean里放了个main了,
    编译通过,可是执行的时候又是空指针异常呀,不懂中
    程序如下:
    //package vote1;
    import java.sql.*;
    public class voteconn 
    {

    public static final String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
    public static final String sConnStr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=school";
        public static final String user="sa";
    public static final String password="123456";
    Connection connect=null;
    ResultSet rs=null;
    public voteconn()
    {
    try
    {
    Class.forName(sDBDriver);
    }
    catch (java.lang.ClassNotFoundException e)
    {
    System.err.println(e.getMessage());
    }
    }
    public ResultSet executeQuery(String sql)
    {
    try
    {
    connect=DriverManager.getConnection(sConnStr,user,password);
    Statement stmt=connect.createStatement();
    rs=stmt.executeQuery(sql);
    System.out.println("连接数据库成功");
    return rs;
    }
    catch (SQLException ex)
    {
    System.err.println(ex.getMessage());
    System.out.println("发生异常啦");
    return null;
    }
    }
    public int executeUpdate(String sql)
    {
    int result=0;
    try
    {
        connect=DriverManager.getConnection(sConnStr);
        Statement stmt=connect.createStatement();
        result=stmt.executeUpdate(sql);
    return result;
    }
    catch (SQLException ex)
    {
    System.err.println(ex.getMessage());
    return result;
    }
    }
    public static void main(String[] args)
    {
    voteconn ConnDbBean=null;
    ResultSet rs=ConnDbBean.executeQuery("select * from vote1");
    try
    {
    while(rs.next())
    {System.out.println("ID:"+rs.getString("id")+"name:"+rs.getString("note"));}
    rs.close();
    }
    catch (Throwable t)
    {
    t.printStackTrace(System.out);
    }

    }
    }执行java voteconn后提示如下:
     ----------java ----------
    Exception in thread "main" java.lang.NullPointerException
    at voteconn.main(voteconn.java:59)输出完成 (耗时 0 秒) - 正常终止麻烦大家帮我分析下吧
      

  4.   

    重复一下,
    而我用一另外一段代码却可以正然访问数据库,
    并有返回值,代码Test1.java如下:
    // Java Document
    import java.sql.*;
    public class Test1
    {
    public static final String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
    public static final String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=school";
    public static final String user="sa";
    public static final String password="123456";
    public static void main(String[] args)
    {
    String queryString="SELECT * FROM vote1";//SQL语句
    Connection conn=null;
    Statement st=null;
    ResultSet rs=null;
    try
    {
    Class.forName(sDBDriver);
    conn=DriverManager.getConnection(url,user,password);
    st=conn.createStatement();
    rs=st.executeQuery(queryString);
    while(rs.next())
    {
    System.out.println("ID:"+rs.getString("id")+"company:"+rs.getString("note")+"  vote:"+rs.getString("c_num"));
    }
    rs.close();
    st.close();
    conn.close();
    }
    catch (Throwable t)
    {
    t.printStackTrace(System.out);
    }
    }
    }
      

  5.   

    放错位置了吧,你的voteconn.class好像应该放在vote1\WEB-INF\classes\下面
      

  6.   

    把voteconn.class放到Tomcat 5.5\webapps\ROOT\WEB-INF\classes\vote1\voteconn.class目录,而我的jsp文件是放在Tomcat 5.5\webapps\ROOT\vote1\index.jsp目录的index.jsp如下:
    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
    <jsp:useBean id="ConnDbBean" scope="page" class="vote.voteconn"/>
    ----------------------------以上是楼主自己的话
    你上面是vote1,下面是vote啊,粗心啊
      

  7.   

    我对那个jdbc驱动做了一个压力测试,在访问稍微多点的时候,就会出现连接不上数据库的问题,而使用asp或者.net却没有什么问题,看来ms东西,还是要使用ms的开发语言来做比较好.
      

  8.   

    复楼上的楼上,我用Access数据库的时候,是要把voteconn.class放到Tomcat 5.5\webapps\ROOT\WEB-INF\classes\vote1\voteconn.class目录的,如果把voteconn.class放到Tomcat 5.5\webapps\ROOT\vote1\WEB-INF\classes\vote1\voteconn.class目录,结果都会报错,
    所以我想我放的位置应该没有错吧再复楼上的,我的那个<jsp:useBean id="ConnDbBean" scope="page"class="vote.voteconn"/>
    只是笔误,我改过来的时候也是不行的
      

  9.   

    没人帮我的,
    我自己搞定了
    是在classpath里设置的驱动路径tomcat不认帐的
    自己把那三个驱动文件放到common\lib里后,
    就可以了自己结贴啦