I check the user as
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;
}
} finally {
rs.close();
}
} finally {
s.close();
}
} catch(SQLException e) {
} finally {
dataManager.putConnection(connection);
}
}
return authenticated;
}
I close the connection at dataManager.putConnection(connection). I want to ask as soon as the user receives the login, then I need to update the status of the user and save the history of the log. 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;
}
if (autherntcated == true) {
sql = "query2(update status)";
rs = s.executeQuery(sql);
while (rs.next()) {
}
sql = "anotherQuery";
rs = s.executeQuery(sql);
while (rs.next()) {
}
}
} finally {
rs.close();
}
} finally {
s.close();
}
} catch(SQLException e) {
} finally {
dataManager.putConnection(connection);
}
Does the same connection use the same statement and the same result set to execute different queries or is this the wrong approach?
Thank.
Edit ------------------------------------------------- - ------------
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;
}
} finally {
rs.close();
}
} finally {
prepStatement.close();
}
if (authenticated == true) {
updateUser(connection, userName);
}
} catch(SQLException e) {
} finally {
dataManager.putConnection(connection);
}
}
UPdate Method:
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) {
}
}
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) {
}
}
source
share