日期:2014-05-17  浏览次数:20736 次

有关java中数据的问题
package com.struts2.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.struts2.bean.userInfo;
import com.struts2.db.DBConnection;
public class userDao {

private Connection conn;
private Statement stmt;
private ResultSet rs;
private DBConnection db;

public userDao()throws Exception{

db = new DBConnection();

}

public boolean doCheckUser(userInfo user) throws Exception{

String sql = "select * from users where username = '" + user.getUsername()+"' and userpwd = '" + user.getUserpwd() +"'";
System.out.println(sql);
conn = db.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);

int count = 0;
while(rs.next()){
count++;
}
rs.close();
stmt.close();
db.closeConn();
return count==1? true : false;
}

public List<userInfo> doGetAll() throws Exception{

List <userInfo> list = new ArrayList<userInfo>();

String sql = "select * from users";

conn = db.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
System.out.println(sql);
while(rs.next()){
userInfo user = new userInfo();
user.setId(String.valueOf(rs.getInt(1)));
user.setUsername(rs.getString(2));
user.setUserpwd(rs.getString(3));
list.add(user);
}

rs.close();
stmt.close();
db.closeConn();
return list;
}
}
在运行时报
HTTP Status 500 - 

--------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
com.microsoft.jdbc.base.BaseConnection.validateClosedState(Unknown Source)
com.microsoft.jdbc.base.BaseConnection.createStatement(Unknown Source)
com.microsoft.jdbc.base.BaseConnection.createStatement(Unknown Source)
com.struts2.dao.userDao.doGetAll(userDao.java:47)
com.struts2.action.LoginAction.execute(LoginAction.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.