0

I'm having a hard time trying to filter data by date

MySQL Query

public DefaultTableModel getData1(Date start,Date end){
.......
 String sql = "SELECT * FROM torch.new_table WHERE pro_date between '"+start+"' and '"+end+"'";
.....
}

Code for get date from a JCalendar

DefaultTableModel dm = new Products().getData1(sDate.getDate(), eDate.getDate());

Complete code is Here: http://postimg.org/image/4zycbtj4n/

RubioRic
  • 2,442
  • 4
  • 28
  • 35

2 Answers2

1

Use the wildcard '?' when you are defining the query.

Replace each wildcard using setDate and a new java.sql.Date.

...

String sql = "SELECT * FROM torch.new_table WHERE pro_date between ? and ?";

PreparedStatement ps = con.prepareStatement(sql);

ps.setDate(1, new java.sql.Date(start.getTime());
ps.setDate(2, new java.sql.Date(end.getTime());

ResultSet rs = ps.executeQuery();
...

If you obtain a String from a JDateChooser, you can use

ps.setDate(1, java.sql.Date.valueOf(start));
ps.setDate(2, java.sql.Date.valueOf(end));

More possibilities here Using setDate in PreparedStatement

Community
  • 1
  • 1
RubioRic
  • 2,442
  • 4
  • 28
  • 35
0

Part of code to filter data by date (Obtained from a jCalender) using MySQL

Thanks for help @RubioRic

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

//dateT and dateE type jDatechooser

    String get = sdf.format(dateT.getDate());

    String get1 = sdf.format(dateE.getDate());

// Query to select get data from database

    String sql = "SELECT * FROM date WHERE DATE(Date) >=   ?  && DATE(Date)  <= ?";

    try{

        Connection conn = dataB.mySqlCon();

        PreparedStatement st = conn.prepareStatement(sql);

       st.setString(1, get);
       st.setString(2, get1);

        ResultSet rs = st.executeQuery();