我只是想知道在java中插入值的最佳方式是什麼,比如java程序員在插入值時會做什麼,並告訴我爲什麼?在數據庫中插入條目的最佳方法
這是這樣嗎?
String query = "insert into table (table1) values (1)"; PreparedStatement stmt = connection.preparedStatement(query); stmt.execute();
或這樣嗎?
String query = "insert into table (table1) values (?)"; PreparedStatement stmt = connection.preparedStatement(query); stmt.setInt(1,"1"); stmt.execute();
還是有更好的辦法?
只是要求,以提高我的編碼知識:d
總是使用第二個選項。如果您使用第一個選項並通過硬編碼字符串和用戶輸入構建語句,則您將開放SQL注入。每當有可變數據時應使用第二種方法。 –