Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:56:34+00:00 2026-05-29T19:56:34+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:56:35+00:00Added an answer on May 29, 2026 at 7:56 pm
    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!!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a java application for Debian Linux. Now I want that that
I have created a java application and packed it into a jar file on
Hi I have created a java application. I found there is an application menu
I have created a sample java socket application. I used Socket s = new
I have created a fairly substantial Java GUI application with many form windows where
I have create a getDBConnection method in my Java application. This returns a connection
I have a problem with Hibernate (3.6.0.-Final) in my Java EE application using MySQL
I have created the following test server using java: import java.io.*; import java.net.*; class
I have created a program, i need to save the content into mysql database.
I have a distributed java application and I want to send the database data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.