calling a method

hi can anyone help me please, i need to add this method makeGUI() to my main method without any errors also to enable my connection to the database.
1) how do i call the method makeGUI() from the main method
2) and exactly where do i place the method in the main method
secondly
3) how do i use a for loop for my GridBagLayout and get the same user interface as i have here.Thank you
this is my code

import java.io.*;
import java.sql.*;


public class DBConnection {
      
      public static void main (String [] args)
      {
                  makeGUI();
            StudyDatabaseAccessOracle myDatabaseAccess =
                  new StudyDatabaseAccessOracle();
            try {
                  Connection connection = myDatabaseAccess.establishConnection();
            
            }
            catch (Exception e) {
                  e.printStackTrace();
            }
      }



//CREATE THE CONNECTION METHOD
      public Connection establishConnection ()
      {
            Connection connection = null;
      try {
            DriverManager.registerDriver(new
                  oracle.jdbc.driver.OracleDriver());
            Class.forName("oracle.jdbc.driver.OracleDriver");

            connection = DriverManager.getConnection(
                  "jdbc:oracle:thin:@(description=(address="
                  +"(host=squirrel.wmin.ac.uk)(protocol=tcp)(port=1521))"
                  +"(connect_data=(sid=ora8)))", "username", "password");
                        // insert you user id and password for Oracle
            System.out.println("Connection successful");
      }
      catch (Exception e) {
            e.printStackTrace();
      }
      return connection;
      
      
      
      
      
      
      
      //CREATE USER INTERFACE
      
      
      static void makeGUI(){
                JFrame f = new JFrame ("Main Frame");
                f.setSize(350, 250);

JPanel panel = new JPanel(new GridBagLayout());
//JPanel panel = new JPanel(new FlowLayout());
panel.setBackground(Color.magenta);
panel.add(new JButton("SEARCH"));
panel.add(new JButton("UPDATE"));
panel.add(new JButton("CLEAR"));
panel.add(new JButton("ADD"));

JPanel tPanel = new JPanel(new GridBagLayout());
tPanel.setBackground(Color.magenta);

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;

tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
gbc.gridy = 0;
tPanel.add(new JLabel("TITLE"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("AUTHOR"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("PUBLISHER"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("YEAR"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("ISBN"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("CATEGORY"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("NUMBER"),gbc);
gbc.gridx = 0;
gbc.gridy++;
tPanel.add(new JTextField(20),gbc);
gbc.gridx = 1;
tPanel.add(new JLabel("UNIT PRICE"),gbc);
gbc.gridx = 0;
gbc.gridy++;

f.getContentPane().add(panel,BorderLayout.NORTH);
f.getContentPane().add(tPanel,BorderLayout.CENTER);

f.setVisible(true);
}
;      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
}      
LVL 1
lucinda77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
>> 1) how do i call the method makeGUI() from the main method
You do already. Looks OK at first sight. What errors do you got?
>> 2) and exactly where do i place the method in the main method
Seems OK.

>> 3) how do i use a for loop for my GridBagLayout and get the same user interface as i have here
Why should you? It's OK like it is now.

Btw, I think in the future you'll need to add "named" JTextField's instead of the anonymous ones now.
I mean:

public class DBConnection {

    JTextField titleField = new JTextField(20);
    ...


   
    tPanel.add(titleField, gbc);   // instead of tPanel.add(new JTextField(20),gbc);


Remark:
I think the line
        >> Connection connection = myDatabaseAccess.establishConnection();
isn't OK.
Since establishConnection() is a function of your DBConnection class, why do you call it on a StudyDatabaseAccessOracle class object?
0
jprgnCommented:
call these kind of methods in constructor
0
zzynxSoftware engineerCommented:
So, I don't understand these lines:

          StudyDatabaseAccessOracle myDatabaseAccess = new StudyDatabaseAccessOracle();      // What object is this?
          try {
               Connection connection = myDatabaseAccess.establishConnection();

I would expect something like this:


public class DBConnection {

     public DBConnection() {
     }

     private void makeGUI(){
        // The code you have now
        // ...
     }
 
     private Connection establishConnection () {
        // The code you have now
        // ...
     }
     
     public static void main (String [] args) {

          DBConnection dbConnection = new DBConnection();
          dbConnection.establishConnection();
          dbConnection.makeGUI();
     }

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.