Friday, January 9, 2015

Use Column Methods and Java Words Program



Use Column Methods

import java.sql.*;
public class jdbcConn 
{
   public static void main(String[] args) throws Exception
  {
      Class.forName("org.apache.derby.jdbc.ClientDriver");
      Connection con = DriverManager.getConnection
      ("jdbc:derby://localhost:1527/testDb","name","pass");
      Statement stmt = con.createStatement();
      String query = "select * from emp order by name";
      ResultSet rs = stmt.executeQuery(query);
      ResultSetMetaData rsmd = rs.getMetaData();
      System.out.println("no of columns in the table= "+
      rsmd.getColumnCount());
      System.out.println("Name of the first column "+ 
      rsmd.getColumnName(1));
      System.out.println("Type of the second column "+
      rsmd.getColumnTypeName(2));
      System.out.println("No of characters in 3rd column "+ 
      rsmd.getColumnDisplaySize(2));
   }
}

Splitting a String

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Resetting 
{
   public static void main(String[] args) throws Exception
   {
      Matcher m = Pattern.compile("[frb][aiu][gx]").
      matcher("fix the rug with bags");
      while (m.find())
         System.out.println(m.group());
      m.reset("fix the rig with rags");
      while (m.find())
         System.out.println(m.group());
   }
} 

Searching Duplicate Words

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main 
{
   public static void main(String args[]) throws Exception 
  {
      String duplicatePattern = "\\b(\\w+) \\1\\b";
      Pattern p = Pattern.compile(duplicatePattern);
      int matches = 0;
      String phrase = " this is a test ";
      Matcher m = p.matcher(phrase);
      String val = null;
      while (m.find()) 
       {
         val = ":" + m.group() + ":";
         matches++;
      }
      if(val>0)
         System.out.println("The string 
         has matched with the pattern.");
      else
      System.out.println("The string 
      has not matched with the pattern.");
   }
}

Finding Word Occurrence

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main 
{
   public static void main(String args[]) throws Exception 
   {
      String candidate = "this is a test, A test.";
      String regex = "\\ba\\w*\\b";
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(candidate);
      String val = null; 
      System.out.println("Input: " + candidate);
      System.out.println("Regex: " + regex + "\r\n");
      while (m.find()) 
      {
         val = m.group();
         System.out.println("Match: " + val);
      }
      if (val == null) 
      {
         System.out.println("No matches: ");
      }
   }
}

Last Index of a Word

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main 
{
   public static void main(String args[]) 
  {
      String candidateString = "This is a Java example.
      This is another Java example.";
      Pattern p = Pattern.compile("Java");
      Matcher matcher = p.matcher(candidateString);
      matcher.find();
      int nextIndex = matcher.end();
      System.out.print("The last index of Java is:");
      System.out.println(nextIndex);
   }
}

No comments:

Post a Comment