能把你的“jsp 怎么样把数据库中查询结果存入数组”代码发给我吗,或者怎么弄也行,谢谢,lym841@yahoo.cn
展开全部
下面是用 j2ee 5.0,假设数据库中查询结果是String
public class ConnectionManager {
private static final String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=数据库名";
private static final String user = "登录名";
private static final String password = "密码";
/**
* 返回数据库连接
*
* @return dbConnection
*/
public static synchronized Connection getConnection(){
Connection dbConnection = null;
try{
Class.forName(driverClassName);
dbConnection = DriverManager.getConnection(url,user,password);
}catch(Exception e){
e.printStackTrace();
}
return dbConnection;
}
/**
* 释放资源
* @param conn 数据库连接
* @param pstmt PreParedStatement对象
* @param rs 结果集
*/
public static void closeAll(Connection conn,Statement stmt,PreparedStatement pstmt,ResultSet rs){
/*如果rs不空,关闭rs*/
if(rs!=null){
try{
rs.close();
}catch(SQLException e){
//输出异常
}
}
/*如果stmt不空,关闭stmt*/
if(stmt!=null){
try{
stmt.close();
}catch(SQLException e){
//输出异常
}
}
/*如果pstmt不空,关闭pstmt*/
if(pstmt!=null){
try{
pstmt.close();
}catch(SQLException e){
//输出异常
}
}
/*如果conn不空,关闭conn*/
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
//输出异常
}
}
}
}
public List<String> insertArray(){
private Connection conn=ConnectionManager.getConnection();
private PreparedStatement pstmt=null;
private ResultSet rs=null;
String sql = "select 列名 from 表名";
try{
rs= command.executeQuery(); // 读取数据
for(int i=0;i<result.getRowCount();i++){
String str = (rs.getString("列名"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
conn.close();
pstmt.close();
rs.close();
}
return strs;
}
public class ConnectionManager {
private static final String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=数据库名";
private static final String user = "登录名";
private static final String password = "密码";
/**
* 返回数据库连接
*
* @return dbConnection
*/
public static synchronized Connection getConnection(){
Connection dbConnection = null;
try{
Class.forName(driverClassName);
dbConnection = DriverManager.getConnection(url,user,password);
}catch(Exception e){
e.printStackTrace();
}
return dbConnection;
}
/**
* 释放资源
* @param conn 数据库连接
* @param pstmt PreParedStatement对象
* @param rs 结果集
*/
public static void closeAll(Connection conn,Statement stmt,PreparedStatement pstmt,ResultSet rs){
/*如果rs不空,关闭rs*/
if(rs!=null){
try{
rs.close();
}catch(SQLException e){
//输出异常
}
}
/*如果stmt不空,关闭stmt*/
if(stmt!=null){
try{
stmt.close();
}catch(SQLException e){
//输出异常
}
}
/*如果pstmt不空,关闭pstmt*/
if(pstmt!=null){
try{
pstmt.close();
}catch(SQLException e){
//输出异常
}
}
/*如果conn不空,关闭conn*/
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
//输出异常
}
}
}
}
public List<String> insertArray(){
private Connection conn=ConnectionManager.getConnection();
private PreparedStatement pstmt=null;
private ResultSet rs=null;
String sql = "select 列名 from 表名";
try{
rs= command.executeQuery(); // 读取数据
for(int i=0;i<result.getRowCount();i++){
String str = (rs.getString("列名"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
conn.close();
pstmt.close();
rs.close();
}
return strs;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询