简体   繁体   中英

Insert data in to oracle table using netbeans

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);
    }
}                                        

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);
}

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);
        }
    }        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM