Searching between dates in SQL with JDBC?

前端 未结 3 1922
悲哀的现实
悲哀的现实 2020-12-03 18:55

I\'m currently writing a Java Swing application that reads data in from a MYOB database file and displays certain information in a table. I\'ve been able to successfully gen

相关标签:
3条回答
  • 2020-12-03 19:18

    Use quotes around your dates:

    rs = stmt.executeQuery("SELECT sales.InvoiceNumber, sales.ShipToAddress, sales.Date "
                + "FROM sales, customers "
                + "WHERE sales.CardRecordID = customers.CardRecordID "
                + "AND customers.Name = 'Cash Sales' "
                + "AND sales.Date BETWEEN '" + sdate + "' AND '" + edate + "' "
                + "ORDER BY sales.ShipToAddress ASC, sales.Date DESC"
                + ";");
    

    Or it might be safer to use a prepared statement (if the dates come from untrusted inputs for example):

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    java.util.Date startDate = formatter.parse(sdate);
    java.util.Date endDate = formatter.parse(edate);
    PreparedStatement pstmt = connection.prepareStatement("SELECT sales.InvoiceNumber, sales.ShipToAddress, sales.Date "
                + "FROM sales, customers "
                + "WHERE sales.CardRecordID = customers.CardRecordID "
                + "AND customers.Name = 'Cash Sales' "
                + "AND sales.Date BETWEEN ? AND ? "
                + "ORDER BY sales.ShipToAddress ASC, sales.Date DESC");
    pstmt.setDate(1, new java.sql.Date(startDate.getTime()))
    pstmt.setDate(2, new java.sql.Date(endDate.getTime()))
    

    The between operator is inclusive, but if your database field is actually a timestamp, then a date with no time is assumed to be at time 00:00:00.000. So, in such a case, for your dates to be inclusive, you can add one day to your end date. Technically it will also include the first instant of the next day (00:00:00.000 hour), but depending on your application it may be enough.

    Otherwise you could use >= on "start date" and < on "end date plus one day":

    "sales.Date >= '" + sdate + "' AND sales.Date < '" + edatePlusOne + "' "
    
    0 讨论(0)
  • 2020-12-03 19:19
    Class.forName("com.mysql.jdbc.Driver");
    
    connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/hospitall","root","");
    
    PreparedStatement ps=connection.prepareStatement("select * from patient where patientid='"+txtpatientid.getText()+"'");
    
    Statement stm=connection.createStatement();
    
    ResultSet rs=ps.executeQuery();
    
    if(rs.next()){
    
    String patientid=txtpatientid.getText(); 
    
    String str="select * from patient where patientid='"+patientid+"'";
    
    ResultSet result=stm.executeQuery(str);
    
    0 讨论(0)
  • 2020-12-03 19:29

    It looks like you need to express dates in this format:

    select * from table where datefield between '2006-10-01' and '2006-11-30'

    Look here also, do not forget that google is your friend! :)

    0 讨论(0)
提交回复
热议问题