私はユーザーを認証しています
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()
で接続を閉じていますdataManager.putConnection(connection)
。ユーザーがログインしたら、ユーザーのステータスを更新し、ログ履歴を維持する必要があります。このようなものを使用できますか
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);
}
同じ接続、同じステートメント、同じ結果セットを使用して他のクエリを実行することを意味しますか、それとも間違ったアプローチですか?
ありがとうございました。
編集 - - - - - - - - - - - - - - - - - - - - - - - - - -------------
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()
維持履歴:
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()