我正在尝试从终端显示数据库中人员的姓名列表,但不确定如何处理。现在我正在使用准备好的语句
output:
([{'status': 1, 'totalDonated': 0, 'name': 'benefactor 1', 'reference': '', 'personId': 'person1', 'docType': 'wallet', 'water': 0, 'objType': 'benefactor', 'id': 'DW1', 'identity': 'wallet:benefactor:DW1'}, {'status': 1, 'totalDonated': 0, 'name': 'benefactor 2', 'reference': '', 'personId': 'person2', 'docType': 'wallet', 'water': 0, 'objType': 'benefactor', 'id': 'DW2', 'identity': 'wallet:benefactor:DW2'}, {'status': 1, 'totalDonated': 0, 'name': 'benefactor 3', 'reference': '', 'personId': 'person3', 'docType': 'wallet', 'water': 0, 'objType': 'benefactor', 'id': 'DW3', 'identity': 'wallet:benefactor:DW3'}, {'status': 1, 'totalDonated': 0, 'name': 'benefactor 4', 'reference': '', 'personId': 'person4', 'docType': 'wallet', 'water': 0, 'objType': 'benefactor', 'id': 'DW4', 'identity': 'wallet:benefactor:DW4'}, {'status': 1, 'totalDonated': 0, 'name': 'benefactor 5', 'reference': '', 'personId': 'person5', 'docType': 'wallet', 'water': 0, 'objType': 'benefactor', 'id': 'DW5', 'identity': 'wallet:benefactor:DW5'}], <type 'list'>)
[{"status": 1, "totalDonated": 0, "name": "benefactor 1", "reference": "", "personId": "person1", "docType": "wallet", "water": 0, "objType": "benefactor", "id": "DW1", "identity": "wallet:benefactor:DW1"}, {"status": 1, "totalDonated": 0, "name": "benefactor 2", "reference": "", "personId": "person2", "docType": "wallet", "water": 0, "objType": "benefactor", "id": "DW2", "identity": "wallet:benefactor:DW2"}, {"status": 1, "totalDonated": 0, "name": "benefactor 3", "reference": "", "personId": "person3", "docType": "wallet", "water": 0, "objType": "benefactor", "id": "DW3", "identity": "wallet:benefactor:DW3"}, {"status": 1, "totalDonated": 0, "name": "benefactor 4", "reference": "", "personId": "person4", "docType": "wallet", "water": 0, "objType": "benefactor", "id": "DW4", "identity": "wallet:benefactor:DW4"}, {"status": 1, "totalDonated": 0, "name": "benefactor 5", "reference": "", "personId": "person5", "docType": "wallet", "water": 0, "objType": "benefactor", "id": "DW5", "identity": "wallet:benefactor:DW5"}]
# the below print from Python interpreter
# Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
print(json.dumps(m))
"[{\"docType\":\"wallet\",\"id\":\"DW1\",\"identity\":\"wallet:benefactor:DW1\",\"name\":\"benefactor 1\",\"objType\":\"benefactor\",\"personId\":\"person1\",\"reference\":\"\",\"status\":1,\"totalDonated\":0,\"water\":0},{\"docType\":\"wallet\",\"id\":\"DW2\",\"identity\":\"wallet:benefactor:DW2\",\"name\":\"benefactor 2\",\"objType\":\"benefactor\",\"personId\":\"person2\",\"reference\":\"\",\"status\":1,\"totalDonated\":0,\"water\":0},{\"docType\":\"wallet\",\"id\":\"DW3\",\"identity\":\"wallet:benefactor:DW3\",\"name\":\"benefactor 3\",\"objType\":\"benefactor\",\"personId\":\"person3\",\"reference\":\"\",\"status\":1,\"totalDonated\":0,\"water\":0},{\"docType\":\"wallet\",\"id\":\"DW4\",\"identity\":\"wallet:benefactor:DW4\",\"name\":\"benefactor 4\",\"objType\":\"benefactor\",\"personId\":\"person4\",\"reference\":\"\",\"status\":1,\"totalDonated\":0,\"water\":0},{\"docType\":\"wallet\",\"id\":\"DW5\",\"identity\":\"wallet:benefactor:DW5\",\"name\":\"benefactor 5\",\"objType\":\"benefactor\",\"personId\":\"person5\",\"reference\":\"\",\"status\":1,\"totalDonated\":0,\"water\":0}]"
答案 0 :(得分:0)
您已经关闭。
下面的代码不是一个完整的答案,但希望足以使您朝着获得完整答案的方向前进。下面的代码基本上是您发布并进行一些修改的代码。
public static void showNames() throws SQLException {
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
String selectTable="SELECT * FROM userInfo;";
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
rs = stmt.executeQuery(selectTable);
while (rs.next()) {
Object obj = rs.getObject("name of column in database table USERINFO");
System.out.println(obj);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
您没有发布数据库表USERINFO的结构,因此将数据库表中列的名称替换为实际的列名。
顺便说一下,互联网上有许多示例,例如Processing SQL Statements with JDBC。