我正在对用户进行身份验证
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()