【问题标题】:why we use setInt with select query instead of using getInt when value is already there in database为什么当数据库中已经存在值时我们使用 setInt 和 select 查询而不是使用 getInt
【发布时间】:2016-04-08 02:18:24
【问题描述】:

为什么我们在选择查询中使用 setInt 而不是在数据库中已经存在值时使用 getInt?

    try {
        conn = getConnection();


        ps = conn.prepareStatement("SELECT * FROM circle where id =?");

        ps.setInt(1, circleId);

        Circle circle = null;
        rs = ps.executeQuery();
        if (rs.next()) {
            //String s = rs.getString(circleId);

            circle = new Circle(circleId, rs.getString("name"));

        }

【问题讨论】:

  • 您为查询的参数赋值。所以你“设置”它。

标签: java jdbc


【解决方案1】:

您正在设置要在查询中使用的参数值。 SQL 中的? 代表参数,在这里你给它一个值。

当您稍后调用getString() 时,这是从查询的结果 中获取一个值,这与作为查询的一部分发送的参数非常不同。

参数化 SQL 允许将值安全地包含到查询中,而无需转义它们以防止SQL injection attacks,或担心数据类型转换。您应该阅读JDBC PreparedStatement tutorial 了解更多详情。

【讨论】:

    猜你喜欢
    • 2020-05-02
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2013-08-03
    • 1970-01-01
    • 2012-02-24
    相关资源
    最近更新 更多