0

BOX_ID 列是整数其余是 varchar

sql="UPDATE TOYS SET ?=?,?=?,?=? WHERE NAME=? AND LICENSE=? AND BOX_ID=? ";
p_statement2=connection.prepareStatement(sql);
p_statement2.setString(1, "NAME");
p_statement2.setString(2, "toy1");
p_statement2.setString(3, "VENDOR");
p_statement2.setString(4, "vendor1");
p_statement2.setString(5, "SIZE");
p_statement2.setString(6, "size1");
p_statement2.setString(7, "toyx");
p_statement2.setString(8, "license1");
p_statement2.setInt(9, 11);
try
{
p_statement2.executeUpdate();
}
catch(Exception kl)
{
kl.toString();
kl.printStackTrace();
p_statement2.close();
}

得到以下异常 -

java.sql.SQLException: ORA-01747: 无效的 user.table.column、table.column 或列规范

4

4 回答 4

1

It is expected. When you bind parameter using setString method, parameter value will be escaped and put in single/double quotes. In this case, your column name will also be put in single/double quotes and your database will throw error in this case.

于 2013-04-29T06:27:46.917 回答
0

Why did you set column names like ?? You can't do this. Try something like:

sql="UPDATE TOYS SET Name=?, Vendor=?, Size=? WHERE NAME=? AND LICENSE=? AND BOX_ID=? ";
p_statement2=connection.prepareStatement(sql);
p_statement2.setString(1, "toy1");
p_statement2.setString(2, "vendor1");
p_statement2.setString(3, "size1");
p_statement2.setString(4, "toyx");
p_statement2.setString(5, "license1");
p_statement2.setInt(6, 11);
try
{
   p_statement2.executeUpdate();
}
catch(Exception kl)
{
   kl.toString();
   kl.printStackTrace();
   p_statement2.close();
}
于 2013-04-29T06:27:50.350 回答
0

试试这个

//assign values dynamically   
String col1 = "NAME";
    String col2 = "VENDOR";
    String col3 = "SIZE";    

//create sql
    sql="UPDATE TOYS SET "+col1+"=?,"+col2+"=?,"+col3+"=? WHERE NAME=? AND LICENSE=? AND BOX_ID=? ";
        p_statement2=connection.prepareStatement(sql);
        p_statement2.setString(1, "toy1");
        p_statement2.setString(2, "vendor1");
        p_statement2.setString(3, "size1");
        p_statement2.setString(4, "toyx");
        p_statement2.setString(5, "license1");
        p_statement2.setInt(6, 11);
        try
        {
        p_statement2.executeUpdate();
        }
        catch(Exception kl)
        {
        kl.toString();
        kl.printStackTrace();
        p_statement2.close();
        }
于 2013-04-29T07:08:24.390 回答
0

最后,它完成了。我首先通过 rsmd.getColumnName() 方法获取列名并根据需要附加“=”运算符和逗号运算符,从而在循环下创建 sql 字符串。然后将它传递给preparedstatement创建代码并完成它。感谢大家的支持。

于 2013-04-29T07:34:56.680 回答