| Author |
Adding Record Problem in JDBC ?
|
Rosewell
Greenhorn
Joined: Jul 22, 2002
Posts: 8
|
|
Hello; I have a problem in adding a record to my database which is Employees.mdb. I tried doing it by using y1 - which is a JButton with an ActionListener, I also did minimize the operation so that I could see if the program will add a record. The Problem is that, when I press the Add Button w/c is y1, the dos promt generates a NullPointerExcaption, thats why adding a record doesnt work. Can someone here help me pls ! its an Emergency !,thanks. // Sample Program /*Program Made By: Jan - Michael Soan 4-D Project Study T-F 3:00 - 4:30 */ import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; public class Database extends JFrame { private String url; private Connection connect; private Statement statement; private Connection connection; public Database() { super( "Timeport . . ." ); try { url = "jdbc dbc:Employees"; Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); connect = DriverManager.getConnection( url ); JOptionPane.showMessageDialog(null,"Connection Successful !", "Database . . .",JOptionPane.PLAIN_MESSAGE); } catch ( ClassNotFoundException cnfex ) { cnfex.printStackTrace(); } catch ( SQLException sqlex ) { sqlex.printStackTrace(); } catch ( Exception ex ) { ex.printStackTrace(); } Container c = getContentPane(); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); JPanel p4 = new JPanel(); JPanel p5 = new JPanel(); JPanel p6 = new JPanel(); JPanel p7 = new JPanel(); // Sub Panel's s1 JPanel s1 = new JPanel(); JPanel s2 = new JPanel(); JPanel s3 = new JPanel(); JPanel s4 = new JPanel(); JPanel s5 = new JPanel(); JPanel s6 = new JPanel(); JLabel l1 = new JLabel("Firstname :"); JLabel l2 = new JLabel("Lastname :"); JLabel l3 = new JLabel("Employee No :"); JLabel l4 = new JLabel("SSS Number :"); final JTextField t1 = new JTextField(20); final JTextField t2 = new JTextField(20); final JTextField t3 = new JTextField(20); final JTextField t4 = new JTextField(20); p1.add(l1); p1.add(t1); p1.add(l2); p1.add(t2); p1.add(l3); p1.add(t3); p1.add(l4); p1.add(t4); p1.setLayout( new GridLayout( 4, 2 ) ); p1.setBorder(new javax.swing.border. TitledBorder(new javax.swing.border.EtchedBorder())); s1.add(p1); c.add(s1, BorderLayout.NORTH); JTabbedPane x1 = new JTabbedPane(); String name1 = "Identification"; JLabel l5 = new JLabel("Gender :"); JLabel l6 = new JLabel("Status :"); JLabel l7 = new JLabel("Birthdate :"); JLabel l8 = new JLabel("Address :"); JLabel l9 = new JLabel("City :"); JLabel l10 = new JLabel("Province :"); JLabel l11 = new JLabel("Country :"); final JCheckBox b1 = new JCheckBox("Male"); final JCheckBox b2 = new JCheckBox("Female"); s4.add(b1); s4.add(b2); s4.setLayout( new GridLayout( 1, 2 ) ); final JTextField t5 = new JTextField(20); final JTextField t6 = new JTextField(20); final JTextField t7 = new JTextField(20); final JTextField t8 = new JTextField(20); final JTextField t9 = new JTextField(20); final JTextField t10 = new JTextField(20); p3.add(l5); p3.add(s4); p3.add(l6); p3.add(t5); p3.add(l7); p3.add(t6); p3.add(l8); p3.add(t7); p3.add(l9); p3.add(t8); p3.add(l10); p3.add(t9); p3.add(l11); p3.add(t10); p3.setLayout( new GridLayout( 9, 2 ) ); p3.setBorder(new javax.swing.border. TitledBorder(new javax.swing.border.EtchedBorder())); x1.add(name1, p3); // End of 1st x1 String name2 = "Contacs"; JLabel l12 = new JLabel("Telephone :"); JLabel l13 = new JLabel("E-Mail :"); JLabel l14 = new JLabel("Spouse :"); JLabel l15 = new JLabel("Contact No :"); final JTextField t11 = new JTextField(20); final JTextField t12 = new JTextField(20); final JTextField t13 = new JTextField(20); final JTextField t14 = new JTextField(20); s2.add(l12); s2.add(t11); s2.add(l13); s2.add(t12); s2.add(l14); s2.add(t13); s2.add(l15); s2.add(t14); s2.setLayout( new GridLayout( 4, 2 ) ); s2.setBorder(new javax.swing.border. TitledBorder(new javax.swing.border.EtchedBorder())); JLabel l16 = new JLabel("Special Contact :"); final JTextField t15 = new JTextField(20); s3.add(l16); s3.add(t15); s3.setLayout( new GridLayout( 4, 2 ) ); s3.setBorder(new javax.swing.border. TitledBorder(new javax.swing.border.EtchedBorder())); p4.add(s2); p4.add(s3); p4.setLayout( new GridLayout( 2, 1 ) ); x1.add(name2, p4); // End of 2nd x1 String name3 = "Employment"; JLabel l17 = new JLabel("Job Title :"); JLabel l18 = new JLabel("Department :"); JLabel l19 = new JLabel("Date hired :"); JLabel l20 = new JLabel("Institution :"); JLabel l21 = new JLabel("Course Attiend :"); JLabel l22 = new JLabel("Awards / GPA :"); final JTextField t16 = new JTextField(20); final JTextField t17 = new JTextField(20); final JTextField t18 = new JTextField(20); final JTextField t19 = new JTextField(20); final JTextField t20 = new JTextField(20); final JTextField t21 = new JTextField(20); p5.add(l17); p5.add(t16); p5.add(l18); p5.add(t17); p5.add(l19); p5.add(t18); p5.add(l20); p5.add(t19); p5.add(l21); p5.add(t20); p5.add(l22); p5.add(t21); p5.setLayout( new GridLayout( 9, 2 ) ); p5.setBorder(new javax.swing.border. TitledBorder(new javax.swing.border.EtchedBorder())); x1.add(name3, p5); // End of 3rd x1 String name4 = "Deductions"; JLabel l23 = new JLabel("Tax Deduction :"); JLabel l24 = new JLabel("SSS Deduction :"); JLabel l25 = new JLabel("Loans :"); JLabel l26 = new JLabel("Medicare :"); JLabel l27 = new JLabel("Others :"); final JTextField t22 = new JTextField(20); final JTextField t23 = new JTextField(20); final JTextField t24 = new JTextField(20); final JTextField t25 = new JTextField(20); final JTextField t26 = new JTextField(20); p6.add(l23); p6.add(t22); p6.add(l24); p6.add(t23); p6.add(l25); p6.add(t24); p6.add(l26); p6.add(t25); p6.add(l27); p6.add(t26); p6.setLayout( new GridLayout( 9, 2 ) ); p6.setBorder(new javax.swing.border. TitledBorder(new javax.swing.border.EtchedBorder())); x1.add(name4, p6); // End of 4th x1 String name5 = "Incomes"; JLabel l28 = new JLabel("Basic Pay :"); JLabel l29 = new JLabel("Overtime Pay :"); JLabel l30 = new JLabel("Holiday Pay :"); JLabel l31 = new JLabel("Others :"); JLabel l32 = new JLabel("Net Pay :"); JLabel l33 = new JLabel(); JLabel l34 = new JLabel(); JLabel l35 = new JLabel(); final JTextField t27 = new JTextField(20); final JTextField t28 = new JTextField(20); final JTextField t29 = new JTextField(20); final JTextField t30 = new JTextField(20); final JTextField t31 = new JTextField(20); JButton com = new JButton("Compute"); s5.add(l28); s5.add(t27); s5.add(l29); s5.add(t28); s5.add(l30); s5.add(t29); s5.add(l31); s5.add(t30); s5.setLayout( new GridLayout( 4, 2 ) ); s5.setBorder(new javax.swing.border. TitledBorder(new javax.swing.border.EtchedBorder())); s6.add(l32); s6.add(t31); s6.add(l33); s6.add(com); s6.add(l34); s6.add(l35); s6.setLayout( new GridLayout( 4, 2 ) ); s6.setBorder(new javax.swing.border. TitledBorder(new javax.swing.border.EtchedBorder())); p7.add(s5); p7.add(s6); p7.setLayout( new GridLayout( 2, 1 ) ); x1.add(name5, p7); // End of 5th x1 JPanel f1 = new JPanel(); JLabel c1 = new JLabel(); JLabel c2 = new JLabel(); JButton y1 = new JButton("Add"); JButton y2 = new JButton("Find"); JButton y3 = new JButton("Update"); JButton y4 = new JButton("Cancel"); f1.add(c1); f1.add(y1); f1.add(y2); f1.add(y3); f1.add(y4); f1.add(c2); f1.setLayout( new GridLayout() ); c.add(x1, BorderLayout.CENTER); c.add(f1, BorderLayout.SOUTH); Dimension sd = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setLocation(sd.width / 2 - 480 / 2, sd.height / 2 - 400 / 2); setResizable(false); setSize(480, 400); show(); y1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // - - - - Here's the Problem ? - - - - // try { statement = connection.createStatement(); String query = "INSERT INTO Record (" + "Firstname" + ") VALUES ('" + t1.getText() + "')"; int result = statement.executeUpdate( query ); statement.close(); } catch ( SQLException sqlex ) { sqlex.printStackTrace(); System.out.println(sqlex.toString()); } } }); com.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { float a1 = Float.parseFloat(t22.getText()); float a2 = Float.parseFloat(t23.getText()); float a3 = Float.parseFloat(t24.getText()); float a4 = Float.parseFloat(t25.getText()); float a5 = Float.parseFloat(t26.getText()); float a6 = Float.parseFloat(t27.getText()); float a7 = Float.parseFloat(t28.getText()); float a8 = Float.parseFloat(t29.getText()); float a9 = Float.parseFloat(t30.getText()); float sal; float ded; float sum; sal = a6 + a7 + a8 + a9; ded = a1 + a2 + a3 + a4 + a5; sum = sal - ded; DecimalFormat change = new DecimalFormat("0.00"); String ans = change.format(sum); t31.setText("P "+ans); } }); } public static void main( String args[] ) { Database app = new Database(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }
|
 |
Blake Minghelli
Ranch Hand
Joined: Sep 13, 2002
Posts: 331
|
|
// - - - - Here's the Problem ? - - - - // try { statement = connection.createStatement();
The problem is that your connection code above this uses a vaiable called "connect". The "connection" variable you're trying to use here is not initialized, that's why you're getting the NullPointerException. So, change the code to this and it should work: Good luck.
|
Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
|
 |
 |
|
|
subject: Adding Record Problem in JDBC ?
|
|
|