FileDocCategorySizeDatePackage
LikeEscapeSyntax.javaAPI DocExample1767Thu Jun 14 17:50:24 BST 2001None

LikeEscapeSyntax.java

import java.io.*;
import java.sql.*;
import java.text.*;

public class LikeEscapeSyntax {
  Connection conn;

  public LikeEscapeSyntax() {
    try {
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      conn = DriverManager.getConnection(
      "jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
      e.printStackTrace();
    }
  }

  public static void main(String[] args) 
   throws Exception, IOException {
    LikeEscapeSyntax iud = new LikeEscapeSyntax();

    iud.executeSelect(
     "select table_name " + 
     "from   all_tables " + 
     "where  table_name like '%\\_%' {escape '\\'}");
  }

  public void executeSelect(String sql) throws IOException, SQLException {
    java.util.Date inactive_date = null;
    int            rows          = 0;
    ResultSet      rslt          = null;
    Statement      stmt          = null;

    System.out.println(sql);
    try {
      stmt = conn.createStatement();
      rslt = stmt.executeQuery(sql);
      while (rslt.next()) {
       rows++;
       System.out.println(rslt.getString(1));
      }
      System.out.println(Integer.toString(rows) + " rows selected");
      System.out.println(" ");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
    finally {
      if (rslt != null)
        try { rslt.close(); } catch (SQLException ignore) { }
      if (stmt != null)
        try { stmt.close(); } catch (SQLException ignore) { }
    }
  }

  protected void finalize() 
   throws Throwable {
    if (conn != null) 
      try { conn.close(); } catch (SQLException ignore) { }
    super.finalize();
  }
}