【发布时间】:2013-05-21 13:05:25
【问题描述】:
我正在尝试编写简单的 Java Web 应用程序来从数据库中获取数据。 我需要在不同的数据库表上运行几个选择查询。
String queryOne = "select firstname from employees where empid = id";
String queryOne = "select title from books where bookid = bid";
String queryOne = "select auther from books where bookid = bid";
我试着这样做:
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet rs1 = statement.executeQuery(queryOne);
while (rs1.nest()) {
String firstName = rs1.getString(1);
}
statement.close();
connection.close();
我只能使用相同的语句运行一个查询。如何使用同一语句执行多个查询?
【问题讨论】:
-
我得到这样的数据源:DataSource dataSource = (DataSource) context.lookup("jdbc/DatabaseName");如何将 allowMultipleQueries 标志添加到该字符串?谢谢。
-
你为什么在乎?为什么使用多个语句是一件坏事?
-
使用
connection.prepareStatement(),你得到的PreparedStatement是“可重复使用的”。你也应该这样做以获得参数替换。 -
如果我使用 PreparedStatement 如何获得多个结果集?