我在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 秒) - 正常终止麻烦大家帮我分析下吧,
而我用一另外一段代码却可以正然访问数据库,
并有返回值,代码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);
}
}
}sql server 数据库打上了sp3补丁了,还下载安装了jdbc驱动,并设置了环境变量
编译通过,可是执行的时候又是空指针异常呀,不懂中
程序如下:
//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 秒) - 正常终止麻烦大家帮我分析下吧,
而我用一另外一段代码却可以正然访问数据库,
并有返回值,代码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);
}
}
}sql server 数据库打上了sp3补丁了,还下载安装了jdbc驱动,并设置了环境变量
{
voteconn ConnDbBean= new voteconn();//修改
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);
}
}
}
但是你能不能帮我看下为什么用javabean连接数据库的时候却会出错呢,voteconn.java只是上面的程序去掉了main
//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="vote1.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>执行后浏览器报错信息如下:
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: Exception in JSP: /vote1/index.jsp:2421: <table width="150" border="1">
22: <form action="vote.jsp" >
23: <%
24: while(rs.next()){%>
25: <tr>
26: <td><input type="radio" name="type" value=<%=rs.getString("id")%>><%=rs.getString("note")%></td>
27: </tr>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
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
25: <tr>
26: <td><input type="radio" name="type" value=<%=rs.getString("id")%>><%=rs.getString("note")%></td>
27: </tr>
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";
public voteconn()
{
try
{
Class.forName(sDBDriver);
}
catch (java.lang.ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
Connection connect=null;
ResultSet rs=null;
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)
{
Connection connect=null;
ResultSet rs=null;
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;
}
}
}
报错为: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: Exception in JSP: /vote1/index.jsp:2926: <td><input type="radio" name="type" value=<%=rs.getString("id")%>><%=rs.getString("note")%></td>
27: </tr>
28: <%}
29: rs.close();
30: %>
31: <tr align="center"><td><input type="submit" value="投票"></td></tr>
32: </form>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
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:89)
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
rs.close();
}
你教下我
我用的是tomcat 5.5
2006-4-16 0:09:48 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Java\jdk1.5.0_04\bin;C:\Program Files\Microsoft SQL Server\80\Tools\BINN
2006-4-16 0:09:49 org.apache.coyote.http11.Http11BaseProtocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2006-4-16 0:09:49 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 2203 ms
2006-4-16 0:09:49 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2006-4-16 0:09:49 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/5.5.15
2006-4-16 0:09:49 org.apache.catalina.core.StandardHost start
信息: XML validation disabled
2006-4-16 0:09:52 org.apache.coyote.http11.Http11BaseProtocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2006-4-16 0:09:52 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2006-4-16 0:09:52 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/156 config=null
2006-4-16 0:09:52 org.apache.catalina.storeconfig.StoreLoader load
信息: Find registry server-registry.xml at classpath resource
2006-4-16 0:09:52 org.apache.catalina.startup.Catalina start
信息: Server startup in 3094 ms
信息: Pausing Coyote HTTP/1.1 on http-8080
2006-4-16 0:54:02 org.apache.catalina.core.StandardService stop
信息: Stopping service Catalina
2006-4-16 0:54:02 org.apache.coyote.http11.Http11BaseProtocol destroy
信息: Stopping Coyote HTTP/1.1 on http-8080
2006-4-16 0:54:02 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: Failed shutdown of Apache Portable Runtime
2006-4-16 0:54:09 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Java\jdk1.5.0_04\bin;C:\Program Files\Microsoft SQL Server\80\Tools\BINN
2006-4-16 0:54:09 org.apache.coyote.http11.Http11BaseProtocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2006-4-16 0:54:09 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1312 ms
2006-4-16 0:54:09 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2006-4-16 0:54:09 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/5.5.15
2006-4-16 0:54:09 org.apache.catalina.core.StandardHost start
信息: XML validation disabled
2006-4-16 0:54:11 org.apache.coyote.http11.Http11BaseProtocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2006-4-16 0:54:11 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2006-4-16 0:54:11 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/63 config=null
2006-4-16 0:54:11 org.apache.catalina.storeconfig.StoreLoader load
信息: Find registry server-registry.xml at classpath resource
2006-4-16 0:54:11 org.apache.catalina.startup.Catalina start
信息: Server startup in 2547 ms
2006-4-16 1:40:24 org.apache.coyote.http11.Http11AprProtocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2006-4-16 1:40:24 org.apache.coyote.ajp.AjpAprProtocol init
信息: Initializing Coyote AJP/1.3 on ajp-8009
2006-4-16 1:40:24 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1375 ms
2006-4-16 1:40:25 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2006-4-16 1:40:25 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/5.5.15
2006-4-16 1:40:25 org.apache.catalina.core.StandardHost start
信息: XML validation disabled
2006-4-16 1:40:27 org.apache.coyote.http11.Http11AprProtocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2006-4-16 1:40:27 org.apache.coyote.ajp.AjpAprProtocol start
信息: Starting Coyote AJP/1.3 on ajp-8009
2006-4-16 1:40:27 org.apache.catalina.storeconfig.StoreLoader load
信息: Find registry server-registry.xml at classpath resource
2006-4-16 1:40:27 org.apache.catalina.startup.Catalina start
信息: Server startup in 2484 ms
现在我又不知道程序怎么会出错了