Search code examples
javaregexprepared-statementucanaccess

Using java for searching for data by using part of the row in microsoft access database


I have this table

enter image description here

And I am using the following code to retrieve data from my table that returns all the English words that its Kurdish word contains بةرز

targetText="بةرز";
try (PreparedStatement ps = conn.prepareStatement(
"SELECT English,Kurdish FROM Info " +
"WHERE Kurdish = ? " +
"OR REGEXP_MATCHES(Kurdish, ?) " +
"OR REGEXP_MATCHES(Kurdish, ?) " +
"OR REGEXP_MATCHES(Kurdish, ?) ")) {
   ps.setString(1, targetText);
   ps.setString(2, "^[. ]*" + targetText+ "[ ]*[:،,]+[ .]*$");
   ps.setString(3, "^[. ]*[:،,]+[ ]*" + targetText+ "[. ]*$");
   ps.setString(4, "^[. ]*[:،,]+[ ]*" + targetText+ "[ ]*[:،,]+[  .]*$");
   try (ResultSet rSet = ps.executeQuery()) {
      while (rSet.next()) {
         System.out.println(rSet.getString("English"));
         System.out.println(rSet.getString("Kurdish"));
      }
   }
}

So it works fine, it prints all the English words that I want.
My problem is that when I get the corresponded Kurdish word it doesn't print the complete cell. It just prints بةرز,

For example the output of the previous code should be:

aesthete
بةرز ، جوانىثةرست
aether
زوَر ناسك ، بةرز ، ثيروَز ، ئاسمانى
affair
بةرز 

But it prints

aesthete
بةرز 
aether
بةرز 
affair
بةرز 

What can I do to get the output that I want?

Note that I am using UCanAccess for my database connection,


Solution

  • Tanks for all, I have solved it just with a few change within the regx

    targetText="بةرز";
    try (PreparedStatement ps = conn.prepareStatement(
       "SELECT English,Kurdish FROM Info " +
       "WHERE Kurdish = ? " +
       "OR REGEXP_MATCHES(Kurdish, ?) " +
       "OR REGEXP_MATCHES(Kurdish, ?) " +
       "OR REGEXP_MATCHES(Kurdish, ?) ")) {
       ps.setString(1, targetText);
       ps.setString(2, "^" + targetText+ "[ ]*(،)[.]*");
       ps.setString(3, "[.]*(،)[ ]*" + targetText+ "$");
       ps.setString(4, "[.]*(،)[ ]*" + targetText+ "[ ]*(،)[.]*");
       try (ResultSet rSet = ps.executeQuery()) {
          while (rSet.next()) {
             System.out.println(rSet.getString("English"));
             System.out.println(rSet.getString("Kurdish"));
          }
       }
    }