Search code examples
javamysqlswingjdatechooser

Insert data into mysql database with LIKE Clause


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.


Solution

  • 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+"%");