Java examples for JDBC:SQL Statement
Matching with Wildcards in a SQL Statement
import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; public class Main { public static void main(String[] args) throws Exception { Connection connection = null; Statement stmt = connection.createStatement(); // Select if str_value contains the word pat String sql = "SELECT * FROM my_table WHERE str_value LIKE '%pat%'"; // Select the row if str_value ends with the word pat sql = "SELECT * FROM my_table WHERE str_value LIKE 'pat%'"; // Select the row if str_value starts with abc and ends with xyz sql = "SELECT * FROM my_table WHERE str_value LIKE 'abc%xyz'"; // Select the row if str_value equals the word pat% sql = "SELECT * FROM my_table WHERE str_value LIKE 'pat\\%'"; // Select the row if str_value has 3 characters and starts with p and ends with t sql = "SELECT * FROM my_table WHERE str_value LIKE 'p_t'"; // Select the row if str_value equals p_t sql = "SELECT * FROM my_table WHERE str_value LIKE 'p\\_t'"; // Execute the query ResultSet resultSet = stmt.executeQuery(sql); }/*from ww w.ja va 2s . c om*/ }