Java DatabaseMetaData getSearchStringEscape() method with example


The getSearchStringEscape() method of the DatabaseMetaData interface is used to get the that is used to escape the wildcard characters ('_' or, '%') by the underlying database.

This method returns a string parameter representing the value that is used to escape wildcard characters

To get the DatabaseMetaData object −

  • Make sure your database is up and running.
  • Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.
  • Get the connection object using the getConnection() method of the DriverManager class. Pass the URL the database and, user name, password of a user in the database, as String variables.
  • Get the DatabaseMetaData object with respect to the current connection using the getMetaData() method of the Connection interface.

Finally, get the search String to escape wild characters, by invoking the getSearchStringEscape() method of the DatabaseMetaData class.

Following JDBC program establishes connection with MySQL database, retrieves the search escape string.

Example

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseMetadata_getSearchStringEscape {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String url = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(url, "root", "password");
      System.out.println("Connection established......");
      //Retrieving the meta data object
      DatabaseMetaData metaData = con.getMetaData();
      //Retrieving the columns in the database
      String escapeString = metaData.getSearchStringEscape();
      //Printing the column name and size
      System.out.println("Search string that that is used to escape the wildcard characters: "+escapeString);
   }
}

Output

Connection established......
Search string that that is used to escape the wildcard characters: \
raja
Published on 15-May-2019 16:01:28
Advertisements