java - DAO 类中的 SQL 查询

标签 java sql oracle

我将日期数据类型转换为字符串并作为字符串存储在表中。我想编写一个查询来显示特定日期之间的结果。我这样写

 public Statement getstatement(Statement s) throws SQLException {
        long accid=0;
        Statement s1=null;
        Connection conn=DatabaseUtil.getConnection();
        PreparedStatement psmt=conn.prepareStatement("select * from Account_groupc_tja05 where Account_id =?");
        psmt.setLong(1,s.getAccid());
        System.out.println("inside dao");
        ResultSet rs=psmt.executeQuery();
        while(rs.next()){
             accid=rs.getLong(1);
             System.out.println("inside while");
        }
        if(s.getAccid()==accid){
            System.out.println("inside if");
            PreparedStatement psmt1=conn.prepareStatement("select * from deposit1_groupc_tja05 where Account_id =? and Transaction_Date between ? and ?");
            psmt1.setLong(1, accid);
            psmt1.setString(2, s.getDatefrom());
            psmt1.setString(3, s.getDateto());
            System.out.println(s.getDateto());
            ResultSet rs1=psmt1.executeQuery();
            while(rs1.next()){
                System.out.println("inside inner while");
                 s1=new Statement(rs.getString(6),rs.getLong(7),rs.getLong(4),rs.getLong(3),rs.getString(5));
                 System.out.println(s1);
            }
        }

        return s1;
    }

但是这个查询没有执行。为什么?

最佳答案

您的语法错误,没有像准备好的语句这样的东西,并且您想要执行 ps来“执行查询” 。例如

PreparedStatement ps=con.prepareStatement("Select * from deposit where transaction_date between ? And ?");
ps.setString(1,fromdate);
ps.setString(2,todate);
ps.execute();
ps.close();

您还需要在连接语句中输入数据库凭据

Connection conn=DatabaseUtil.getConnection();

Connection conn=DatabaseUtil.getConnection(<databaseurl>, <username>, <password>);

关于java - DAO 类中的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45909236/

相关文章:

java - 如何创建只接受实现 Iterable 的元素的方法

java - Eclipse 的 LogCat 被 Android 的 WAIT_FOR_CONCURRENT_GC 淹没

sql - 用于将查询中的表保存为 csv 的 Flask 按钮

sql - 引用复合主键的一列作为外键

java - Ode 服务器停止时出错(以及 BPEL 部署错误)

java - 如何修复以下正则表达式?

sql - F# 查询异常, "unrecognized method call value"

sql - 使用 SSRS 报告将 csv 列表传入 SQL 数据集的参数值

oracle - ODP.NET + 端口 6200 上的意外流量(Oracle 通知服务)

oracle - 如何使用 jmeter 测试具有 sys_refcursor 返回类型的 Oracle 存储过程?