0

I want to insert data into my oracle table using netbeans text fields and there is a problem with this code. When this screen execute it gives me an exception:

java.sql.SQLException:Invalid column index

Please help me as soon as possible.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
    conn = javadb.ConnectDb();

    try{
        String sql = "insert into addbook (id, title, isbn, author)"
                + " values(null,'','','');";
        pst = (OraclePreparedStatement) conn.prepareStatement(sql);
        pst.setString(1, title.getText());
        pst.setString(2, isbn.getText());
        pst.setString(3, author.getText());
        rs = (OracleResultSet) pst.executeQuery();

    }catch(SQLException | HeadlessException e){
        JOptionPane.showMessageDialog(null, e);
    }
}                                        
1
  • Because you are setting parameters, but have no placeholders in the values clause. Remove the literals and replace with ? Commented Dec 12, 2013 at 2:06

2 Answers 2

1

conn = JavaDbCon.ConnecrDb();

try{
    String sql = "insert into Balance (ecode,ltype,rol,ldate) values(?,?,?,?)";" // there he show me error

    pst = (OraclePreparedStatement) conn.prepareStatement(sql);
    pst.setString(1,'NULL'); // also here
    pst.setString(2, ltype.getText());
    pst.setString(3, rol.getText());
    pst.setString(4, ldate.getText());
    rs = (OracleResultSet) pst.executeQuery();

}catch(SQLException | HeadlessException e){ // here also
    JOptionPane.showMessageDialog(null, e);
}
Sign up to request clarification or add additional context in comments.

1 Comment

i coment on those lines where he show me errors plz reply as soon as possible
0

This should work, try this.

   conn = javadb.ConnectDb();

        try{
            String sql = "insert into addbook (id, title, isbn, author) values(?,?,?,?)";
            pst = (OraclePreparedStatement) conn.prepareStatement(sql);

            pst.setString(1,'NULL');
            pst.setString(2, title.getText());
            pst.setString(3, isbn.getText());
            pst.setString(4, author.getText());
            rs = (OracleResultSet) pst.executeUpdate(sql);

        }catch(SQLException | HeadlessException e){
        JOptionPane.showMessageDialog(null, e);
        }
    }        

4 Comments

I try this but it is giving me error on this line rs = (OracleResultSet) pst.executeUpdate(sql); Inconvertible types Required OracleResultSet found int
Hats off man. I was doing wrong by using semicolon at values(?,?,?,?);"; I pasted your code and problem has solved.
You can probably just leave out the first setString if the id is automatically generated.
@Bill, its just for understanding. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.