有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在数据库登录注销密集型环境中处理登录凭据

我有一个数据库监控应用程序,每n:th秒从数据库读取一次数据,并将结果显示在仪表板上

我处理登录凭证的解决方案感觉不稳定,所以我想知道类似的应用程序是否有共同的做法

用户登录一次,然后在后台使用凭据在需要时登录和注销数据库

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;
}

共 (0) 个答案