薰 风 2023-09-01 20:59 采纳率: 63.3%
浏览 3
已结题

mysql数据库在java中查询异常

IDEA使用sql语句查询异常
#这个sql 语句 是获取表的模糊查询 数据总数,在mysql数据库中查出来是5
/ select count(*) from t_student s1,t_clazz c1,t_group t1,t_city c2,t_mark t2 where s1.s_clazz_id=c1.id and t1.g_clazz_id=c1.id and c2.id=s1.s_city_id and t2.m_id=s1.s_mark_id and s1.name like concat('张','%') /
================================================================================

/***

 /***
     * 模糊查询总数
     * @param field    要查询的字段名
     * @param obscure  模糊字段
     * @return
     */
    @Override
    public int count(String field, String obscure) {
        String sql = "select count(*) from t_student s1,t_clazz c1,t_group t1,t_city c2,t_mark t2 where s1.s_clazz_id=c1.id and t1.g_clazz_id=c1.id and c2.id=s1.s_city_id and t2.m_id=s1.s_mark_id and ? like concat(?,'%')";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, field);
            ps.setString(2, obscure);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getInt("count(*)");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

//测试类

public class Text {
public static void main(String[] args) {
StudentDaoImpl studentDao = new StudentDaoImpl();
int count = studentDao.count("s1.name", "张");
**//_测试类里面测出来是怎么0啊**
System.out.println(count);
}
}

问题:为什么在数据库中查到的正确的5,在测试类里面查到的确是0?

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-09-01 22:35
    关注

    【以下回答由 GPT 生成】

    我怀疑问题出在SQL语句中使用了占位符代替字段名的部分,导致查询条件无法正确匹配。我会尝试修改代码中的SQL语句,同时加入相关的调试输出语句来验证和定位问题。

    以下是修改后的代码和SQL语句:

    /**
     * 模糊查询总数
     * @param field 要查询的字段名
     * @param obscure 模糊字段
     * @return
     */
    @Override
    public int count(String field, String obscure) {
        String sql = "select count(*) from t_student s1, t_clazz c1, t_group t1, t_city c2, t_mark t2 where s1.s_clazz_id=c1.id and t1.g_clazz_id=c1.id and c2.id=s1.s_city_id and t2.m_id=s1.s_mark_id and " + field + " like concat(?,'%')";
        try {
            System.out.println("生成的SQL语句:" + sql); // 添加调试输出
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, obscure);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getInt("count(*)");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }
    
    // 测试类
    public class Test {
        public static void main(String[] args) {
            StudentDaoImpl studentDao = new StudentDaoImpl();
            int count = studentDao.count("s1.name", "张");
            System.out.println(count);
        }
    }
    

    您可以尝试运行这段代码,然后查看生成的SQL语句是否正确,以及是否得到了正确的结果。



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月9日
  • 已采纳回答 9月1日
  • 创建了问题 9月1日

悬赏问题

  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 有关类的报错,在模块里调用另一个模块的方法下的变量时出错
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格
  • ¥15 kali linux用wget archive.kali.org/archive-key.asc指令下载签名无效(失败)
  • ¥15 openHarmony 利用c++程序在dayu210开发板上实现拉取RTSP视频流并且在屏幕上显示
  • ¥15 GD32H757的can通信配置
  • ¥20 nist随机数测试的问题