Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将变量设置到JDBC语句中?_Java_Mysql_Jdbc - Fatal编程技术网

Java 如何将变量设置到JDBC语句中?

Java 如何将变量设置到JDBC语句中?,java,mysql,jdbc,Java,Mysql,Jdbc,当我尝试插入变量时,它会起作用,例如: String insertStr="INSERT INTO table1(username1,password1) VALUES(\"john\",\"password\")"; 但无法使用变量插入 String a=username.getText(); String b=password.getText(); try { Class.forName("com.mysql.jdbc.Driver"); Connection co

当我尝试插入变量时,它会起作用,例如:

String insertStr="INSERT INTO  table1(username1,password1) VALUES(\"john\",\"password\")";
但无法使用变量插入

String a=username.getText();
String b=password.getText();

try {
    Class.forName("com.mysql.jdbc.Driver");  
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/java_db1","root","");  

    Statement stmt=con.createStatement();        
    String insertStr="INSERT INTO  table1(username1,password1) VALUES(a,b);";
    stmt.executeUpdate(insertStr);
} catch (Exception e) { }
因为您将“a”和“b”作为字符串插入,而不是它们的变量值

String insertStr="INSERT INTO  table1(username1,password1) VALUES("+a+","+b+");"; 

应该这样做,但我建议在这里使用一个准备好的语句:

使用[
PreparedStatement
][1]而不是您的方式,因为您的方式可能是SQL注入或语法错误的受害者:

String insertStr = "INSERT INTO  table1(username1,password1) VALUES(?, ?)";
try (PreparedStatement pst = con.prepareStatement(insertStr)) {
    pst.setString(1, a);
    pst.setString(2, b);
    pst.executeUpdate();
}
出于安全考虑,我不建议使用
getText()
获取密码,而是使用
getPassword()
,因此您可以使用:

pst.setString(1, username.getText());
pst.setString(2, new String(passwordField.getPassword()));
看看这个:

  • [1] :

在sql中插入变量值的最常见方法是使用

使用此对象,您可以将变量值添加到SQL查询中,而无需担心SQL注入。 下面是一个PreparedStatement的示例:

//[Connection initialized before]
String insertStr="INSERT INTO  table1(username1,password1) VALUES(?,?);";
PreparedStatement myInsert = myConnectionVariable.prepareStatement(insertStr); // Your db will prepare this sql query
myInsert.setString(1, a); //depending on type you want to insert , you have to specify the position of your argument inserted (starting at 1)
myInsert.setString(2, b); // Here we set the 2nd '?' with your String b
myInsert.executeUpdate(); // It will returns the number of affected row (Works for UPDATE,INSERT,DELETE,etc...)
//You can use executeQuery() function of PreparedStatement for your SELECT queries
这比像这样使用字符串连接更安全:
值(“+a+”,“+b+”)


查看Java文档了解更多信息;)

String insertStr=“INSERT INTO table1(username1,password1)值(“+strA+”,“+strB+”);”;您不应该在SQL字符串中直接使用变量,因为这容易受到DB注入攻击。@TravisStevens这就是我建议使用准备语句的原因……第2行有一个小语法错误,冒号不应该出现在“资源试用”中。谢谢你的回答:)谢谢你的评论@joshpetit,你什么意思我不明白。这里有一个*分号:
try(PreparedStatement pst=con.prepareStatement(insertStr);)
在insertStr之后。这可能是一篇单独的文章,但是有没有办法在多个字段中使用相同的变量?例如,对准备好的语句中的两个问号使用相同的变量?@joshpetit是的,这是不必要的分号,您可以编辑我的问题,以便跟踪:)关于您的问题,是的,您可以使用
pst.setString(1,sameThing);pst.设置管柱(2,相同)