I want to update my database but the problem is that the driver is not taking any values from the preparedStatement.
I am getting an error :
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] You must enter a value in the 'ClassicRoom.Occupy' field.
Press any key to continue...

Here is the ActionPerformed code :

public void actionPerformed(ActionEvent ae)
    {
            String str=ae.getActionCommand();   
        if(str.equals("Save"))
            {
                try
            {   
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              Connection con=DriverManager.getConnection("JDBC:ODBC:HotelT");
              PreparedStatement st=null;

            if(room.getSelectedItem()=="Classic Rooms")
            {
                for(int j = 0; j < checkList.size(); j++)
                {
                 JCheckBox cb = checkList.get(j);
                 if(cb.isSelected())
                 {
                    st=con.prepareStatement("update ClassicRoom set Occupy = ? AND RegNo = ? where RoomNo = ?");
                    st.setInt(1,1);
                    st.setInt(2,Integer.parseInt(reg_no.getText()));
                    st.setInt(3,Integer.parseInt(cb.getText()));
                    st.executeUpdate();
                 }
                }
            } 

            st.close();
            con.close();
            }

            Catch(Exception e)
            {
            System.out.println(e);
            }

            try
            {

                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              Connection con=DriverManager.getConnection("JDBC:ODBC:HotelT");

               int val=Integer.parseInt(reg_no.getText());
               String val1=name.getText();
               String val2=addr.getText();
               String val3=tel.getText();
               String val4=date_in.getText();
             String val5=room.getSelectedItem();
             int val6=Integer.parseInt(adult.getSelectedItem());
             int val7=Integer.parseInt(child.getSelectedItem());
             int val8=Integer.parseInt(no_rooms.getText());
             String val9="";
              for(int i = 0; i < checkList.size(); i++){
                 JCheckBox cb = checkList.get(i);
                 if(cb.isSelected()){
                    val9 += cb.getText();
                    val9 += ",";
                 }

            }
             //String val9=roomno.getSelectedItem();

            //String b="insert into entry values ("+val+","+val1+",'"+val2+"','"+val3+"','"+val4+"','"+val5+"',"+val6+")";
            // int exe=st.executeUpdate(b);
            // String s="full";
            // b="update room set state='"+s+"',regno="+val+" where room="+val1+" ";
            // exe=st.executeUpdate(b);
                PreparedStatement pst=con.prepareStatement("insert into Check_In values (?,?,?,?,?,?,?,?,?,?)");

                pst.setInt(1,val);
                pst.setString(2,val1);
                pst.setString(3,val2);
                pst.setString(4,val3);
                pst.setString(5,val4);
                pst.setString(6,val5);
                pst.setInt(7,val6);
                pst.setInt(8,val7);
                pst.setInt(9,val8);
                pst.setString(10,val9);

            pst.executeUpdate();
            pst.close();
            con.close();

            setVisible(false);
            Menu a=new Menu();

                }
                catch(Exception e)
            {
            System.out.println(e);
            }

            }

            if(str.equals("Menu"))
            {
                setVisible(false);
                Menu b=new Menu();
            }
            if(str.equals("Exit"))
            {
              System.exit(0);
            }

    }

Note: while the below statement for insert works just fine.... but the above update statement does not work... I have been banging my head coz the syntax seems correct for prepared statement plz Help... Its Urgent...

Recommended Answers

All 3 Replies

I don't know much Java but by the looks of the error you're getting it looks like the ClassicRoom.Occupy field should never be null but is null at the point the error is thrown.

But as you see i am already passing 1 in the Occupy field....
coz the occupy field in the db has only two values 0 or 1.. nd i have to set it to one...
even if I change the required setting in the db it still passes null values but that it does it in the correct row...

Had to finally solve the problem using Udateable Reultset

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("JDBC:ODBC:HotelT");
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
int occ=1;

            if(room.getSelectedItem()=="Classic Rooms")
            {
                for(int j = 0; j < checkList.size(); j++)
                {
                 JCheckBox cb = checkList.get(j);
                 if(cb.isSelected())
                 {
                    ResultSet rs=st.executeQuery("Select Occupy,RegNo from ClassicRoom where RoomNo ="+Integer.parseInt(cb.getText())+"");
                    rs.first();
                    rs.updateInt("Occupy",occ);
                    rs.updateInt("RegNo",Integer.parseInt(reg_no.getText()));
                    rs.updateRow();
                    rs.close();
                 }
                }
            } 


            con.close();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.