我试图创建一个使用DBMS用户和密码的登录屏幕。澄清一下:没有用户表,只有数据库访问权限。
private String username = "root";
private String password = "root";
private String classname = "org.postgresql.Driver";
private String url = "jdbc:postgresql://localhost:5432/bd";
要清楚,我想从用户输入中获取用户和密码,而不是硬编码。
答案 0 :(得分:1)
如果您只需要某个给定用户的连接并通过使用:
import java.awt.GridLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class DynamicConnection {
protected String user = null;
protected String pass = null;
protected String url;
public DynamicConnection(String url) {
this.url = url;
}
public Connection getConnection() throws SQLException {
while(user == null || pass == null) {
askForUsernamePassword();
}
return DriverManager.getConnection(url, user, pass);
}
private void askForUsernamePassword() {
JPanel panel = new JPanel(new GridLayout(2,2));
panel.add(new JLabel("Username"));
JTextField tf = new JTextField();
panel.add(tf);
panel.add(new JLabel("Password"));
JPasswordField pf = new JPasswordField();
panel.add(pf);
if(JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(null, panel)) {
user = tf.getText().trim();
pass = pf.getText().trim();
}
}
public static void main(String[] args) {
try {
Connection con = new DynamicConnection("jdbc:postgresql://localhost:5432/bd").getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在某些代码中链接以显示如何完成:
JPanel