Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Searching

843859Nov 20 2006 — edited Nov 21 2006
hi!, I'm just new to java swing and jdbc... I'm the pace of searching records... I was making a code which can search for a record by typing a certain letter.. when I type 'j' all the letters that begins with 'j' appears etc. and I made it right..

here's the problem, I wanted to make a search which is better than that.. if I'm looking for a record example name: "john" when I type 'j' of course all the records that starts with 'j' will appear and then 'o' and all the records that starts with 'j' and has a second character 'o' will appear.. and so on... until it found "john"...

can anybody correct my code or can give me any idea on how to make that.. hope to hear from all of you soon.. thanks
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import java.sql.*; 

public class MySearch extends JFrame 
{ 
  private Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); 
  private JPanel JPContainer = new JPanel(); 
  private JTextField capture = new JTextField( 100 ); 

  private String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
  private String DBSource = "jdbc:odbc:Search"; 

  private Connection connect; 
  private ResultSet rs; 
  private Statement state; 

  private ListSelectionModel searchSelector, searchSelected; 

  private JTable searchTable, createdSearchTable; 
  private JScrollPane searchScroller = new JScrollPane(); 
  private int searchTotal, searchRow; 
  private String[][] searchRecord = new String[ 0 ][ 0 ]; 
  private String queryName = "SELECT * FROM tblNames"; 

  private boolean verifySearch; 

  public MySearch() 
  { 

    JPContainer.setLayout( null ); 

    try 
    { 
      Class.forName( DBDriver ); 
      connect = DriverManager.getConnection( DBSource, "", "" ); 
    }catch( SQLException sql ) { System.out.println( sql.getMessage() ); } 
     catch( ClassNotFoundException notFound ) { System.out.println( notFound.getMessage() ); } 


    searchTable = createSearchTable(); 
    searchScroller.getViewport().add( searchTable ); 
    searchScroller.setBounds( 10, 10, 150, 250 ); 
    JPContainer.add( searchScroller ); 

    capture.setBounds( 1, 1, 1, 1 ); 
    capture.addKeyListener( searchKey ); 
    JPContainer.add( capture ); 

    getContentPane().add( JPContainer ); 
    setLocation( ( size.width - 200 ) / 2, ( ( size.height - 300 ) / 2 ) - 45 ); 
    setSize( 200, 300 ); 
    setVisible( true ); 

  } 

  protected JTable createSearchTable() 
  { 
    String[] searchColumn = { "Name" }; 

    try 
    { 
      state = connect.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE ); 
      rs = state.executeQuery( queryName ); 

      rs.afterLast(); 
      if( rs.previous() ) searchTotal = rs.getRow(); 
      rs.beforeFirst(); 

      if( searchTotal > 0 ) 
      { 
        searchRecord = new String[ searchTotal ][ 1 ]; 
        while( rs.next() ) 
        { 
          searchRecord[ searchRow ][ 0 ] = ""+ rs.getString( "Names" ); 
          searchRow++; 
        } 
      } 
      else 
      { 
        searchRecord = new String[ 0 ][ 1 ]; 
        while( rs.next() ) 
        { 
          searchRecord[ 0 ][ 0 ] = ""+ rs.getString( "Names" ); 
        } 
      } 
    }catch( SQLException sql ) { System.out.println( sql.getMessage() ); } 

    createdSearchTable = new JTable( searchRecord, searchColumn ) 
    { 
      public boolean isCellEditable( int row, int cols ) 
      { return false; } 
    }; 

    createdSearchTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); 
    searchSelector = createdSearchTable.getSelectionModel(); 
    searchSelector.addListSelectionListener( searchListener ); 

    searchColumn = null; 
    searchRecord = null; 
    searchRow = 0; 

    return createdSearchTable; 
  } 

  ListSelectionListener searchListener = new ListSelectionListener() 
  { 
    public void valueChanged( ListSelectionEvent listEvent ) 
    { 
      searchSelected = ( ListSelectionModel ) listEvent.getSource(); 

      if( searchSelected.getValueIsAdjusting() ) return; 

      if( searchSelected.isSelectionEmpty() ) 
      { 
        verifySearch = true; 
      } 
      else 
      { 
        verifySearch = false; 
      } 

    } 
  }; 

  KeyListener searchKey = new KeyListener() 
  { 
    public void keyTyped( KeyEvent keyEvent ) 
    { 
       getKeyTyped( keyEvent ); 
    } 

    public void keyPressed( KeyEvent keyEvent ) { } 
    public void keyReleased( KeyEvent keyEvent ) { } 

  }; 

  protected void getKeyTyped( KeyEvent eventKey ) 
  { 
    char accumulate = eventKey.getKeyChar(); 
    System.out.println( accumulate ); 
    reloadSearch( "SELECT * FROM tblNames WHERE Names LIKE '"+accumulate+"%"+"' " ); 
  } 

  protected void reloadSearch() 
  { 
    searchScroller.getViewport().remove( searchTable ); 
    searchTable = createSearchTable(); 
    searchScroller.getViewport().add( searchTable ); 
  } 

  protected void reloadSearch( String namesQuery ) 
  { 
    queryName = namesQuery; 
    searchScroller.getViewport().remove( searchTable ); 
    searchTable = createSearchTable(); 
    searchScroller.getViewport().add( searchTable ); 
  } 


  public static void main( String[] args ) 
  { 
    MySearch app = new MySearch(); 
    app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
  } 
} 
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 19 2006
Added on Nov 20 2006
7 comments
38 views