7 Replies - 1545 Views - Last Post: 27 November 2010 - 01:38 AM Rate Topic: -----

#1 seyidesh  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 51
  • Joined: 24-November 10

Connecting to Access Database

Posted 26 November 2010 - 09:26 AM

Hi Guys, Please i need you help, I am a complete Java beginner and i have problem connecting my GUI to Access Database. Here is my codes kindly help!:
import javax.swing.*;
import javax.sql.*;
import java.awt.*;
import java.sql.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class StaffRepository2 implements ActionListener{
	
	 JFrame frame1;
	 JPanel panel;
	 JPanel panel1;
	
	 JLabel label1;
	 JLabel label2;
	 JLabel label3;
	 JLabel label4;
	 JLabel label5;
	 JLabel label6;
	 JLabel label7;
	 JLabel label8;
	 JLabel label9;
	 JLabel label10;
	 JLabel label11;
	 JLabel label12;
	 JLabel label13;


	 JTextField id;
	 JTextField fname;
	 JTextField mname;
	 JTextField lname;
	 JTextArea address;
	 JTextField phone;
	 JTextField basic;
	 JTextField housing;
	 JTextField medical;
	 JTextField tax;
	 JTextField net;
	 JTextField gross;
	Connection con;
	PrparedStatement stat;
	Statement stmt;
	ResultSet rs;

	static JButton save;
	static JButton calculate;
	static JButton referech;
	static JButton search;

	public StaffRepository2(){
	frame1 = new JFrame("CBN StaffRepository");//Gives the heading of the frame
	frame1.setSize(200, 300);//Gives the size of the frame
	frame1.setVisible(true);//Makes the frame visible otherwise it would not display

	panel = new JPanel() ;
	panel1 = new JPanel() ;

try{
	Class.forName("Jdbc.odbc.JdbcOdbcDriver");

}
	
	label1 = new JLabel("ID No.") ;
	label2 = new JLabel("First name") ;
	label3 = new JLabel("Middle name") ;
	label4 = new JLabel("Last name") ;
	label5 = new JLabel("Address") ;
	label6 = new JLabel("Phone number") ;
	label7 = new JLabel("Basic salary") ;
	label8 = new JLabel("Housing allowance") ;
	label9 = new JLabel("Medical") ;
	label10 = new JLabel("Tax") ;
	label11 = new JLabel("Net pay") ;
	label12 = new JLabel("Gross pay") ;
//This specifies the text field for each of the above labels
	id = new JTextField (2);
	fname = new JTextField (2);
	mname = new JTextField (2);
	lname = new JTextField (2);
	address = new JTextArea(2, 3);
	phone = new JTextField (2);
	basic = new JTextField (2);
	housing = new JTextField (2);
	medical = new JTextField (2);
	tax = new JTextField (2);
	net = new JTextField (2);
	gross = new JTextField (2);

	save = new JButton("Save");
	calculate = new JButton("Calculate");
	referech = new JButton("Referesh");
	search = new JButton("Search");
	

panel.setLayout(new GridLayout(12,2));
panel1.setLayout(new GridLayout(1,4));

frame1.getContentPane().setLayout(new BorderLayout());

panel.add(label1);
panel.add(id);

panel.add(label2);
panel.add(fname);

panel.add(label3);
panel.add(mname);

panel.add(label4);
panel.add(lname);

panel.add(label5);
panel.add(address);

panel.add(label6);
panel.add(phone);

panel.add(label7);
panel.add(basic);

panel.add(label8);
panel.add(housing);

panel.add(label9);
panel.add(medical);

panel.add(label10);
panel.add(tax);

panel.add(label11);
panel.add(net);

panel.add(label12);
panel.add(gross);

panel1.add(save);
panel1.add(calculate);
panel1.add(referech);
panel1.add(search);

frame1.getContentPane().setLayout(new BorderLayout());

//frame1.getContentPane().setLayout(new BorderLayout.WEST);

frame1.getContentPane().add(panel, BorderLayout.CENTER);


frame1.getContentPane().add(panel1, BorderLayout.SOUTH);

save.addActionListener(this);
save.setActionCommand("save");


calculate.addActionListener(this);
calculate.setActionCommand("calculate");


referech.addActionListener(this);
referech.setActionCommand("referech");


search.addActionListener(this);
search.setActionCommand("search");

}

public void actionPerformed(ActionEvent e){
if ("calculate".equals(e.getActionCommand())){

String bas=basic.getText();
int ba=Integer.parseInt(bas);

double medica=0.15*ba;
double hous=0.10*ba;
double tx=0.05*ba;

double nettotal=ba+medica+hous-tx;
double grs=ba+medica+hous;

tax.setText(String.valueOf(tx));
medical.setText(String.valueOf(medica));
housing.setText(String.valueOf(hous));
net.setText(String.valueOf(nettotal));
gross.setText(String.valueOf(grs));


}


if ("referech".equals(e.getActionCommand())){

id.setText(" ");
fname.setText(" ");
mname.setText(" ");
lname.setText(" ");
address.setText(" ");
phone.setText(" ");
basic.setText(" ");
housing.setText(" ");
medical.setText(" ");
tax.setText(" ");
net.setText(" ");
gross.setText(" ");


}

if ("save".equals(e.getActionCommand())){

try{

con = DriverManager.getConnect("Jdbc:Odbc:CBNDB");
stmt = con.prepareStatement("INSERT INTO Payroll ("id, fname, mname, lname, address, phone, basic, housing, medical, tax, net, gross") VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
String idval= id.getText();
String fnamebox= fname.getText();
String mnamebox= mname.getText();
String lnamebox= lname.getText();
String addressbox= address.getText();
String phonebox= phone.getText();
String basicallowance= Double.parseDouble(basic);
String housingallowance= Double.parseDouble(housing);
String medicalallowance= Double.parseDouble(medical);
String taxp= Double.parseDouble(tax);
String netp= Double.parseDouble(net);
String grossp= Double.parseDouble(gross);

stat.setString(1,idval);
stat.setString(2,fnamebox);
stat.setString(3,mnamebox);
stat.setString(4,lnamebox);
stat.setString(5,addressbox);
stat.setString(6,phonebox);
stat.setString(7,basicallowance);
stat.setString(8,housingallowance);
stat.setString(9,medicalallowance);
stat.setString(10,taxdeduce);
stat.setString(11,netp);
stat.setString(12,grossp);
stat.setString.executeQuery();
con.close();
error.setText("Record Saved");


}

catch(Exception e){System.out.println("Error" +e);
error.setText("Cannot save record");}


}

if ("search".equals(e.getActionCommand())){

}


}

public static void main(String args[]){
StaffRepository2 staff=new StaffRepository2();

}
}



Edited by macosxnerd101: Please, :code:.

Is This A Good Question/Topic? 0
  • +

Replies To: Connecting to Access Database

#2 Sh4d0ws  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 40
  • Joined: 23-July 10

Re: Connecting to Access Database

Posted 26 November 2010 - 09:55 AM

As a start, you should place your code in the provided code blocks. [.code][./code] (without the .)

People will probably be able to help you much easier if you do this for them. Also, what specifically is happening that is causing problems? Are you getting any errors, and if so you should post them as well.

This post has been edited by Sh4d0ws: 26 November 2010 - 09:56 AM

Was This Post Helpful? 0
  • +
  • -

#3 seyidesh  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 51
  • Joined: 24-November 10

Re: Connecting to Access Database

Posted 26 November 2010 - 10:19 AM

View PostSh4d0ws, on 26 November 2010 - 08:55 AM, said:

As a start, you should place your code in the provided code blocks. [.code][./code] (without the .)

People will probably be able to help you much easier if you do this for them. Also, what specifically is happening that is causing problems? Are you getting any errors, and if so you should post them as well.


Thanks for the advise but like i told you, am new to programming, Java and even this site. i really did not know the provide code block space that was provided.

I am having 4 when compling my codes.
here is the attachment of my Error Page.

kindly help.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Connecting to Access Database

Posted 26 November 2010 - 03:20 PM

I've added code tags for you. When you go to post, there is a big red box that demonstrates how to use them. You can use code tags, like so: :code:.

Also, please post your errors.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Connecting to Access Database

Posted 26 November 2010 - 03:58 PM

View Postseyidesh, on 26 November 2010 - 11:19 AM, said:

Thanks for the advise but like i told you, am new to programming, Java and even this site. i really did not know the provide code block space that was provided.

If you are new to programming don't start by trying to access a database
If you have another language experience, I'll bet this other language had arrays... Java too: you should be able to cut by at least 4 your number of lines of code.

If you do not know how to use array... don't try to write a program that access an SQL database.
Was This Post Helpful? 2
  • +
  • -

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Connecting to Access Database

Posted 26 November 2010 - 04:15 PM

Definitely agree with pbl. I would also suggest splitting up the front end GUI 'Swing' with the back-end database since it will make your application more maintainable. Make another class for the database stuff.

Edit -Errors

You have got a typo PrparedStatement stat;.

medical is a JTextField yet you are passing it in this String medicalallowance= Double.parseDouble(medical);

The method parseDouble() takes a String as it's params not a JTextField.

You should really have a look at the Java API and look at what each method you invoke takes as its params etc, it will help you out.

http://download.orac...ew-summary.html

This post has been edited by m-e-g-a-z: 26 November 2010 - 04:26 PM

Was This Post Helpful? 4
  • +
  • -

#7 LynnL  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 109
  • Joined: 13-April 09

Re: Connecting to Access Database

Posted 26 November 2010 - 09:28 PM

View Postpbl, on 26 November 2010 - 02:58 PM, said:

.If you do not know how to use array... don't try to write a program that access an SQL database.

I think PBL is G*** d*** right
The code will be a lot simpler with array use.
Learn to use them before even thinking accessing a database. Anyhow you will need them to store database query requests.
Was This Post Helpful? 1
  • +
  • -

#8 laupkram  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 25-August 09

Re: Connecting to Access Database

Posted 27 November 2010 - 01:38 AM

I always use this bunch of codes

public static void main(String []args) throws Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String myDB = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/youracessfile.MDB";
DBConn = DriverManager.getConnection(myDB,"","");
}



this will work only on machines that has MS Access installed on your PC, by the way other codes like querying the database needs a knowledge about SQL so study it first then look for other codes on how you can do SELECT, UPDATE, DELETE and INSERT :smile2:

This post has been edited by laupkram: 27 November 2010 - 01:39 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1