使用Java更新SQL表

时间:2014-04-25 19:23:12

标签: java sql

我正在开发一个可以更新我的数据库的应用程序......但是,我无法使我的Java方法正常工作。它给了我以下错误:Must declare the scalar variable "@P0WHERE".有任何建议吗?

我正在使用的方法:

public void updateTable() throws SQLException
        {
            Scanner input = new Scanner(System.in);
            System.out.println("Update the following:\n" + this);
            this.getReservationInfo(input);

            DataConnection connection = new DataConnection();

            String query = "UPDATE " + TableName;
            query += "Set Name = ?";
            query += "WHERE Person_ID = " + id;

            connection.updateData(query, person_name);
            connection.closeConnection();
        }

2 个答案:

答案 0 :(得分:4)

在'SET'和'WHERE'之前添加空格,否则它将无效。

String query = "UPDATE " + TableName;
        query += " SET Name = ?";
        query += " , Age = ?";
        query += " , Col1 = ?"; //And other cols 
        query += " WHERE Person_ID = " + id;

编辑:更改了查询以更新多个列。

答案 1 :(得分:2)

我是这么认为的。你似乎缺少空间。在TableName之后和?之后。

String query = "UPDATE " + TableName;
query += " Set Name = ?"; // tableSet not good, and
                           // ?WHERE is not valid add spaces.
query += " WHERE Person_ID = " + id;