sto autenticato utente Likeposso usare lo stesso JDBC collegamento, affermazione e di risultati per eseguire due query 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()
sto chiudendo la connessione in dataManager.putConnection(connection)
. Voglio chiedere una volta che l'utente ottiene il login, quindi devo aggiornare lo stato dell'utente e mantenere la cronologia del registro. Posso usare qualcosa di simile
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);
}
significa usare stessa connessione, stessa affermazione e lo stesso gruppo di risultati eseguire altre query o è un approccio sbagliato?
Grazie.
Modifica ------------------------------------------- -------------------
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)
metodo di aggiornamento:
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:
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()
Intendete dire che posso chiudere Statement e ResultSet. Voglio dire che una volta che chiudo l'istruzione e ResultSet, quindi utilizzare l'aggiornamento e un'altra query nello stesso modo e quindi chiudere la connessione al database 'dataManager.putConnection (connessione); '. È? – Basit
@Basit: aggiornata la mia risposta per includere ulteriori informazioni. Idealmente vorrai riutilizzare l'oggetto * connessione *. ResultSet non ha senso e, come ho specificato, vorrei suggerire lo spostamento da * Dichiarazione * a * * PreparedStatement – Sujay
Come quando ho CREATE 'Statement s = connection.createStatement();' Poi devo per chiuderla. Devo anche chiudere la PreparedStatement nello stesso modo in cui sto facendo la Dichiarazione? Per prima cosa sto creando una dichiarazione usando la connessione, quindi ottieni 'ResultSet' usando' s.executeQuery (query) ', quindi esegui il ciclo attraverso il set di risultati. Come posso sostituirlo con PreparedStatement? – Basit