关于DAO类的SQL查询

时间:2017-08-27 20:50:54

标签: 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;
    }

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

1 个答案:

答案 0 :(得分:1)

您的语法错误,没有类似Prepared statement的内容,您希望execute ps“执行查询”。 e.g。

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>);