2014-07-14 78 views
0

爲什麼會發生這種情況,我該如何解決?java.sql.SQLException:在index :: 1處缺少IN或OUT參數。我該如何解決這個異常?

public void insertXML() 
{ 
    pars.getParsare(); 
    Hashtable<Integer,ArrayList<Object>> has = pars.getHashTable(); 
    ArrayList<String> query = new ArrayList<String>(); 
    Enumeration en = has.elements(); 

    while(en.hasMoreElements()) 
    { 
     ArrayList<Object> aux = (ArrayList<Object>)en.nextElement(); 
     String name = ""; 

     Element e = (Element) aux.get(0); 
     name = e.getName(); 
     System.out.println("NUMEEE " + e.getName()); 
     ArrayList<String> col_name = getColName(name.toUpperCase()); 
     ArrayList<String> col_type = getColType(name.toUpperCase()); 

     String col_name_string = ""; 
     String val = "?"; 

     for(int i=0;i<col_name.size()-1;i++) 
     { 
      col_name_string = col_name_string + col_name.get(i).toLowerCase() + ", "; 
      val = val + ",?"; 
     } 

     col_name_string = col_name_string + col_name.get(col_name.size()-1).toLowerCase(); 

     Connection dbConnection = null; 
     PreparedStatement preparedStatement = null; 
     System.out.println("INSERT INTO "+ name +"("+ col_name_string +") VALUES" 
       + "("+ val +")"); 
     String insertTableSQL = "INSERT INTO "+ name +"("+ col_name_string +") VALUES" 
       + "("+ val +")"; 


     for(int i=1;i<aux.size();i++) 
     { 
       dbConnection = bd.getConnection(); 
       try { 
        preparedStatement = dbConnection.prepareStatement(insertTableSQL); 
       } catch (SQLException e3) { 
        e3.printStackTrace(); 
       } 
       Attribute a = (Attribute)aux.get(i); 
       System.out.println(a.getValue()); 

       if(col_type.get(i-1).equals("NUMBER")) 
       { 
        try { 
         System.out.println("intra in number: " + a.getDoubleValue()); 

         preparedStatement.setDouble(i, a.getDoubleValue()); 
        } catch (DataConversionException e1) { 
         e1.printStackTrace(); 
        } catch (SQLException e1) { 
         e1.printStackTrace(); 
        } 

       } 
       if(col_type.get(i-1).equals("VARCHAR2")) 
       { 
        System.out.println("intra in varchar " + a.getValue()); 
        try { 
         preparedStatement.setString(i, a.getValue()); 
        } catch (SQLException e1) { 
         e1.printStackTrace(); 
        } 

       } 

       if(col_type.get(i-1).equals("DATE")) 
       { 
        System.out.println("intra in date: " + a.getValue()); 
        String s = a.getValue(); 
        String []s2 = new String[10]; 
        s2 = s.split("-"); 

        Time d = new Time(Integer.parseInt(s2[0]),Integer.parseInt(s2[1]),Integer.parseInt(s2[2])); 

        try { 
         preparedStatement.setTime(i, d); 
        } catch (SQLException e1) { 
         e1.printStackTrace(); 
        } 
       } 
     } 

     try { 
      preparedStatement.executeUpdate(); 
      System.out.println("MERGEEE"); 
     } catch (SQLException e1) { 
      e1.printStackTrace(); 
     } 

      System.out.println("Gata inserarea"); 

    } 

它提供了以下錯誤

java.sql.SQLException: Missing IN or OUT parameter at index:: 1 
    at oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:1821) 
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3579) 
    at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3665) 
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1352) 
    at BazaDeDateAcces.insertXML(BazaDeDateAcces.java:123) 
    at Main.main(Main.java:46) 
+0

你可以給你想要執行SQL查詢? –

+0

我想從xml插入數據庫。 –

+0

例如:

回答

1

在你的代碼中加入額外?請將您的代碼更改爲此

StringBuilder val = new StringBuilder(); 
    for(int i=0;i<col_name.size()-1;i++) 
    { 
     col_name_string = col_name_string + col_name.get(i).toLowerCase() + ", "; 
     val .append(",?"); 
    } 

StringBuilder可能會提高您的性能。

而在SQL查詢中我添加了一個?請改變你的代碼,這

String insertTableSQL = "INSERT INTO "+ name +"("+ col_name_string +") VALUES" 
       + "(?"+ val +")"; 

就拿事先準備好的聲明退出循環,改變代碼的這個

  dbConnection = bd.getConnection(); 
      try { 
       preparedStatement = dbConnection.prepareStatement(insertTableSQL); 
      } catch (SQLException e3) { 
       e3.printStackTrace(); 
      } 
      //then your loop 
      for(int i=1;i<aux.size();i++) 
      { 
+0

謝謝,我沒有注意到。我改變了它,但它會拋出相同的異常 –

+0

.... VALUES(?,?,?,?,?,?):檢查您是否通過準備好的語句分配了總共6個值。它可能缺少一個或兩個值 –

+0

好吧, 感謝您的回答。我發現了這個問題 –

相關問題