无法将数据保存到数据库

时间:2013-03-16 05:42:29

标签: java sql database

我不知道该程序有什么问题,但每次点击保存的按钮都会说数据未保存。

这是有问题的代码:

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        String TicketNum = "FC-0";
        int count= 1;

        try {

             host = "jdbc:derby://localhost:1527/Ticket Reservation";
             username = "cmpe325";
             pass = "final";

             con = DriverManager.getConnection(host, username,pass);

             stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            String sql = "select * from CMPE325.TICKET_RESERVATION";
            rs = stmt.executeQuery(sql);


       while(rs.next())
             {
                 String TicketCal = rs.getString("TICKET_NUM");
                 String FirstName = rs.getString("FIRST_NAME");
                 String MiddleName = rs.getString("MIDDLE_NAME");
                 String LastName = rs.getString("LAST_NAME");
                 String Address = rs.getString("ADDRESS");
                 String Residence = rs.getString("RESIDENCE_NUMBER");
                 String Offuce= rs.getString("OFFUCE_NUMBER");
                 String Mobile = rs.getString("MOBILE"); 

                //NumPassenger = rs.getDouble("PASSENGER_NUM");
                Double Adults = rs.getDouble("ADULTS_NUM");
                Double Kids= rs.getDouble("KIDS_NUM");
                String accomo= rs.getString("ACCOMODATION");
                Double Stud = rs.getDouble("DISCOUNT_STUDENT");
                Double Senfee = rs.getDouble("DISCOUNT_SENIORCITIZEN");              
                Double kid = rs.getDouble("KIDS_FEE");
                Double normal = rs.getDouble("NORMAL_FEE");
                Double pay  = rs.getDouble("TOTAL_PAYMENT");

                count ++;
             } 

        }

    catch(SQLException err)
    {
        JOptionPane.showMessageDialog(this,err.getMessage());
    }

         TicketNum += count;
         String FirstName = txtBoxFirst.getText();
         String MiddleName = txtBoxMiddle.getText();
         String LastName = txtBoxLast.getText();
         String Address = txtBoxAddress.getText();
         String Residence = txtBoxResidence.getText();
         String Offuce = txtBoxOffice.getText();
         String Mobile = txtBoxMobile.getText();


        Double Adults = Double.parseDouble(String.valueOf(txtBox2.getText()));
        Double Student = Double.parseDouble(String.valueOf(txtBox4.getText()));
        Double Kids = Double.parseDouble(String.valueOf(txtBox3.getText()));
        Double SeniorCitizen = Double.parseDouble(String.valueOf(txtBox5.getText()));

        String accomo = accomodation;
        Double normal = NumAdults;
        Double kid = NumKids;
        Double Stud = disStudent;
        Double Senfee = disSeniorCitizen;
        Double pay = totalpay;

     try
     {

       rs.moveToInsertRow();

       rs.updateString("TICKET_NUM", TicketNum);
       rs.updateString("FIRST_NAME",FirstName);
       rs.updateString("MIDDLE_NAME",MiddleName);
       rs.updateString("LAST_NAME",LastName);
       rs.updateString("ADDRESS", Address);
       rs.updateString("RESIDENCE_NUMBER",Residence);
       rs.updateString("OFFUCE_NUMBER",Offuce);
       rs.updateString("MOBILE", Mobile); 

       //rs.updateDouble("PASSENGER_NUMBER", NumPassenger);
       rs.updateDouble("ADULTS_NUMBER", Adults);
       rs.updateDouble("STUDENT_NUMBER", Student);
       rs.updateDouble("SENIORCITIZEN_NUMBER", SeniorCitizen);
       rs.updateDouble("KIDS_NUMBER", Kids);
       rs.updateString("ACCOMODATION", accomo);
       rs.updateDouble("DISCOUNT_STUDENT", Stud);
       rs.updateDouble("DISCOUNT_SENIORCITIZEN",Senfee);
       rs.updateDouble("KIDS_FEE", kid);
       rs.updateDouble("NORMALFEE", normal);
       rs.updateDouble("TOTAL_PAYMENT", pay);

       rs.insertRow( ); 
       stmt.close();
       rs.close();


       JOptionPane.showMessageDialog(this,"DATA SAVED Ticket Number is:"+ TicketNum);

     }

     catch(SQLException err)
    {

        JOptionPane.showMessageDialog(this,"DATA NOT SAVED PLEASE TRY AGAIN");

    }

    }  

Stacktrace

error:
run:
java.sql.SQLIntegrityConstraintViolationException: Column 'PASSENGER_NUMBER'  cannot accept a NULL value.
    at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
    at org.apache.derby.client.am.ResultSet.insertRow(Unknown Source)
    at FinalPro.java.Reserve.btnSaveActionPerformed(Reserve.java:1135)
    at FinalPro.java.Reserve.access$900(Reserve.java:11)
    at FinalPro.java.Reserve$10.actionPerformed(Reserve.java:605)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:155)
    at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182)
    at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:221)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:219)
    at java.awt.Dialog.show(Dialog.java:1077)
    at java.awt.Component.show(Component.java:1651)
    at java.awt.Component.setVisible(Component.java:1603)
    at java.awt.Window.setVisible(Window.java:1014)
    at java.awt.Dialog.setVisible(Dialog.java:1003)
    at FinalPro.java.FinalPro.MenuReserveActionPerformed(FinalPro.java:99)
    at FinalPro.java.FinalPro.access$000(FinalPro.java:3)
    at FinalPro.java.FinalPro$1.actionPerformed(FinalPro.java:34)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Caused by: org.apache.derby.client.am.SqlException: Column 'PASSENGER_NUMBER'  cannot accept a NULL value.
    at org.apache.derby.client.am.Statement.completeExecute(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.parseEXCSQLSTTreply(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.readExecute(Unknown Source)
    at org.apache.derby.client.net.StatementReply.readExecute(Unknown Source)
    at org.apache.derby.client.net.NetPreparedStatement.readExecute_(Unknown Source)
    at org.apache.derby.client.am.ResultSet.readInsertRow(Unknown Source)
    at org.apache.derby.client.am.ResultSet.insert(Unknown Source)
    at org.apache.derby.client.am.ResultSet.insertRowX(Unknown Source)
    ... 87 more
BUILD SUCCESSFUL (total time: 24 seconds)

1 个答案:

答案 0 :(得分:4)

查看错误SQLIntegrityConstraintViolationException: Column 'PASSENGER_NUMBER' cannot accept a NULL value

您正在传递一个空值,该值在数据库中不为空。

SOLUTION:您需要为该字段传递值。或者在数据库中删除非空约束。