• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

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);
}
;      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
}      
0
lucinda77
Asked:
lucinda77
  • 3
1 Solution
 
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
 
zzynxSoftware engineerCommented:
Thank you
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now