日期:2014-05-20 浏览次数:20982 次
/***
*这是查询部分
*/
private ResultSet select(String flag ,int id , String name){
ResultSet rs = null ;
GetConnection getConn = new GetConnection();
Connection conn ;
Statement stat = null ;
String sql = null ;
if( null == flag ) flag = " * ";
if( id < 0 && null == name)sql = "select "+ flag + " from user_inf " ;
if( id >=0 && null == name)sql = "select "+ flag + " from user_inf where id = " + id ;
if( id < 0 && null != name)sql = "select "+ flag + " from user_inf where name = " + parseSqlDate(name) ;
if( id >=0 && null != name)sql = "select "+ flag + " from user_inf where id = " + id + " and name = " + parseSqlDate(name);
System.out.println(sql);
try{
conn = getConn.open();
stat = conn.createStatement();
rs = stat.executeQuery(sql);
r =rs ;
}
catch(Exception err){err.printStackTrace();}
finally{
try {
if( null!=stat || !stat.isClosed())
stat.close();
}
catch (SQLException e) {e.printStackTrace();}
getConn.close();
}
return r;
}
private ResultSet r = null ;//这是类变量 !
package util.db;
import java.sql.ResultSet;
import java.sql.SQLException;
public class test {
public static void main(String[] args){
OperateUser_Inf o = new OperateUser_Inf();
ResultSet rs = o.findAll();
System.out.println(rs);
try {
while( rs.next()){
System.out.print(rs.getInt(1));
System.out.print(rs.getString(2));
System.out.print(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}