java.sql.SQLException:在index :: 1处缺少IN或OUT参数。如何修复此异常?

时间:2014-07-14 10:48:15

标签: java sql

为什么会发生这种情况,我该如何解决?

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)  

1 个答案:

答案 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 Query中我添加了一个? ,请将您的代码更改为

 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++)
             {