0

This is my table in oracle:

CREATE TABLE  "STUDENT" 
   (    "STU_ID" NUMBER(10,0), 
    "NAME" VARCHAR2(20), 
    "MOBILENO" NUMBER(10,0), 
    "EMAIL" VARCHAR2(50), 
    "ADDRESS" VARCHAR2(100), 
    "STREAM" CHAR(50), 
    "SEMESTER" CHAR(50), 
    "DATE_OF_BIRTH" DATE, 
     CONSTRAINT "PK_STU_ID" PRIMARY KEY ("STU_ID") ENABLE
   ) ;

I want to store student information in the above table by pressing the add button in eclipse.getting missing expression error when tries to execute this line"st.executeUpdate(sql);",here is the code:

JLabel lblAdd = new JLabel(" Add");
        lblAdd.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                Clock=new Timer(5000,null);
                Clock.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        // TODO Auto-generated method stub
                        try {
                            Connection con=DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE","ADMIN","admin");
                            Statement st=con.createStatement();
                            ResultSet rslt=st.executeQuery("select * from student");
                            ResultSet stu_seq=st.executeQuery("select student_sequence.nextval from DUAL");
                            System.out.println("getting resultset");
                            if(stu_seq.next())
                            {   System.out.println(stu_seq.getInt(1));

                                String name=txtNm.getText().trim();
                                String email=txtEmailid.getText().trim();
                                String mobile=txtMobileNo.getText().trim();


                                String sql="INSERT INTO student(STU_ID,NAME,MOBILENO,EMAIL,ADDRESS,DATE_OF_BIRTH,STREAM,SEMESTER) VALUES ("+stu_seq.getInt(1)+",'"+name+"','"+mobile+"','"+email+"',";

                                sql+="'"+txtPrmntAdd.getText().trim()+"',";

                                sql+="'"+cmbDay.getSelectedItem()+"'";
                                sql+="'"+cmbMnth.getSelectedItem()+"'";
                                sql+="'"+cmbYr.getSelectedItem()+"',";

                                sql+="'"+cmbRgstrStrm.getSelectedItem()+"',";

                                sql+="'"+cmbRgstrSem.getSelectedItem()+"',";   


                                sql+=")";

                                st.executeUpdate(sql);
                               // System.out.println("after executing");
                            }    

                            System.out.println("Inserted into database");
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        frameReset();
                        //lblStatus.setText("data saved successfully");
                        //lblAni.setVisible(false);
                        Clock.stop();
                    }
                });
                Clock.start();
                //lblStatus.setVisible(true);
                //lblAni.setVisible(true);      
            }
        });
        lblAdd.setBounds(172, 340, 60, 18);
        panelRegister.add(lblAdd);
        lblAdd.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        lblAdd.setForeground(new Color(0, 51, 102));
        lblAdd.setFont(new Font("Tw Cen MT", Font.PLAIN, 16));
        lblAdd.setIcon(new ImageIcon(Student.class.getResource("/img/Add-New-16.png")));

want to store student information in the table.where I am doing wrong so that I am getting missing expression error?

  • Print out the content of your `sql` string before running `executeUpdate` and run that directly on your database manually. Does it fail? If so, then your query is obviously wrong which is what seems to be happening. – Kon May 09 '16 at 19:56

1 Answers1

3

This fragment of your code is wrong:

 sql+="'"+cmbRgstrSem.getSelectedItem()+"',";   
                                sql+=")";

because it gives the following result at the end of the INSERT statement:

INSERT ....VALUES( ...... , 'someValue',)

The database expects next value after the last comma, but there is nothing there.


By the way, use a PreparedStatement to do such a task,
see this link for details - this is a tutorial from Oracle: "Using Prepared Statements" : https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

Study also this answer to know why you should use PreparedStatement:
When should we use a PreparedStatement instead of a Statement?

Community
  • 1
  • 1
krokodilko
  • 35,300
  • 7
  • 55
  • 79