1
这是我的一小段代码。基本上,我不知道如何打印我的ResultSet或将它变成字符串。不知道如何转换为字符串并打印ResultSet,SELECT语句
try {
String url = "jdbc:odbc:" + "userstuff";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url,"","");
// Gets a statement
Statement state = con.createStatement();
String query = "SELECT description FROM topics WHERE title = '" + title + "'";
String query2 = "SELECT * FROM comment WHERE topic = '" + title + "'";
// selects the description for the selected topic (title will be referenced to the chosen topic)
ResultSet results = state.executeQuery(query);
// selects * of the rows from "comment" table where the topic equals the selected title.
ResultSet results2 = state.executeQuery(query2);
desc = results.toString();
}
这不是很好的代码。简而言之,您应该将所有ResultSet映射到对象或数据结构中,并将其关闭。学习如何使用PreparedStatement;不要像你那样手动建立查询字符串。谷歌的“SQL注入”来理解为什么。 ODBC桥驱动程序在JDK 8中不再可用。您不应该依赖它。 – duffymo
好的,谢谢你的提示和帮助。我会尽力改进我的代码,对此我很陌生。 – Questioning