使用PreparedStatement模板问题

发布于 2024-10-17 23:44:25 字数 1944 浏览 2 评论 0原文

这是我的静态实用程序:

 //String sqlQuery = "select count(name) as num from tbname where name = ?"
    //String name = "testString";

    private static int correct(Connection connection, String sqlQuery, String name) {
        int result = 0;
        PreparedStatement statatement = null;
        ResultSet rs = null;
        try {
            statatement = connection.prepareStatement(sqlQuery);
            statatement.setString(1, name);
            rs = statatement.executeQuery();
            while (rs.next()) {
                result = rs.getInt("num");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                statatement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e.getMessage());
            }
            return result;
        }
    }

上面的方法返回 0 (不正确的结果),但下面的方法返回 '1' (它工作正常,它是相同的 sql 查询):

//String sqlQuery = "select count(name) as num from tbname where name = 'testString'"

    private static int correct(Connection connection, String sqlQuery, String name) {
        int result = 0;
        PreparedStatement statatement = null;
        ResultSet rs = null;
        try {
            statatement = connection.prepareStatement(sqlQuery);
            rs = statatement.executeQuery();
            while (rs.next()) {
                result = rs.getInt("num");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                statatement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e.getMessage());
            }
            return result;
        }
    }

您能给我任何建议吗,这样我就可以解决问题。

PS:我不确定这是否重要,但实际的 streetName - 在 windows-1251 编码(俄语)文本中有一个名称。 PPS:数据库是Oracle 10。

Here's my static utility:

 //String sqlQuery = "select count(name) as num from tbname where name = ?"
    //String name = "testString";

    private static int correct(Connection connection, String sqlQuery, String name) {
        int result = 0;
        PreparedStatement statatement = null;
        ResultSet rs = null;
        try {
            statatement = connection.prepareStatement(sqlQuery);
            statatement.setString(1, name);
            rs = statatement.executeQuery();
            while (rs.next()) {
                result = rs.getInt("num");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                statatement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e.getMessage());
            }
            return result;
        }
    }

The method above returns 0 (incorrect result), but the following one returns '1' (it works OK, it the same sql query):

//String sqlQuery = "select count(name) as num from tbname where name = 'testString'"

    private static int correct(Connection connection, String sqlQuery, String name) {
        int result = 0;
        PreparedStatement statatement = null;
        ResultSet rs = null;
        try {
            statatement = connection.prepareStatement(sqlQuery);
            rs = statatement.executeQuery();
            while (rs.next()) {
                result = rs.getInt("num");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                statatement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e.getMessage());
            }
            return result;
        }
    }

Could you please give me any advise, so I could resolve the problem.

PS: I'm not sure if it does matter, but the actual streetName - has a name in windows-1251 encoding (Russian) text.
PPS: The database is Oracle 10.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角卖回忆 2024-10-24 23:44:25

可能是字符集问题。根据 Oracle JDBC 驱动程序版本 10.1.0.2.0 ( 10g) 自述文件

以下是已知的列表
问题/限制:

  • 如果数据库字符集是 AL32UTF8,您可能会在下面看到错误
    以下情况:

    • 访问 LONG 和 VARCHAR2 数据类型。
    • 使用 setString() 和 setCharacterStream() 绑定数据。

因此,如果您的数据库字符集是 AL32UTF8,您可能需要将其更改为其他字符集。

另外,您的列的数据类型是什么? VARCHAR2?

It might be a character set issue. According to the Oracle JDBC Drivers release 10.1.0.2.0 (10g) README:

The following is a list of known
problems/limitations:

  • If the database character set is AL32UTF8, you may see errors under the
    following circumstances:

    • accessing LONG and VARCHAR2 datatypes.
    • binding data with setString() and setCharacterStream().

So if your database character set is AL32UTF8, you might have to get it changed to something else.

Also, what is the datatype of your column? VARCHAR2?

梦境 2024-10-24 23:44:25

看来编码与您在数据库编码字符集中设置的编码和您传递的字符串发生冲突。

您可以执行这 2 次尝试

  1. 将数据库编码设置为 UTF-8,然后尝试一下。如果这不起作用,您可以使用以下第二个选项

  2. 将 DB 编码字符集设置为 UTF-8,并使用此 String 构造函数设置字符串 String(byte[] bytes, String charsetName)

It seems the encoding conflict with the one which you set in your DB encoding charset and the the String you are passing..

You can do these 2 tries

  1. Set the DB encoding to UTF-8 and then give a try. If this does not work you may go with following 2nd option

  2. Set DB encoding charset to UTF-8 and also set the String by using this String constructor String(byte[] bytes, String charsetName)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文