我正在為大學做的期末項目需要幫助,我正在做一個圖書館類系統(tǒng)。我是 mySQL 的新手。我在代碼上遇到問題,“參數(shù)索引超出范圍(3 > 參數(shù)數(shù)量,即 2)”。PreparedStatement book_edit = book_db.prepareStatement("update books set ?='?' where id=?"); book_edit.setString(1, column); //where column is to set the column of the table book_edit.setString(2, input); //where input is what you want to change book_edit.setInt(3, book_id); //the primary key in my database int i = book_edit.executeUpdate(); if (i != 0) { System.out.println("UPDATED!!"); } else { System.out.println("Failed to Add Data!"); }
1 回答

瀟湘沐
TA貢獻1816條經(jīng)驗 獲得超6個贊
列名不能是 的變量PreparedStatement。它必須是 SQL 的一部分String:
PreparedStatement book_edit = book_db.prepareStatement("update books set " + column + "= ? where id = ?");
book_edit.setString(1, input); //where input is what you want to change
book_edit.setInt(2, book_id); //the primary key in my database
添加回答
舉報
0/150
提交
取消