Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. package bmsearch;
  6.  
  7. import java.io.*;
  8. import java.sql.*;
  9.  
  10.  
  11. import javax.naming.InitialContext;
  12.  
  13. public class BoyerMoore {
  14.  
  15.  
  16. public static int bmMatch(String text,String pattern)
  17. {
  18. int last[] = buildLast(pattern);
  19. int n = text.length();
  20. int m = pattern.length();
  21. int i = m-1;
  22. if (i > n-1)
  23. return -1; // no match if pattern is
  24. // longer than text
  25. int j = m-1;
  26. do {
  27. if (pattern.charAt(j) == text.charAt(i))
  28. if (j == 0)
  29. return i; // match
  30. else { // looking-glass technique
  31. i--;
  32. j--;
  33. }
  34.  
  35. else { // character jump technique
  36. int lo = last[text.charAt(i)]; //last occ
  37. i = i + m - Math.min(j, 1+lo);
  38. j = m - 1;
  39. }
  40. } while (i <= n-1);
  41.  
  42. return -1; // no match
  43. } // end of bmMatch()
  44.  
  45.  
  46. public static int[] buildLast(String pattern)
  47. /* Return array storing index of last
  48. occurrence of each ASCII char in pattern. */
  49. {
  50. int last[] = new int[128]; // ASCII char set
  51.  
  52. for(int i=0; i < 128; i++)
  53. last[i] = -1; // initialize array
  54. for (int i = 0; i < pattern.length(); i++)
  55. last[pattern.charAt(i)] = i;
  56. return last;
  57. } // end of buildLast()
  58.  
  59.  
  60.  
  61. /*
  62. private Connection getConnection() {
  63. try {
  64. InitialContext initial= new InitialContext();
  65. Datasource ds = (DataSource)
  66. initial.lookup("java:comp/env/jdbc/fcbkrel_4");
  67. return ds.getConnection();
  68. }
  69. catch (Exception e){
  70. e.printStackTrace();
  71. throw new EJBException(e.toString());
  72. }
  73. }
  74. */
  75.  
  76. public static void main(String args[]) throws ClassNotFoundException, SQLException
  77. {
  78. String text= "catdogtree";
  79. String pattern= "that";
  80.  
  81. Connection connection = null;
  82. try {
  83. // Load the JDBC driver
  84. String driverName = "oracle.jdbc.driver.OracleDriver";
  85. Class.forName(driverName);
  86.  
  87. // Create a connection to the database
  88. String serverName = "localhost";
  89. String portNumber = "1521";
  90. String sid = "orcl"; //fcbkrel_4
  91. String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
  92. String username = "fcbkrel_4";
  93. String password = "fc";
  94. connection = DriverManager.getConnection(url, username, password);
  95. } catch (ClassNotFoundException e) {
  96. // Could not find the database driver
  97. } catch (SQLException e) {
  98. // Could not connect to the database
  99. }
  100.  
  101. Statement s=connection.createStatement();
  102. ResultSet rset = s.executeQuery("select * from comments");
  103. while (rset.next()){
  104.  
  105. int posn = bmMatch(rset.getString(4), pattern);
  106.  
  107. if (posn == -1)
  108. {
  109. System.out.println("Pattern not found: "+rset.getString(4));
  110. }
  111. else{
  112. System.out.print(rset.getString(1)+"--");
  113. System.out.print(rset.getString(4)+"--");
  114. System.out.println(" POST: " + posn);
  115. }
  116.  
  117. }
  118. //System.out.println(rset.getString(1));
  119. s.close();
  120. rset.close();
  121. connection.close();
  122. }
  123.  
  124. /*
  125. if (args.length != 2) {
  126. System.out.println("Usage: java BmSearch <text> <pattern>");
  127. System.exit(0);
  128. }
  129.  
  130. System.out.println("Text: " + args[0]);
  131. System.out.println("Pattern: " + args[1]);
  132. */
  133. /*
  134. int posn = bmMatch(text, pattern);
  135.  
  136. if (posn == -1)
  137. System.out.println("Pattern not found");
  138. else
  139. System.out.println("Pattern starts at posn " + posn);
  140. }
  141.  
  142.  
  143. */
  144.  
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement