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
Update 1