我已经制作了某些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);
}
我得到的例外是:
SQLException: Missing IN or OUT parameter at index:: 1
帮我纠正错误。
答案 0 :(得分:2)
您提供的值多于SQL中的列数。
使用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);
更新时:
String updateTableSQL = "UPDATE DBUSER SET USERNAME = ? WHERE USER_ID = ?";
PreparedStatement preparedStatement = dbConnection.prepareStatement(updateTableSQL);
preparedStatement.setString(1, "value");
preparedStatement.setInt(2, 1001);
preparedStatement .executeUpdate();