Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bmsearch;
- import java.io.*;
- import java.sql.*;
- import javax.naming.InitialContext;
- public class BoyerMoore {
- public static int bmMatch(String text,String pattern)
- {
- int last[] = buildLast(pattern);
- int n = text.length();
- int m = pattern.length();
- int i = m-1;
- if (i > n-1)
- return -1; // no match if pattern is
- // longer than text
- int j = m-1;
- do {
- if (pattern.charAt(j) == text.charAt(i))
- if (j == 0)
- return i; // match
- else { // looking-glass technique
- i--;
- j--;
- }
- else { // character jump technique
- int lo = last[text.charAt(i)]; //last occ
- i = i + m - Math.min(j, 1+lo);
- j = m - 1;
- }
- } while (i <= n-1);
- return -1; // no match
- } // end of bmMatch()
- public static int[] buildLast(String pattern)
- /* Return array storing index of last
- occurrence of each ASCII char in pattern. */
- {
- int last[] = new int[128]; // ASCII char set
- for(int i=0; i < 128; i++)
- last[i] = -1; // initialize array
- for (int i = 0; i < pattern.length(); i++)
- last[pattern.charAt(i)] = i;
- return last;
- } // end of buildLast()
- /*
- private Connection getConnection() {
- try {
- InitialContext initial= new InitialContext();
- Datasource ds = (DataSource)
- initial.lookup("java:comp/env/jdbc/fcbkrel_4");
- return ds.getConnection();
- }
- catch (Exception e){
- e.printStackTrace();
- throw new EJBException(e.toString());
- }
- }
- */
- public static void main(String args[]) throws ClassNotFoundException, SQLException
- {
- String text= "catdogtree";
- String pattern= "that";
- Connection connection = null;
- try {
- // Load the JDBC driver
- String driverName = "oracle.jdbc.driver.OracleDriver";
- Class.forName(driverName);
- // Create a connection to the database
- String serverName = "localhost";
- String portNumber = "1521";
- String sid = "orcl"; //fcbkrel_4
- String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
- String username = "fcbkrel_4";
- String password = "fc";
- connection = DriverManager.getConnection(url, username, password);
- } catch (ClassNotFoundException e) {
- // Could not find the database driver
- } catch (SQLException e) {
- // Could not connect to the database
- }
- Statement s=connection.createStatement();
- ResultSet rset = s.executeQuery("select * from comments");
- while (rset.next()){
- int posn = bmMatch(rset.getString(4), pattern);
- if (posn == -1)
- {
- System.out.println("Pattern not found: "+rset.getString(4));
- }
- else{
- System.out.print(rset.getString(1)+"--");
- System.out.print(rset.getString(4)+"--");
- System.out.println(" POST: " + posn);
- }
- }
- //System.out.println(rset.getString(1));
- s.close();
- rset.close();
- connection.close();
- }
- /*
- if (args.length != 2) {
- System.out.println("Usage: java BmSearch <text> <pattern>");
- System.exit(0);
- }
- System.out.println("Text: " + args[0]);
- System.out.println("Pattern: " + args[1]);
- */
- /*
- int posn = bmMatch(text, pattern);
- if (posn == -1)
- System.out.println("Pattern not found");
- else
- System.out.println("Pattern starts at posn " + posn);
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement