我有一个数据库监控应用程序,我每隔n秒从数据库中读取数据,并在仪表板上显示结果。
我处理登录凭据的解决方案感觉不稳定所以我想知道类似的应用程序是否有通用的做法?
用户登录一次,在幕后,凭据将用于在需要时登录和注销数据库。
package common;
import java.awt.GridLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.prefs.Preferences;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class OraLogin {
private JTextField uName = new JTextField();
private JPasswordField pWord = new JPasswordField();
private JTextField dBase = new JTextField();
private JTextField host = new JTextField();
// This will define a node in which the preferences can be stored
Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
// Init prefs ID:s
private String prefs1 = "Username";
private String prefs2 = "Password";
private String prefs3 = "Database";
private String prefs4 = "Hostname";
private String un;
private String pw;
private String db;
private String hn;
private static Connection currentConn;
/**
* Constructor
* @throws SQLException
*/
public OraLogin(){
// Get prefs
getPreferences();
}
/**
* User login to db
*/
public void userLogin() throws Exception{
try {
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(new JLabel("Username"));
panel.add(uName);
panel.add(new JLabel("Password"));
panel.add(pWord);
panel.add(new JLabel("Database"));
panel.add(dBase);
panel.add(new JLabel("Hostname/IP"));
panel.add(host);
// Init fields
uName.setText(un);
pWord.setText(pw);
dBase.setText(db);
host.setText(hn);
int result = JOptionPane.showConfirmDialog(null, panel, "Connect to database",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
currentConn = dbConnect();
}else{
System.exit(0);
}
} catch (Exception e) {
throw new Exception(this.getClass().getSimpleName()+": "+e.getMessage());
}
setPreferences();
}
/**
* Get current connection
* @return
*/
public Connection getCurrentConnection(){
return currentConn;
}
/**
* Silent connect to database
* @return
* @throws Exception
*/
public Connection silentLogin() throws Exception {
Connection silentConn = null;
getPreferences();
try {
silentConn = DriverManager.getConnection("jdbc:oracle:thin:@//"+hn+":1521/"+db, un,pw);
} catch (Exception e) {
sysLogger.logMsg("SEVERE",this.getClass().getSimpleName()+": "+e.getMessage());
throw new Exception(this.getClass().getSimpleName()+": "+e.getMessage());
}
return silentConn;
}
/**
*
* Create database connection
* @return
* @throws Exception
*/
private Connection dbConnect() throws Exception{
Connection conn;
try {
pWord.selectAll();
conn = DriverManager.getConnection("jdbc:oracle:thin:@//"+host.getText()+":1521/"+dBase.getText(), uName.getText(),pWord.getSelectedText());
} catch (Exception err) {
throw new Exception(this.getClass().getSimpleName()+": "+err.getMessage());
}
System.out.println(conn);
return conn;
}
/**
* Set preferences
*/
private void setPreferences() {
prefs.put(prefs1, uName.getText());
prefs.put(prefs2, pWord.getSelectedText());
prefs.put(prefs3, dBase.getText());
prefs.put(prefs4, host.getText());
}
/**
* Get preferences
*/
private void getPreferences() {
un = (prefs.get(prefs1,""));
pw = (prefs.get(prefs2,""));
db = (prefs.get(prefs3,""));
hn = (prefs.get(prefs4,""));
}
}
来自db类的调用......
public int getCount(){
System.out.println(this.getClass().getSimpleName()+": getCount");
CallableStatement stmt = null;
ResultSet rs = null;
int invalidCount = 0;
// Get connection
try {
OraLogin login = new OraLogin();
Connection conn = login.silentLogin(); // Create disposable connection
conn.setAutoCommit(false);
// Prepare statement and execute
try {
stmt = conn.prepareCall(query);
stmt.registerOutParameter(1, OracleTypes.CURSOR);
stmt.execute();
rs = (ResultSet) stmt.getObject(1);
// Init values from result set
try {
while (rs.next()) {
invalidCount = rs.getInt(1);
}
} catch (SQLException e) { // Cannot resolve result set
sysLogger.logMsg("SEVERE",this.getClass().getSimpleName()+": "+e.getMessage());
e.printStackTrace();
} finally {
rs.close(); // Close result set
}
} catch (SQLException e) { // Cannot prepare and execute
sysLogger.logMsg("SEVERE",this.getClass().getSimpleName()+": "+e.getMessage());
e.printStackTrace();
} finally {
stmt.close(); // Close statement
conn.close(); // Close connection
}
} catch (Exception e1) { // Cannot get connection
sysLogger.logMsg("SEVERE",this.getClass().getSimpleName()+": "+e1.getMessage());
e1.printStackTrace();
}
return invalidCount;
}