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.establish Connection ();
}
catch (Exception e) {
e.printStackTrace();
}
}
//CREATE THE CONNECTION METHOD
public Connection establishConnection ()
{
Connection connection = null;
try {
DriverManager.registerDriv er(new
oracle.jdbc.driver.OracleD river());
Class.forName("oracle.jdbc .driver.Or acleDriver ");
connection = DriverManager.getConnectio n(
"jdbc:oracle:thin:@(descri ption=(add ress="
+"(host=squirrel.wmin.ac.u k)(protoco l=tcp)(por t=1521))"
+"(connect_data=(sid=ora8) ))", "username", "password");
// insert you user id and password for Oracle
System.out.println("Connec tion 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(pan el,BorderL ayout.NORT H);
f.getContentPane().add(tPa nel,Border Layout.CEN TER);
f.setVisible(true);
}
;
}
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.establish
}
catch (Exception e) {
e.printStackTrace();
}
}
//CREATE THE CONNECTION METHOD
public Connection establishConnection ()
{
Connection connection = null;
try {
DriverManager.registerDriv
oracle.jdbc.driver.OracleD
Class.forName("oracle.jdbc
connection = DriverManager.getConnectio
"jdbc:oracle:thin:@(descri
+"(host=squirrel.wmin.ac.u
+"(connect_data=(sid=ora8)
// insert you user id and password for Oracle
System.out.println("Connec
}
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.
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
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(pan
f.getContentPane().add(tPa
f.setVisible(true);
}
;
}
call these kind of methods in constructor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thank you
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.establish
isn't OK.
Since establishConnection() is a function of your DBConnection class, why do you call it on a StudyDatabaseAccessOracle class object?