我可以使用相同的JDBC连接,语句和结果集在JDBC中执行两个查询 [英] can i use same JDBC connection, statement and resultset to execute two queries in JDBC
问题描述
我正在验证用户
public static boolean login(DataManager dataManager, String userName, String password) {
boolean authenticated = false;
Connection connection = dataManager.getConnection();
if (connection != null) {
try {
Statement s = connection.createStatement();
String sql = "query";
try {
ResultSet rs = s.executeQuery(sql);
try {
while (rs.next()) {
String group_code = rs.getString(1);
String orgaunit = rs.getString(2);
authenticated = true;
} //end of while()
} finally {
rs.close();
}
} finally {
s.close();
}
} catch(SQLException e) {
//System.out.println("Could not login from dataabse:" + e.getMessage());
} finally {
dataManager.putConnection(connection);
}
} //end of if (connection != null)
return authenticated;
} //end of login()
我在<$ c $关闭连接C> dataManager.putConnection(连接)。我想问一旦用户登录然后我必须更新用户的状态并维护日志历史记录。我可以使用这样的东西
I am closing connection in dataManager.putConnection(connection)
. I want to ask once user get login then i have to update the status of user and maintain log history. Can i use something like this
try {
Statement s = connection.createStatement();
String sql = "query";
try {
ResultSet rs = s.executeQuery(sql);
try {
while (rs.next()) {
String group_code = rs.getString(1);
authenticated = true;
} //end of while()
if (autherntcated == true) {
sql = "query2(update status)";
rs = s.executeQuery(sql);
while (rs.next()) {
//dos tuff
}
sql = "anotherQuery";
rs = s.executeQuery(sql);
while (rs.next()) {
//do stuff
}
}
} finally {
rs.close();
}
} finally {
s.close();
}
} catch(SQLException e) {
//System.out.println("Could not login from dataabse:" + e.getMessage());
} finally {
dataManager.putConnection(connection);
}
意味着使用相同的连接,相同的语句和相同的结果集执行其他查询或是它错误的做法?
Means using same connection, same statement and same resultset execute other queries or is it wrong approach ?
谢谢。
编辑
------ -------------------------------------------------- ------
if (connection != null) {
try {
String sql = "query";
PreparedStatement prepStatement = connection.prepareStatement(sql);
try {
ResultSet rs = prepStatement.executeQuery(sql);
try {
while (rs.next()) {
String group_code = rs.getString(1);
authenticated = true;
} //end of while()
} finally {
rs.close();
}
} finally {
prepStatement.close();
}
/// Addition
if (authenticated == true) {
updateUser(connection, userName);
}
} catch(SQLException e) {
//System.out.println("Could not login from dataabse:" + e.getMessage());
} finally {
dataManager.putConnection(connection);
}
} //end of if (connection != null)
更新方法:
private static void updateUser(Connection connection, String userName) {
try {
String sql = "UPDATE users SET status_code = 'A' WHERE login_id = '" + userName + "'";
PreparedStatement prepStatement = connection.prepareStatement(sql);
try {
int numberOfRowsUpdated = prepStatement.executeUpdate(sql);
} finally {
prepStatement.close();
}
maintainHistory(connection);
} catch(SQLException e) {
//System.out.println("Could not login from dataabse:" + e.getMessage());
}
} //end of updateUser()
maintainHistory:
maintainHistory:
private static void maintainHistory(Connection connection) {
try {
String sql = "INSERT INTO auditlog_user_logins(user_code,logintime,prstid) VALUES ();";
PreparedStatement prepStatement = connection.prepareStatement(sql);
try {
int numberOfRowsUpdated = prepStatement.executeUpdate(sql);
} finally {
prepStatement.close();
}
} catch(SQLException e) {
//System.out.println("Could not login from dataabse:" + e.getMessage());
}
} //end of maintainHistory()
推荐答案
我可以使用相同的JDBC连接,声明
can I use same JDBC connection, statement
是的。您可以在结束前重复使用它们。
Yes. You can reuse them prior to closing.
和结果集
and resultset
没有。这个问题没有意义。结果集是执行查询或更新的结果。没有重新使用它的问题。我想你需要在执行下一个查询或更新之前关闭前面的结果集。
No. The question doesn't make sense. The result set is the result of executing a query or update. There is no question of re-using it. I imagine you need to close the preceding result set prior to executing the next query or update.
这篇关于我可以使用相同的JDBC连接,语句和结果集在JDBC中执行两个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!