gpt4 book ai didi

java.sql.SQLException:索引::1 处缺少 IN 或 OUT 参数。我该如何修复这个异常?

转载 作者:行者123 更新时间:2023-12-01 12:45:32 28 4
gpt4 key购买 nike

为什么会发生这种情况以及如何解决它?

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)

最佳答案

在你的代码中额外的?存在。请将您的代码更改为此

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

关于java.sql.SQLException:索引::1 处缺少 IN 或 OUT 参数。我该如何修复这个异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24734836/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com