我正在编写一个网页,它从表单接收输入,通过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