Rehan
Rehan

Reputation: 195

Cannot put entry in the mysql database

private void B_save_pActionPerformed(java.awt.event.ActionEvent evt)      {                                         

    try{

        String db = "jdbc:mysql://localhost/ar";
        Connection con = DriverManager.getConnection(db, "rehan", "1234");
        PreparedStatement ps;
        String sql = "INSERT INTO purchase (item_type, md_by, model, selling_price, purchase_price, purchase_date, item_image, vouch_no, vouch_date) VALUES ('?', '?', '?', ?, ?, '?', ?, ?, '?');" ;
        ps = con.prepareStatement(sql);


            ps.setString(1 , txt_itype_p.getText());
            ps.setString(2, txt_md_by_p.getText());
            ps.setString(3, txt_model_p.getText());
            ps.setString(4, txt_s_price_p.getText());
            ps.setString(5 , txt_p_price.getText());
            ps.setString(6, null);
            ps.setString(7, null);
            ps.setString(8, txt_vouch_p.getText());
            ps.setString(9 , null);

            ps.execute();



    }
    catch(SQLException ex){
        JOptionPane.showMessageDialog(null, ex);

}   
    DefaultTableModel tblModel = (DefaultTableModel) Table_p.getModel();
    if(!txt_itype_p.getText().trim().equals(""))     {
    tblModel.addRow(new Object[]{tbl_sr, txt_itype_p.getText(),txt_model_p.getText(),txt_md_by_p.getText(),txt_date_p.getText(),txt_p_qty.getText(),txt_p_price.getText(),txt_s_price_p.getText()});   
    txt_itype_p.setText(null);
    txt_ipath.setText(null);
    txt_itype_p.setText(null);
    txt_md_by_p.setText(null);
    txt_model_p.setText(null);
    txt_p_date.setText(sdf.format(date));
    txt_p_price.setText(null);
    txt_p_qty.setText(null);
    txt_s_price_p.setText(null);

    }
    tbl_sr = tbl_sr +1;    
    saved = true;/*If someone accidently click the close button, system will
    confirm while getting the value of this bolean variable*/
}                                        

This is my code for the save button which is supposed to show the entries on a JTable in a JFrame and also saves the entries into mysql database in 'purchase' table, but the problem is it shows the entries on the JTable but doesnot saves in database. I have same code to create new users which saves username, password and account type in the login table in database and that code is working fine. I have a feeling that this problem is due to some data types issue but i am not confirmed. I am using Netbeans and mysql database. I tried to send 'null' to date and image data types but still its not working. Someone please help me.

Upvotes: 1

Views: 60

Answers (1)

Luke Woodward
Luke Woodward

Reputation: 64959

This line has a problem:

String sql = "INSERT INTO purchase (item_type, md_by, model, selling_price, purchase_price, purchase_date, item_image, vouch_no, vouch_date) VALUES ('?', '?', '?', ?, ?, '?', ?, ?, '?');" ;

The problem is the five ?s that are surrounded by single quotes. JDBC does not recognise these as placeholders and assumes you want to insert a string containing just a ? character into the item_type, md_by, model, purchase_date and vouch_date columns.

The fix is to remove all of the single quotes around the ? marks:

String sql = "INSERT INTO purchase (item_type, md_by, model, selling_price, purchase_price, purchase_date, item_image, vouch_no, vouch_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);" ;

Upvotes: 1

Related Questions