我正在編寫一個網頁,它從表單接收輸入,通過cgi將其發送到java文件,通過sql將輸入插入到數據庫中,然後打印出數據庫。我在使用變量插入數據庫時遇到了困難,而且我想知道是否有人能夠幫助我。用Java在SQL中插入變量
String a1Insert = (String)form.get("a1");
String a2Insert = (String)form.get("a2");
這是我得到我的變量構成的形式(只相信它的作品,還有一羣更後端,但我以前使用過這一點,我知道這是正確獲取變量)。
String dbURL = "jdbc:derby://blah.blahblah.ca:CSE2014;user=blah;password=blarg";
Connection conn = DriverManager.getConnection(dbURL);
Statement stmt = conn.createStatement();
stmt.executeUpdate("set schema course");
stmt.executeUpdate("INSERT INTO MEMBER VALUES (a1Insert, a2Insert)");
stmt.close();
這是我試圖插入數據庫的地方。它給我的錯誤:
Column 'A1INSERT' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'A1INSERT' is not a column in the target table.
如果任何人有這將是可愛的任何想法^^感謝
在http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html和http://www.mkyong.com/jdbc/jdbc-preparestatement-example上可以找到很好的信息。 -insert-a-record/ –
如果你的語法正確 - stmt.executeUpdate(「INSERT INTO MEMBER VALUES('」+ a1Insert +「','」+ a2Insert +「')」); - 那麼它將在技術上起作用,但是如果有人可以操縱a1Insert/s2Insert並將它們設置爲惡意值,則會讓您打開SQL注入攻擊。相反,你應該遵循Nathan提到的PreparedStatement的方法。 –
@NeilCoffey,只是正確的SQL指定了插入語句中的列名。因此,需要將...加入成員(colname1,colname2)值(value1,value2) – jwenting