Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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
Mysql DB从表中选择字段语句java_Java_Jdbc_Spring Jdbc - Fatal编程技术网

Mysql DB从表中选择字段语句java

Mysql DB从表中选择字段语句java,java,jdbc,spring-jdbc,Java,Jdbc,Spring Jdbc,我正在尝试使用java查询数据库,我不理解这个函数的问题。它返回一个垃圾值 我只想从MySQL数据库中检索与名字匹配的值 public List<Customer> select(String cusDB) { return jdbcTemplate.query( "SELECT id, first_name, last_name FROM customers WHERE first_name= cusDB", (rs, row

我正在尝试使用java查询数据库,我不理解这个函数的问题。它返回一个垃圾值

我只想从MySQL数据库中检索与名字匹配的值

public List<Customer> select(String cusDB) {
    return jdbcTemplate.query(
            "SELECT id, first_name, last_name FROM customers WHERE first_name= cusDB",
            (rs, rowNum) -> new Customer(rs.getLong("id"),
                    rs.getString("first_name"), rs.getString("last_name")));
}

您可以使用两种方法,第一种是将您的查询与您要搜索的第一个\u名称关联起来:

"SELECT id, first_name, last_name FROM customers WHERE first_name= '" + cusDB + "'"
第二种用法是这样使用prepapred语句:

"SELECT id, first_name, last_name FROM customers WHERE first_name= ?"
st.setString(1, cusDB);
但是我没有看到任何关于Prepapred声明的迹象,所以你可以在这里学习

编辑

正如@AndréSchild在评论中所说:


您很容易受到SQL注入的攻击,例如名字中带有“;从客户中删除//将从数据库中删除所有客户。Alwaystm使用预先准备好的语句

您可以使用两种方法,第一种方法是将您的查询与您要搜索的名字联系起来:

"SELECT id, first_name, last_name FROM customers WHERE first_name= '" + cusDB + "'"
第二种用法是这样使用prepapred语句:

"SELECT id, first_name, last_name FROM customers WHERE first_name= ?"
st.setString(1, cusDB);
但是我没有看到任何关于Prepapred声明的迹象,所以你可以在这里学习

编辑

正如@AndréSchild在评论中所说:


您很容易受到SQL注入的攻击,例如名字中带有“;从客户中删除//将从数据库中删除所有客户。Alwaystm使用准备好的语句

查询字符串中不能只有Java参数的名称。您需要显式地向查询提供参数。为此,请将代码更改为:

public List<Customer> select(String cusDB) {
    return jdbcTemplate.query(
            "SELECT id, first_name, last_name FROM customers WHERE first_name= ?",
            new Object[] { cusDB },
            (rs, rowNum) -> new Customer(rs.getLong("id"),
                    rs.getString("first_name"), rs.getString("last_name")));
}

也就是说,是否要引入一个参数占位符?在查询字符串中,将参数值数组添加到方法中,并为查询中的每个参数占位符调用一个值。另请参阅的和文档。

查询字符串中不能只包含Java参数的名称。您需要显式地向查询提供参数。为此,请将代码更改为:

public List<Customer> select(String cusDB) {
    return jdbcTemplate.query(
            "SELECT id, first_name, last_name FROM customers WHERE first_name= ?",
            new Object[] { cusDB },
            (rs, rowNum) -> new Customer(rs.getLong("id"),
                    rs.getString("first_name"), rs.getString("last_name")));
}

也就是说,是否要引入一个参数占位符?在查询字符串中,将参数值数组添加到方法中,并为查询中的每个参数占位符调用一个值。另请参见的和文档。

不要使用第一个版本,您很容易受到SQL注入的攻击,例如带有“”;从客户中删除//将从数据库中删除所有客户。Alwaystm使用prepared statementsys@AndréSchild为此,我建议使用preparedstatement,我想他错过了一些关于字符串连接的东西,因为我发布了第一个,非常感谢。不要使用第一个版本,你很容易受到SQL注入的影响,例如,名字中带有“;从客户中删除//将从数据库中删除所有客户。Alwaystm使用prepared statementsys@AndréSchild我建议使用prepared statement,我想他错过了一些关于字符串连接的东西,因为我发布了第一个,谢谢,如果这会返回垃圾值,我会非常惊讶,我更希望看到未知列cusDB或类似的异常。如果这会返回垃圾值,我会非常惊讶,我更希望看到未知列cusDB或类似的异常。