String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//数据库用户名
String userName="sa";
//密码
String userPasswd="1";
//数据库名
String dbName="webcode";
//表名
String tableName="t_usb_list_f";
//联结字符串
//String url="jdbc:microsoft:sqlserver://localhost:1433/"+dbName+"?user="+userName+"&password="+userPasswd; String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=webcode";
String UserName = "sa";
String PassWord = "1";
Class.forName(driverName);
Connection connection= DriverManager.getConnection(url, UserName, PassWord); Statement statement4 = connection.createStatement();String sql="select * from t_usb_list_c where jgdm='test'";
ResultSet rs = statement4.executeQuery(sql);//获得数据结果集合ResultSetMetaData rmeta = rs.getMetaData();//确定数据集的列数,亦字段数int numColumns=rmeta.getColumnCount();rs.next();
如上代码是查找根据jgdm字段查找数据库数据,如果无数据 我该怎么判断并且打印无数据啊。
//数据库用户名
String userName="sa";
//密码
String userPasswd="1";
//数据库名
String dbName="webcode";
//表名
String tableName="t_usb_list_f";
//联结字符串
//String url="jdbc:microsoft:sqlserver://localhost:1433/"+dbName+"?user="+userName+"&password="+userPasswd; String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=webcode";
String UserName = "sa";
String PassWord = "1";
Class.forName(driverName);
Connection connection= DriverManager.getConnection(url, UserName, PassWord); Statement statement4 = connection.createStatement();String sql="select * from t_usb_list_c where jgdm='test'";
ResultSet rs = statement4.executeQuery(sql);//获得数据结果集合ResultSetMetaData rmeta = rs.getMetaData();//确定数据集的列数,亦字段数int numColumns=rmeta.getColumnCount();rs.next();
如上代码是查找根据jgdm字段查找数据库数据,如果无数据 我该怎么判断并且打印无数据啊。
public static Connection conn;
public static PreparedStatement ps;
public static ResultSet rs;
public static Connection conn1;
public static PreparedStatement ps1;
public static ResultSet rs1;
public List getUserList() {
List list = new ArrayList();
String sql = "SELECT ALL up.priv_name,users.user_priv, users.user_id, users.user_name, users.dept_id, users.sex, users.headship, department.dept_name FROM user_priv up, users, department WHERE users.dept_id = department.dept_id AND users.user_priv = up.user_priv and users.user_id<>'system'"; try {
conn = ConnectionPool.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Users user = new Users();
user.setUserId(rs.getString("user_id"));
user.setUserName(rs.getString("user_name"));
user.setUserPriv(rs.getString("user_priv"));
user.setDeptId(rs.getString("dept_id"));
user.setSex(rs.getString("sex"));
user.setHeadship(rs.getString("headship"));
user.setDeptName(rs.getString("dept_name"));
user.setPrivName(rs.getString("priv_name"));
list.add(user);
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
ConnectionPool.close(conn); }
} catch (Exception exception) {
exception.printStackTrace();
}
} return list;
}
true是基础类型,不是对象,这样写:if (true == rs.next())由于rs.next()本来就是布尔型,更简洁的可以这样:if (rs.next())
if(rs.next) {
有值的处理
} else {
没值的处理
}像这种if (true == rs.next())写法不建议哦,太傻瓜了。具体可以看看代码重构。另外是判断你得到的集合size()是否大于0,然后处理结果
if (rs.next())
或者
while(rs.next())