package com.kczyw.classmanagement.bll;import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;import com.kczyw.classmanagement.comm.Comm;
import com.kczyw.classmanagement.comm.Const;
import com.kczyw.classmanagement.dao.DbHelper;
import com.kczyw.classmanagement.model.T_Class;public class ClassBll { DbHelper dbHelper = new DbHelper(Const.getHost(), Const.getDbName(),
Const.getPort(), Const.getUserName(), Const.getPasswd()); private List<T_Class> parseToClass(ResultSet rs) {
List<T_Class> lst_class = null;
try {
if (rs == null){
System.out.println("rs is noe");
return null;
}
else {
lst_class = new ArrayList<T_Class>();
System.out.println("开始转化");

while (rs.next()) {

System.out.println("班级第"+rs.getRow());
T_Class tc = new T_Class();
tc.setCreateDate(rs.getDate("createDate"));
tc.setGrade(rs.getString("grade"));
tc.setId(rs.getInt("id"));
tc.setName(rs.getString("name"));
lst_class.add(tc);
}
} } catch (Exception ex) {
}
return lst_class;
} public void insertClass(T_Class tc) {
try {
String sql = "insert into t_class(name,createDate,grade)values(?,now(),?)"; dbHelper.createConnection(); java.sql.Connection con = dbHelper.getConn();
if (con != null) {
System.out.println("T not null");
} else {
System.out.println("T is null");
}
// dbHelper.executeUpdate(sql); PreparedStatement pstmt = dbHelper.getConn().prepareStatement(sql);
Statement stmt = dbHelper.getConn().createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE); pstmt.setString(1, tc.getName());
pstmt.setString(2, tc.getGrade());
dbHelper.executeUpdate(pstmt);
} catch (Exception ex) {
Comm.writeLog(ex, "");
} finally {
dbHelper.close();
}
} public void deleteClass(T_Class tc) {
String sql = null;
try {
if (tc != null) {
dbHelper.createConnection();
PreparedStatement pstmt = null;
if (tc.getGrade() != null) {
sql = "delete from T_class where grade=?"; pstmt = dbHelper.getConn().prepareStatement(sql);
pstmt.setString(1, tc.getGrade());
dbHelper.executeUpdate(pstmt);
} else if (tc.getId() > 0) {
sql = "delete from t_class where id=?";
pstmt = dbHelper.getConn().prepareStatement(sql);
pstmt.setInt(1, tc.getId());
} else if (tc.getName() != null) {
sql = "delete from t_class where name=?";
pstmt = dbHelper.getConn().prepareStatement(sql);
pstmt.setString(1, tc.getName());
}
dbHelper.executeUpdate(pstmt);
} } catch (Exception ex) { } finally {
dbHelper.close();
}
} public void updateClass(T_Class tc) {
try {
if (tc != null) {
String sql = "update T_class set name=?,createDate=now(),grade=?";
dbHelper.createConnection();
PreparedStatement pstmt = dbHelper.getConn().prepareStatement(
sql);
pstmt.setString(1, tc.getName());
pstmt.setString(2, tc.getGrade());
dbHelper.executeUpdate(pstmt);
}
} catch (Exception ex) {
} finally {
dbHelper.close();
}
} public List<T_Class> getClasses(T_Class tc) {
ResultSet rs = null;
try {
String sql = null;
dbHelper.createConnection();
PreparedStatement pstmt = null;
if (dbHelper.getConn() != null) {
if (tc == null) {
sql = "select * from t_class ";
pstmt = dbHelper.getConn().prepareStatement(sql);
} else {
if (tc.getGrade() != null) {
sql = "select * from t_class where grade = ?";
pstmt = dbHelper.getConn().prepareStatement(sql);
pstmt.setString(1, tc.getName());
} else if (tc.getId() > 0) {
sql = "select * from t_class where id=?";
pstmt = dbHelper.getConn().prepareStatement(sql);
pstmt.setInt(1, tc.getId());
} else if (tc.getName() != null) {
sql = "select * from t_class where name=?";
pstmt = dbHelper.getConn().prepareStatement(sql);
pstmt.setString(1, tc.getName());
}
}
rs = dbHelper.executeScalar(pstmt);
}else{
System.out.println("DataBase not connection");
}
//System.out.println(rs==null?"Empty":rs.getRow());
} catch (Exception ex) { } finally {
dbHelper.close();
}

return parseToClass(rs);
}
}
在ParseToClass这个方法内只能打印到“开始转化”。不能继续往下面输入。
DbHelper是这样的package com.kczyw.classmanagement.dao;import java.sql.*;import com.kczyw.classmanagement.comm.*;public class DbHelper {
private static Connection conn = null;
private String host, dbName, port, passwd, userName;
private Statement st; static {
try {
Class.forName("com.mysql.jdbc.Driver");

} catch (Exception ex) {
Comm.writeLog(ex, "com.kczyw.classmanagement.comm.DbHelper.java");
}
} public Connection getConn() {
return conn;
} public DbHelper(String host, String dbName, String port, String userName,
String passwd) {
this.host = host;
this.dbName = dbName;
this.userName = userName;
this.passwd = passwd;
this.port = port;
//createConnection();
} public synchronized void createConnection() {
try {
String url = "jdbc:mysql://" + host + ":" + port + "/" + dbName
+ "?useUnicode=true&characterEncoding=GB2312";
conn = DriverManager.getConnection(url, userName, passwd);
if (conn == null) {
System.out.println(" null");
} else {
System.out.println("not null");
}
} catch (SQLException ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
Comm.writeLog(ex, "");
}
} public synchronized void close() {
try {
if (conn != null) {
if (!conn.isClosed()) {
conn.close();
}
}
} catch (SQLException ex) {
Comm.writeLog(ex, "");
} } public synchronized ResultSet executeScaler(String sql) {
ResultSet rs = null;
try {
st = conn.createStatement();
rs = st.executeQuery(sql);
} catch (SQLException ex) {
Comm.writeLog(ex, "executeSql");
}
return rs;
} public synchronized Object executeUpdate(String sql) {
Object o = null;
try {
st.executeUpdate(sql);
} catch (SQLException ex) {
Comm.writeLog(ex, "");
}
return o;
} public synchronized int executeUpdate(PreparedStatement p) {
int count = 0;
try {
p.execute();
count = p.getUpdateCount();
} catch (Exception ex) { }
return count;
} public synchronized ResultSet executeScalar(PreparedStatement p) throws SQLException {
ResultSet rs = null;
try {

rs = p.executeQuery();

} catch (Exception ex) {
System.out.println(ex.getMessage());
}

return rs;
}}在DBHelper内是有值的。但是在parseToClass内While(rs.next())就进不去了···高人们啊,快啊 我急啊···