当前位置: 技术问答>java相关
调用javavbean时出现了异常??请大虾进来看看!!
来源: 互联网 发布时间:2017-04-11
本文导语: java.lang.NullPointerException dbcon bean: package dc; import java.sql.*; import javax.servlet.http.*; import java.io.*; public class dbcon implements HttpSessionBindingListener{ private Connection con=null; public dbcon(){ BuildConnection(); } private void B...
java.lang.NullPointerException
dbcon bean:
package dc;
import java.sql.*;
import javax.servlet.http.*;
import java.io.*;
public class dbcon implements HttpSessionBindingListener{
private Connection con=null;
public dbcon(){
BuildConnection();
}
private void BuildConnection(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:test");
}
catch(Exception ex){
System.out.print(ex.toString());
}
}
public Connection getConnection(){
if(con==null)
BuildConnection();
return this.con;
}
public void close()
{
try{
con.close();
con=null;
}
catch(SQLException sex){
System.out.println(sex.toString());
}
}
public void valueBound(HttpSessionBindingEvent event){}
public void valueUnbound(HttpSessionBindingEvent event){
if(con!=null)
close();
}
}
votebean:
package dc;
import java.sql.*;
public class VoteBean{
private Connection con=null;
private Statement stmt=null;
private ResultSet BookRs=null;
public VoteBean(){
}
public void InitBookRs(Connection con)
{
try{
this.con=con;
stmt=this.con.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
BookRs=stmt.executeQuery("select * from uin");
}
catch(SQLException sex){
System.out.println(sex.toString());
}
}
public void CloseBookRs(){
try{
if(BookRs!=null){
BookRs.close();
BookRs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
}
catch(SQLException sex){
System.out.println(sex.toString());
}
}
public boolean next(){
boolean result=false;
try{
result=BookRs.next();
}
catch(SQLException sex){
System.out.println(sex.toString());
}
return result;
}
public boolean login(Connection con,String un,String pw){
boolean result=false;
String sql="select * from uin where un='"+un+"' and pw='"+pw+"'";
try{
this.con=con;
Statement lstmt=null;
lstmt=this.con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs=lstmt.executeQuery(sql);
result=rs.next();
rs.close();
}
catch(SQLException sex){
System.out.println(sex.toString());
}
return result;
}
}
jsp调用:
excute
dbcon bean:
package dc;
import java.sql.*;
import javax.servlet.http.*;
import java.io.*;
public class dbcon implements HttpSessionBindingListener{
private Connection con=null;
public dbcon(){
BuildConnection();
}
private void BuildConnection(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:test");
}
catch(Exception ex){
System.out.print(ex.toString());
}
}
public Connection getConnection(){
if(con==null)
BuildConnection();
return this.con;
}
public void close()
{
try{
con.close();
con=null;
}
catch(SQLException sex){
System.out.println(sex.toString());
}
}
public void valueBound(HttpSessionBindingEvent event){}
public void valueUnbound(HttpSessionBindingEvent event){
if(con!=null)
close();
}
}
votebean:
package dc;
import java.sql.*;
public class VoteBean{
private Connection con=null;
private Statement stmt=null;
private ResultSet BookRs=null;
public VoteBean(){
}
public void InitBookRs(Connection con)
{
try{
this.con=con;
stmt=this.con.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
BookRs=stmt.executeQuery("select * from uin");
}
catch(SQLException sex){
System.out.println(sex.toString());
}
}
public void CloseBookRs(){
try{
if(BookRs!=null){
BookRs.close();
BookRs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
}
catch(SQLException sex){
System.out.println(sex.toString());
}
}
public boolean next(){
boolean result=false;
try{
result=BookRs.next();
}
catch(SQLException sex){
System.out.println(sex.toString());
}
return result;
}
public boolean login(Connection con,String un,String pw){
boolean result=false;
String sql="select * from uin where un='"+un+"' and pw='"+pw+"'";
try{
this.con=con;
Statement lstmt=null;
lstmt=this.con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs=lstmt.executeQuery(sql);
result=rs.next();
rs.close();
}
catch(SQLException sex){
System.out.println(sex.toString());
}
return result;
}
}
jsp调用:
excute
|
一,测试数据库是否连接成功;
二,测试rs是否为null
二,测试rs是否为null
|
可以用System.out.println 大法,定位在那里出的异常···就好解决了