Java SQLException: 索引处缺少 IN 或 OUT 参数:: 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23945427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLException: Missing IN or OUT parameter at index:: 1
提问by user3689611
I've made certain jtextfield, jRadioButtons, and some text areas. I want them to insert to data entered in these SQL Database. I've made the following code:
我制作了某些 jtextfield、jRadioButtons 和一些文本区域。我希望它们插入到这些 SQL 数据库中输入的数据中。我做了以下代码:
Connection conn = null;
//Statement stm = null;
PreparedStatement pst = null;
ResultSet rs = null;
try{
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("Driver Loaded.");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "hr", "hr");
System.out.println("Driver Connected.");
pst=conn.prepareStatement("insert into directorsdb (DIRECTOR_NAME, DOB, DIN, PAN, UIN, BIRTH_PLACE, PASSPORT, DRIVING_LIC, NATIONALITY, OCCUPATION, OCCUPATION_COMPANY, DESIGNATION, QUALIFICATION, ADDRESS_OFF, ADDRESS_RESI, CONTACT_HOME, CONTACT_WORK, CONTACT_MOBILE, EMAIL_PERSONAL, EMAIL_WORK, EMAIL_OTHER, SECRETARY_NAME, SECRETARY_CONTACT, SECRETARY_EMAIL, DSC, DSC_EXPIRY, FATHER_NAME, MOTHER_NAME, APPOINTMENT_DATE, POSITION_BOARD, CESSATION_DATE, CESSATION_REASON, PROMOTER, INDEPENDENT_DIRECTOR, ABOUT)"
+ "values("+jFormattedTextField1.getText()+"','"+jDateChooser1.getDate()+"','"+DIN.getText()+"','"+PAN.getText()+"','"+UIN.getText()+"','"+jFormattedTextField4.getText()+"','"+passportno.getText()+"','"+dlicense.getText()+"','"+jFormattedTextField5.getText()+"','"+jFormattedTextField6.getText()+"','"+jFormattedTextField7.getText()+"','"+jFormattedTextField8.getText()+"','"+jFormattedTextField9.getText()+"','"+officeadd.getText()+"','"+residentialadd.getText()+"','"+jFormattedTextField12.getText()+"','"+jFormattedTextField13.getText()+"','"+jFormattedTextField14.getText()+"','"+jFormattedTextField15.getText()+"','"+jFormattedTextField16.getText()+"','"+jFormattedTextField17.getText()+"','"+jFormattedTextField18.getText()+"','"+jFormattedTextField19.getText()+"','"+jFormattedTextField20.getText()+"','"+buttonGroup1.getSelection().getActionCommand()+"','"+jDateChooser2.getDate()+"','"+jFormattedTextField22.getText()+"','"+jFormattedTextField23.getText()+"','"+jDateChooser3.getDate()+"','"+jFormattedTextField25.getText()+"','"+jDateChooser4.getDate()+"','"+jTextArea2.getText()+"','"+buttonGroup2.getSelection().getActionCommand()+"','"+buttonGroup3.getSelection().getActionCommand()+"','"+jTextArea1.getText()+"',')");
pst.execute();
if(pst.execute()==true){
JOptionPane.showMessageDialog( null, "Saved Successfully!!");
}else{
JOptionPane.showMessageDialog( null, "Saved Successfully!!");
}
}catch(SQLException e){
System.err.println(e.fillInStackTrace());
}
catch(ClassNotFoundException ex){
System.err.println(ex);
}
The exception I'm getting is:
我得到的例外是:
SQLException: Missing IN or OUT parameter at index:: 1
Help me in rectifying the same.
帮我纠正同样的问题。
回答by sendon1982
You have more values provided than the column count in your SQL.
您提供的值多于 SQL 中的列数。
When using PrepareStatement, please use place holder in the SQL to avoid SQL injection.
使用 PrepareStatement 时,请在 SQL 中使用占位符,以避免 SQL 注入。
String selectSQL = "SELECT USER_ID, USERNAME FROM DBUSER WHERE USER_ID = ? and NAME = ?";
PreparedStatement preparedStatement = dbConnection.prepareStatement(selectSQL);
preparedStatement.setInt(1, 1001);
preparedStatement.setString(2, "test");
ResultSet rs = preparedStatement.executeQuery(selectSQL);
When it is update:
更新时:
String updateTableSQL = "UPDATE DBUSER SET USERNAME = ? WHERE USER_ID = ?";
PreparedStatement preparedStatement = dbConnection.prepareStatement(updateTableSQL);
preparedStatement.setString(1, "value");
preparedStatement.setInt(2, 1001);
preparedStatement .executeUpdate();