0

I am developing simple swing application to get some information from the database. Two of the field in database table called Used_PR & Create_PR. As follows,enter image description here

Data structure is like mention below.

  PR-L-Machine-Date-Shift

Then I want to retrieve data by PR. Here is my interface to insert search detailsenter image description here

Date choose from date JDateChooser. I used LIKE clause to get data from the db, but it does not work. Here is the MYSQL query statement.

        String prlQuery = "SELECT * FROM final_report WHERE used_PR LIKE '?%' OR create_PR LIKE '?%'";

        try {
            Date chooseDate = new Date(dateChooser.getDate().getTime());
            String parameter = "PR-L-" + selectMachine.getSelectedItem() + "-" + chooseDate;

            pst = connect.dbConnection().prepareStatement(prlQuery);
            pst.setString(1, parameter);
            pst.setString(2, parameter);

But seems it is not working. Can somebody help me with this code structure.

MD40
  • 157
  • 2
  • 13
  • try to change the query to "SELECT * FROM final_report WHERE used_PR LIKE ? OR create_PR LIKE ?", and add the % at the end of the parameter string builder – Shaiba7 Nov 23 '17 at 10:25
  • You probably need to check the format of the date used stored in the database and format which you are currently getting from the JDateChooser. – Jigar Shah Nov 23 '17 at 10:25
  • I tried & it works. Thanks – MD40 Nov 23 '17 at 11:28

1 Answers1

1

You must pass the % in the parameters your query must look like

 String prlQuery = "SELECT * FROM final_report WHERE used_PR LIKE ? OR create_PR LIKE ?";

    try {
        Date chooseDate = new Date(dateChooser.getDate().getTime());
        String parameter = "PR-L-" + selectMachine.getSelectedItem() + "-" + chooseDate;

        pst = connect.dbConnection().prepareStatement(prlQuery);
        pst.setString(1, parameter+"%");
        pst.setString(2, parameter+"%");
vichu
  • 353
  • 2
  • 10