我想将结果查询转换为String以在我的java代码中使用.. 有人可以帮忙吗? TQ
代码::
rs = st.executeQuery("SELECT password FROM USER WHERE UserID='"+txtUserId.getText()+"';");
pass.add(rs.getString("password"));
st.close();
//rs.close();
con.close();
}catch(Exception e) {
e.printStackTrace();
System.out.println("Could Not Connect to Database");
}
答案 0 :(得分:2)
您需要rs.next()
光标向前移动ResultSet
数据。
while(rs.next()){
pass.add(rs.getString("password"));
}
答案 1 :(得分:0)
executeQuery
返回ResultSet
- 然后您可以在ResultSet中导航以获取语句/查询返回的值
此处示例
public static void viewTable(Connection con, String dbName)
throws SQLException {
Statement stmt = null;
String query = "select COF_NAME, SUP_ID, PRICE, " +
"SALES, TOTAL " +
"from " + dbName + ".COFFEES";
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "\t" + supplierID +
"\t" + price + "\t" + sales +
"\t" + total);
}
} catch (SQLException e ) {
JDBCTutorialUtilities.printSQLException(e);
} finally {
if (stmt != null) { stmt.close(); }
}
}
答案 2 :(得分:0)
试试这个: 您希望获得多个值均值使用
while(rs.next()){
pass.add(rs.getString("password"));
}
您想获得单一价值
if(rs.next()){
pass.add(rs.getString("password"));
}