1

I want to implement a program where there is:

  1. A MySQLConnection class that handles the database connection
  2. A Screen class for the GUI that takes input to be stored in the DB
  3. Main class

I want to call the MySQLConnection object created in the main method from the Screen class.

What I have attempted so far - erroneously- is as follows. Pointers to resolve this problem are greatly appreciated.

Thanks in advance!

Main Class:

public class Main {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    //Start main screen
                    Screen start = new Screen(); 
                    start.setVisible(true);
                   //create db connection
                   **MySQLConnection sqlConn = new MySQLConnection();
                   Connection con=sqlConn.connectToDatabase();**
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

MySQL Connection class:

public class MySQLConnection {
   public Connection connectToDatabase() {//connect to db   }
   public void addToDatabase(String tableName, ArrayList<String> columns, ArrayList<String> values) {//add a row to table in db}
   public void closeConnection() {//close db connection}
}

AddScreen class:

public class Screen extends JFrame{
    ArrayList<String> mainValues= new ArrayList<String>();

    //Create GUI
    public Screen(){
        ...
        ArrayList<String> mainValues= new ArrayList<String>(); 
        String mainId = mainI.getText();
        String mainName = mainN.getText();
        mainValues.add(mainId);
        mainValues.add(mainName);
  }

    public void saveToDatabase() {
        String[] mainColumns= {"mid", "mname"};
        **con.addToDatabase("main_category", mainColumns, mainValues);**
    }
}

2 Answers 2

2

The problem is that the Screen class doesn't have access to the MySQLConnection object. There are several ways to solve this, the simplest is to simply pass the MySQLConnection object to the Screen class.

Main:

MySQLConnection sqlConn = new MySQLConnection();
Screen start = new Screen(sqlConn); 
start.setVisible(true);

MySQLConnection

private Connection con;

public MySQLConnection() {
    this.connectToDatabase();
}

public void connectToDatabase() { 
    this.con = //connect to db   
}

public void addToDatabase(String tableName, ArrayList<String> columns, ArrayList<String> values) {...}

public void closeConnection() {...}

Screen:

private MySQLConnection sqlConn ;

public Screen(MySQLConnection sqlConn){
    ...
    this.sqlConn = sqlConn;
    String mainId = mainI.getText();
    String mainName = mainN.getText();
    mainValues.add(mainId);
    mainValues.add(mainName);
}

public void saveToDatabase() {
    String[] mainColumns= {"mid", "mname"};
    sqlConn.addToDatabase("main_category", mainColumns, mainValues);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This works and the explanation is clear. I get a new error "The method addToDatabase(...) is undefined for the type Connection" which can be resolved with a casting to MySQlConnection. Is there any way to manage this without a cast? Thanks!
Not really sure about your requirements. But you might want to change the design by wrapping Connection in MySQLConnection. See the updated answer.
1

In your main class you need to get the return value in the Connection object,

Connection con=sqlConn.connectToDatabase();

Now, con will hold the connection object . so that you can use it to query the database as normal.

4 Comments

Attempted. But when trying to call con from within Screen with con.addToDatabase(...), I still get "con cannot be resolved"
Editted question to reflect changes
Make sure that you have imported java.sql.connection class
Yes I had. Passing the connection via the constructor solved the problem.

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.