0

I'm trying to make a login screen that uses users and passwords of DBMS. To clarify: there is no a table of users, only the database access rights.

private String username = "root";
private String password = "root";
private String classname = "org.postgresql.Driver";
private String url = "jdbc:postgresql://localhost:5432/bd";

To be clear, I want to obtain user and password from user input, not to hardcode them.

5
  • 2
    What's the question?
    – Maroun
    Dec 13, 2015 at 12:37
  • where are the "inputs"? Are you thinking a Dialog maybe? And then grab the values and pass them into you connection?
    – Jan
    Dec 13, 2015 at 13:11
  • Possible duplicate of How can I add a login screen to an existing Java Swing program?
    – Basilevs
    Dec 13, 2015 at 17:01
  • The problem is that in the process of make a connection with DBMS it needs to receive a username and password. In all codes that I lookup, it is hardcode in the the database class, and I want it to come from inputs. I already have the inputs, I only need to know how to set up the connection dynamic.
    – Tarcisiofl
    Dec 14, 2015 at 14:02
  • @Jan Yes, that is it. I'm now only need to find how to grab and pass the values to the connection.
    – Tarcisiofl
    Dec 14, 2015 at 15:47

1 Answer 1

1

If you only need a connection for some given user and pass use:

public Connection getConnection(String user, String pass) {
  String url = "jdbc:postgresql://localhost:5432/bd";
  return DriverManager.getConnection(url, user, pass);
}

Linked in some code to show how it could be done:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.