0

i am trying to make Java ucanaccess program and for some reasons i am not able to insert record to database and have appeared some error messages as following:

package lcp_v1;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;

public class DB2 extends JFrame implements ActionListener {

    private JPanel contentPane;
    private JTextField id_tt;
    private JTextField name_tt;

    Connection con;
    Statement st;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    DB2 frame = new DB2();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public DB2() {
        try {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 577, 550);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);

            JLabel lblEmployeeId = new JLabel("employee id");
            lblEmployeeId.setBounds(39, 53, 123, 23);
            contentPane.add(lblEmployeeId);

            id_tt = new JTextField("");
            id_tt.setBounds(161, 50, 136, 29);
            contentPane.add(id_tt);
            id_tt.setColumns(10);

            JLabel lblName = new JLabel("Name");
            lblName.setBounds(39, 118, 71, 23);
            contentPane.add(lblName);

            name_tt = new JTextField("");
            name_tt.setColumns(10);
            name_tt.setBounds(161, 115, 136, 29);
            contentPane.add(name_tt);

            JButton insert = new JButton("Insert");
            insert.setBounds(39, 388, 105, 30);
            contentPane.add(insert);
            insert.addActionListener(this);

            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            String dataSource = "jdbc:ucanaccess://D:/Database.accdb";
            con = DriverManager.getConnection(dataSource, "", "");
            Statement st = con.createStatement();

        } catch (Exception e) {
        }
    }//end DB2 constructor      

    public void actionPerformed(ActionEvent ae) {

        try {

            String id = id_tt.getText();
            String name = name_tt.getText();
            String s1 = "INSERT INTO Table1 (ID, Name) VALUES (id,name)";
            st.executeUpdate(s1);
            id_tt.setText("");
            name_tt.setText("");

        } catch (SQLException e1) {
            e1.printStackTrace();
        }

    }
}

error messages

please tell me what error did i make.thanks a lot

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

1 Answers1

0

First : you get NullPointException because you don't change the value of st in :

Statement st = con.createStatement();

You just create another variable with the same name of that in your class, so instead you have to use :

st = con.createStatement();//Inisialize it, don't create and inisialize another variable.

read more about What is a NullPointerException, and how do I fix it?


Second : It seems that you don't insert any thing, your query is just a string which can give you SQL exception.


Third : this is the main objectif of this answer, so instead of Statement which can gives you syntax error or SQL Injection i will suggest to use Prepared Statement for example :

int id = Integer.parseInt(id_tt.getText());//get the id
int name = name_tt.getText();//get the name
try (PreparedStatement insert = connection.prepareStatement(
        "INSERT INTO Table1 (ID, Name) VALUES (?, ?))")) {

    insert.setInt(1, id);//set the id to your statement
    insert.setString(2, name);//set the name to your statement
    insert.executeUpdate();//execute the statement

    id_tt.setText("");
    name_tt.setText("");
}

Note, generaly the id should be primary key and auto_increment, so i think you have to look to the design of your database.

Graham
  • 7,431
  • 18
  • 59
  • 84
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140