package dbconn ;
import java.sql.* ;
// 接数据库、关闭数据库
public class DbConnection
{
private final String DBDRIVER = "sun.jdbc.odbc.JdbcOdbcDriver" ;
private final String DBURL = "jdbc:odbc:logistics" ;
private final String DBUSER = "admin3" ;
private final String DBPASSWORD = "admin3" ;
private static Connection conn = null ;
public DbConnection()
{
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
}
catch (Exception e)
{
}
}
// 取得数据库连接
public static Connection getConnection()
{
return conn ;
}
// 关闭数据库连接
public static void close()
{
try
{
conn.close() ;
}
catch (Exception e)
{
}
}
};
////////////////////////////////////////////////////////
package wh;
import java.sql.*;
import dbconn.*;
//把从表corp中的corp_name字段全部存放到数组str中,并返回这个数组
public class WhCorp {
public String[]updateUser( ){
String []str = null;
Statement stmt=null;
try{
stmt = DbConnection.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(" select corp_name from corp ");
ResultSet rsCount =stmt.executeQuery("select count(*) from corp");
rsCount.next();
int tc = rsCount.getInt(1); //得到记录的个数
for(int i=0;i<tc;i++){
str[i]=(String) rs.getObject(i+1); //通过循环存如str中
}
rsCount.close();
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
finally{
DbConnection.close();
}
return str;
}
}
/////////////////////////////////////
然后我在jsp里调用第二个javabean说我数组里什么也没有,直接空指针错误.
[此贴子已经被作者于2007-5-29 23:06:54编辑过]