Link to home
Start Free TrialLog in
Avatar of lucinda77
lucinda77

asked on

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);
}
;      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
}      
Avatar of zzynx
zzynx
Flag of Belgium image

>> 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?
Avatar of jprgn
jprgn

call these kind of methods in constructor
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you