1

I have created a Java class for connecting to the MySQL database for my Java SWING Application.

public class DB1 {

private static Connection c;

public static Connection getMyConnection() throws Exception {
    if (c == null) {
        JTextField myUserNameTextField = new JTextField();
        JTextField myPasswordTextField = new JTextField();
        JTextField myIPTextField = new JTextField();
        JTextField myPortTextField = new JTextField();
        String myUserName = myUserNameTextField.getText();
        String myPassword = myPasswordTextField.getText();
        String myURL = "jdbc:mysql:// " + myIPTextField.getText() + ":" + myPortTextField.getText() + "/DBName";

        Class.forName("com.mysql.jdbc.Driver");

        c = DriverManager.getConnection(myURL, myUserName, myPassword);
    }
    return c;

}

public static void InsertEditDelete(String sql) throws Exception {
    getMyConnection().createStatement().executeUpdate(sql);
}

public static ResultSet Search(String sql) throws Exception {
    return getMyConnection().createStatement().executeQuery(sql);
}
}

I want to dynamically change the IP , which is localhost here and the port number from Jtextfields , so u do not recompile every time u change the IP and PORT number. These JTextfields should get opened up in a JFRAME

4
  • I created my class according what you told me. Now how do I get to display Jtextfields to enter the details. These details should also get saved in the class. Commented Feb 11, 2012 at 10:39
  • 1
    one time you are saying you want to take hostname and port from textfield and another time you are saying you want to access from table... please clear question... re-iterate your question again.... Commented Feb 11, 2012 at 10:45
  • Sorry, I just want to change it using JTextFields. I want these textfields to open in a frame. Commented Feb 11, 2012 at 10:51
  • see my updated answer Update 1 Commented Feb 11, 2012 at 11:00

4 Answers 4

2
ResultSet rs = st.executeQuery("SELECT username, password, host, port FROM  myAllData where id=1");

String myURL = "jdbc:mysql:// " + rs.getString("host") + ":" + rs.getString("port") + "/DBName"
String myUserName = rs.getString("username");
String myPassword = rs.getString("password");

Class.forName("com.mysql.jdbc.Driver");

c = DriverManager.getConnection(myURL, myUserName, myPassword);

Update 1

If you need JTextField use below.

String myUserName = myUserNameTextField.getText();
String myPassword = myPasswordTextField.getText();
String myURL = "jdbc:mysql:// " + myIPTextField.getText() + ":" + myPortTextField.getText() + "/DBName"

Class.forName("com.mysql.jdbc.Driver");

c = DriverManager.getConnection(myURL, myUserName, myPassword);

Good Luck!!!

Sign up to request clarification or add additional context in comments.

1 Comment

OK Thanks I get it. But I want to change the values in Strings by entering into a JRAME or something like that. I can't edit the class every time. Basically, I have developed a SWING application. So I want a JRAME which has JTexfields, which will change the values of the DB Class I have created above on a button click event.
1

Something like this should be what you want :

String host = textFieldHost.getText();
String port = textFieldPort.getText();

DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/DBName",host,port), "root", "123");

Comments

1

You can put this settings into .properties or XML file, and then read it when needed.

This link should Help

http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html

Comments

0

Just use some string concatenation

String host = ...
int port = ...
String jdbcurl = "jdbc:mysql://" + host + ":" + port + "/DBName";
DriverManager.getConnection(jdbcurl,"root","123");

3 Comments

Cant I store the host and port in mySQL database, and retrieve it using a resultset?
And how do you first access the database when you want to first retrieve host and port from there?
Yes you are right! I cannot access the database when my IP and HOST are changed.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.