日期:2014-05-18  浏览次数:20790 次

为什么我不能调用java bean的有些函数
帮我看下哪里错了,这个javabean编译是通过的,可就是调用closeStmt()和closeConn()出错
1、这是我的javabean程序sdb.java
//   封装数据库连接及一些底层操作
package   jbean;
import   java.sql.*;
import   java.lang.*;
import   java.net.*;
import   java.io.*;
import   java.util.*;

public   class   sdb
{
Connection   conn=null;
ResultSet   rs=null;
Statement   stmt=null;
String   url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sdb ";
String   user= "sa ";
String   password= " ";
public   sdb()
{
try
{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();

  conn=   DriverManager.getConnection(url,user,password);
Statement   stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}

catch(java.lang.Exception   e)
{
System.err.println( "mydb(): "+e.getMessage());
}

}


public   ResultSet   executeQuery(String   sql)
{
rs=null;
try
{
conn=   DriverManager.getConnection(url,user,password);
Statement   stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//String   sql= "select   *   from   Students ";
  rs=stmt.executeQuery(sql);

}
catch(SQLException   ex)
{
System.err.println( "db,excuteQuery: "+ex.getMessage());
System.out.print( "执行有错误: "+ex.getMessage());//输出到客户端
}
return   rs;
}

public   boolean   executeUpdate(String   sql)
//public   ResultSet   executeUpdate(String   sql)
{
boolean   bupdate=false;
rs=null;
try
{
conn=   DriverManager.getConnection(url,user,password);
Statement   stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

////stmt.executeUpdate(sql);
int   rowCount=stmt.executeUpdate(sql);
if(rowCount!=0)   bupdate=true;
}
catch(SQLException   ex)
{
System.err.println( "db,excuteUpdate: "+ex.getMessage());
System.out.print( "执行插入或删除有错误: "+ex.getMessage());//输出到客户端
}
return   bupdate;
//return   rs;
}

public   void   closeStmt()
{
try{
stmt.close();
}catch(SQLException   er)
{
er.printStackTrace();
}
}

public   void   closeConn()
{
try{
conn.close();
}catch(SQLException   er)
{
er.printStackTrace();
}

}

public   static   String   toChinese(String   str)
{
try
{
if(str==null)
{
return   null;
}
else
{
str=   new   String(str.getBytes( "gbk "));  
return   str;
}
}
catch(Exception   e)
{
return   null;
}

}
}