Java Resultset.getInt()没有正确返回?

时间:2012-02-21 11:54:28

标签: java mysql jdbc resultset

我正在制作一个程序,如果已经过了大量的资金,我必须提醒用户。

我已经制定了一种方法来检查用户是否已经提醒该阈值,因此我们不重复警报,因为我已经根据警报是否已发送设置了一个填充1和0的MySQL表已经或不是。

这是表格:

mysql> describe avisos;
+----------------+------------+------+-----+---------+-------+
| Field          | Type       | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+-------+
| CLI            | varchar(9) | NO   | PRI | NULL    |       |
| mes            | int(2)     | YES  |     | NULL    |       |
| AvisadoPrimero | int(11)    | NO   |     | NULL    |       |
| AvisadoSegundo | int(11)    | NO   |     | NULL    |       |
| AvisadoTercero | int(11)    | NO   |     | NULL    |       |
| AvisadoCuarto  | int(11)    | NO   |     | NULL    |       |
+----------------+------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

然而,在我的程序中,我进行查询以检查警报是否已经发送,无论如何,代码将始终返回0(因此声明警报未发送)。

在这里,您可以看到我正在寻找的字段的值:

mysql> SELECT AvisadoPrimero from avisos where CLI=#########; //(hidden for security reasons)
+----------------+
| AvisadoPrimero |
+----------------+
|              1 |
+----------------+
1 row in set (0.00 sec)

这是我的程序中运行的代码:

         try {
        Statement sentencia = conexion.createStatement();
        PreparedStatement avisosQuery = conexion.prepareStatement(avisosString);
        PreparedStatement subtotalesQuery = conexion.prepareStatement(subtotalesString);
        //ResultSet sobrepasados = sentencia.executeQuery("SELECT cli FROM subtotales where Precio >=" + umbralInferior + " and Precio <" + umbralSuperior + ";");
        subtotalesQuery.setInt(1, umbralInferior);
        subtotalesQuery.setInt(2, umbralSuperior);
        sobrepasados = subtotalesQuery.executeQuery();
        while (sobrepasados.next()) {
            clis.add(sobrepasados.getString("CLI"));
        }
        if (!clis.isEmpty()) {
            for (int i = 0; i < clis.size(); i++) {

                switch (umbralInferior) {
                    case UMBRAL1:
                        avisosQuery.setString(1, "AvisadoPrimero");
                        avisosQuery.setString(2,clis.get(i));
                        resultado = avisosQuery.executeQuery();

                        resultado.first();

                        //System.out.println("Entrando en prueba\n\nConsulta realizada:");
                        //System.out.println(resultado.getInt("CLI"));
                        System.out.println(resultado.getInt(1));

因此,正如您所看到的,将字段设置为1,最后一次打印将始终打印为0.

任何提示?我做错了什么?

==============

最后,通过使用PreparedStatement并修复列名,得出结论。

(我也相信MySQL表中的某些触发器总是错误地初始化为0,但这是未经证实的)

这是最终的代码:

try {
        Statement sentencia = conexion.createStatement();
        PreparedStatement avisosQuery = conexion.prepareStatement(avisosString);
        PreparedStatement subtotalesQuery = conexion.prepareStatement(subtotalesString);
        //ResultSet sobrepasados = sentencia.executeQuery("SELECT cli FROM subtotales where Precio >=" + umbralInferior + " and Precio <" + umbralSuperior + ";");
        subtotalesQuery.setInt(1, umbralInferior);
        subtotalesQuery.setInt(2, umbralSuperior);
        sobrepasados = subtotalesQuery.executeQuery();
        while (sobrepasados.next()) {
            clis.add(sobrepasados.getString("CLI"));
        }
        if (!clis.isEmpty()) {
            for (int i = 0; i < clis.size(); i++) {

                switch (umbralInferior) {
                    case UMBRAL1:
                        //avisosQuery.setString(1, "AvisadoPrimero");
                        avisosQuery.setString(1,clis.get(i));
                        resultado = avisosQuery.executeQuery();

                        resultado.first();

                        //System.out.println("Entrando en prueba\n\nConsulta realizada:");
                        //System.out.println(resultado.getInt("CLI"));
                        System.out.println(resultado.getBoolean(1));

谢谢大家!!

1 个答案:

答案 0 :(得分:1)

MySQL预处理语句不允许参数列或表名。现在,代码选择字符串"AvisadoPrimero"而不是列的值。也就是说,当你写:

PreparedStatement avisosQuery = conexion.prepareStatement(
        "SELECT ? FROM avisos WHERE CLI=?");
avisosQuery.setString(1, "AvisadoPrimero");
avisosQuery.setString(2,clis.get(i));

数据库收到:

SELECT "AvisadoPrimero" FROM avisos WHERE CLI=something

然后调用getInt失败,因为文本"AvisadoPrimero"无法转换为数字。