Hi I am making some kind of search engine in java.In which i want to search initials from database(ms access)
suppose to search united states of america
we type usoa
so how can i do that
from where to i start
can anybody plz.........

Recommended Answers

All 10 Replies

karamjeet2004,

You have to use java.sql package and sql select statement with pattern (%,?) and where caluse.

package src.maintenance.accntMasterfile;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import src.maintenance.DBConn.DBConnect;
import src.maintenance.Functions.ActionCursor;
import src.maintenance.Functions.Message;
import src.maintenance.Functions.MyField;
import src.maintenance.core.mainform;

public class EditaccntMasterfile extends JDialog {

	/**
	 * 
	 */
	/**
	 * @Author:	Oliver D. Lundag
	 * @Title:	Maintenance for Account Master File
	 * 			
	 * @oliver_lundag@yahoo.com
	 */
	private static final long serialVersionUID = 1L;
	
	private JPanel jContentPane = null;
	
	private JLabel jlSearch = null;
	
	private MyField jtSearch = null;
	private JLabel jlSearchby = null;
	
	private JButton jExit = null;
	
	static JComboBox jcbsearchBy = null;
	
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private DefaultTableModel tableSearch = new DefaultTableModel(new String[][]{},new String[]{"No.", "accntcode", "bankcode", "accntnumber","accntdesc"});
	private DefaultTableModel tempTable = new DefaultTableModel(new String[][]{},new String[]{"No.","accntcode", "bankcode", "accntnumber","accntdesc"});
	private String defaultQuery = "select accntcode, bankcode, accntnumber,accntdesc from accntmasterfile order by accntcode desc ";
	private String xString = ""; 
	private Timer sTimer = null;
	private int timer = 0;

	public EditaccntMasterfile(){
		super(mainform.frame);
		this.setDefaultCloseOperation(EditaccntMasterfile.DO_NOTHING_ON_CLOSE);
		 initialize();
	}
	
	private void initialize(){
		this.setSize(500, 450);
		this.setTitle("Account Master File");
		this.setLocationRelativeTo(null);
		this.setContentPane(getJContentPane());
		this.setVisible(true);
	}
	
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			
			jlSearchby = new JLabel();
			jlSearchby.setText("Search by:");
			jlSearchby.setSize(new Dimension(110, 20));
			jlSearchby.setLocation(new Point(15, 340));
			
			jlSearch = new JLabel();
			jlSearch.setText("Search:");
			jlSearch.setSize(new Dimension(110, 20));
			jlSearch.setLocation(new Point(15, 380));			
		}
		jContentPane = new JPanel();
		jContentPane.setLayout(null);
		
		jContentPane.add(jlSearchby,null);
		jContentPane.add(jlSearch,null);
		jContentPane.add(getjcbsearchBy(), null);
		jContentPane.add(getJtSearch(), null);
		jContentPane.add(getjExit() , null);
		jContentPane.add(getJScrollPane(),null);
		
		return jContentPane;
	}
	
	private JComboBox getjcbsearchBy(){
		if(jcbsearchBy == null){
			jcbsearchBy = new JComboBox();
			jcbsearchBy.setSize(new Dimension(150, 20));
			jcbsearchBy.setLocation(new Point(100, 340));
			jcbsearchBy.addItem("Account Code");
			jcbsearchBy.addItem("Bank Code");
			jcbsearchBy.addItem("Account Number");
			jcbsearchBy.addItem("Account Desc");
			jcbsearchBy.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0) {
					try{
						jTable.setRowSelectionInterval(0, 0);
						jTable.grabFocus();
						if	(jcbsearchBy.getSelectedIndex() == 0){
							jtSearch.setText("");
							jtSearch.grabFocus();
						}
						else if	(jcbsearchBy.getSelectedIndex() == 1){
							jtSearch.setText("");
							jtSearch.grabFocus();
						}
						
						else if	(jcbsearchBy.getSelectedIndex() == 2){
							jtSearch.setText("");
							jtSearch.grabFocus();
						}
						
						else if	(jcbsearchBy.getSelectedIndex() == 3){
							jtSearch.setText("");
							jtSearch.grabFocus();
						}
					}catch (IllegalArgumentException e) {}
				}
			});
		}
		return jcbsearchBy;
	}
	
	private JTextField getJtSearch() {
		if (jtSearch == null) {
			jtSearch = new MyField(true,20);
			jtSearch.setLocation(new Point(100, 380));
			jtSearch.setSize(new Dimension(150, 20));
			jtSearch.addKeyListener(new KeyAdapter(){
				@SuppressWarnings("static-access")
				public void keyReleased(KeyEvent e){
					InputMap IMP = jtSearch.getInputMap(jtSearch.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
                    KeyStroke ent = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
         		    IMP.put(ent, "none");
         		    
         		   if	(e.getKeyCode() == 10){
   		    		  setTable(jtSearch.getText().trim());
   		    		  if (jTable.getRowCount()==0){
   		    			  Message.messageInfo("No record found.");
   		    			  System.out.println("Walang record");
   		    		  }
   		    		  timer = 0;
         		   }
         		   
				}    
			});
		}
		return jtSearch;
	}
	
	private JButton getjExit() {
		if (jExit == null) {
			jExit = new JButton();
			jExit.setBounds(new Rectangle(380, 380, 100, 20));
			jExit.setMnemonic(KeyEvent.VK_S);
			jExit.setText("Cancel");
			jExit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (jExit.getText().toString().equalsIgnoreCase("Cancel") == true){
						try{
						Cancel();
						dispose();
						}catch(NullPointerException e1){
						}
					}
				}
			});
		}
		return jExit;	
	}
	
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(10, 10, 470, 300));
			jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			jScrollPane.setViewportView(getJTable());
			jScrollPane.setVisible(true);
		}
		return jScrollPane;
	}
	
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable(tableSearch){
				public boolean isCellEditable(int irows, int icols){
					return false;
				}
			};
			
			TableCellRenderer renderer = new Renderer();
			jTable.setDefaultRenderer(Object.class,renderer);
			
			jTable.setSelectionMode(0);
			jTable.getTableHeader().setResizingAllowed(false);
			jTable.getTableHeader().setReorderingAllowed(false);
			
			jTable.getColumnModel().getColumn(0).setMinWidth(0);
	        jTable.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
			jTable.getColumnModel().getColumn(1).setMinWidth(80);
	        jTable.getTableHeader().getColumnModel().getColumn(1).setMaxWidth(80);
	        jTable.getColumnModel().getColumn(2).setMinWidth(140);
	        jTable.getTableHeader().getColumnModel().getColumn(2).setMaxWidth(140);
	        jTable.getColumnModel().getColumn(3).setMinWidth(80);
	        jTable.getTableHeader().getColumnModel().getColumn(3).setMaxWidth(80);
	        jTable.getColumnModel().getColumn(4).setMinWidth(150);
	        jTable.getTableHeader().getColumnModel().getColumn(4).setMaxWidth(150);
			
			jTable.getColumnModel().getColumn(1).setHeaderValue("Account Code");
	        jTable.getColumnModel().getColumn(2).setHeaderValue("Bank Code");
	        jTable.getColumnModel().getColumn(3).setHeaderValue("Account No.");
	        jTable.getColumnModel().getColumn(4).setHeaderValue("Account Description");
	        
	        jTable.addKeyListener(new KeyAdapter(){
				public void keyPressed(KeyEvent e){
					InputMap IMP = jTable.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
                    KeyStroke ent = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
         		    IMP.put(ent, "none");
         		    
         		    if	(e.getKeyCode() == 10){
         		    	if	(xString.toString().trim().length() == 0){
         		    		for	(int i = 0; i < tempTable.getRowCount(); i++){
         		    			if	(jcbsearchBy.getSelectedIndex() == 0){
	         		    			if (tempTable.getValueAt(i, 1).toString().equalsIgnoreCase(jTable.getValueAt(jTable.getSelectedRow(), 1).toString())){
	         		    				accntMasterfile.xRecordE = Integer.parseInt(tempTable.getValueAt(i, 0).toString());
	         		    				break;
	         		    			}
         		    			}else if (jcbsearchBy.getSelectedIndex() == 1){
	         		    			if (tempTable.getValueAt(i, 2).toString().equalsIgnoreCase(jTable.getValueAt(jTable.getSelectedRow(), 2).toString())){
	         		    				accntMasterfile.xRecordE = Integer.parseInt(tempTable.getValueAt(i, 0).toString());
	         		    				break;
	         		    			}
         		    			}else if (jcbsearchBy.getSelectedIndex() == 2){
	         		    			if (tempTable.getValueAt(i, 3).toString().equalsIgnoreCase(jTable.getValueAt(jTable.getSelectedRow(), 3).toString())){
	         		    				accntMasterfile.xRecordE = Integer.parseInt(tempTable.getValueAt(i, 0).toString());
	         		    				break;
	         		    			}
         		    			}else if (jcbsearchBy.getSelectedIndex() == 3){
	         		    			if (tempTable.getValueAt(i, 4).toString().equalsIgnoreCase(jTable.getValueAt(jTable.getSelectedRow(), 4).toString())){
	         		    				accntMasterfile.xRecordE = Integer.parseInt(tempTable.getValueAt(i, 0).toString());
	         		    				break;
	         		    			}
         		    			}
         		    		}
         		    		accntMasterfile.getRecordE();
         		    		setenabledUpdate();
             		    	dispose();	
         		    	}
         		    	
         		    	
         		    }else{
         		    	if	((e.getKeyCode() > 65 && e.getKeyCode() < 90) || (e.getKeyCode() > 96 && e.getKeyCode() < 105)){
         		    		char[] keyChar ={ e.getKeyChar()};
         		    		String x = new String(keyChar);
         		    		xString = (xString+x);
         		    		setTable(xString);
         		    		timer = 0;
         		    	}
         		    }
				}
			});	
			setTable();
			setTimer();
		}
		return jTable;
	}
	
	private void setTimer(){
		sTimer = new Timer (500,new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				timer++;
				if	(timer == 3){
					xString = "";
				}
			}
		});
		sTimer.start();
	}
	
	private class Renderer extends DefaultTableCellRenderer{
		private static final long serialVersionUID = 1L;

		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
		{
			super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
			if (column == 4)setHorizontalAlignment(LEFT);
			if (column == 3)setHorizontalAlignment(LEFT);
			if (column == 2)setHorizontalAlignment(LEFT);
			if (column == 1)setHorizontalAlignment(LEFT);
			setBorder(BorderFactory.createLineBorder(getBackground(), 1));
			return this;
		}
	}
	
	private void setTable(String like){
		ActionCursor actionCursor = new ActionCursor();
		try	{
			boolean ok = false;
			String likeQuery = "";
			if	(jcbsearchBy.getSelectedIndex() == 0){
				likeQuery = "select " +
		    	 "accntcode, bankcode, accntnumber, accntdesc " +
		    	 "from accntmasterfile  " +
		    	 "where " +
		    	 "accntcode like '"+like.trim()+"%' " +
		    	 "order by accntcode ";	
			}else if (jcbsearchBy.getSelectedIndex() == 1){
				likeQuery = "select " +
		    	 "accntcode, bankcode, accntnumber, accntdesc " +
		    	 "from accntmasterfile  " +
		    	 "where " +
		    	 "bankcode like '"+like.trim()+"%' " +
		    	 "order by accntcode ";
			}else if (jcbsearchBy.getSelectedIndex() == 2){
				likeQuery = "select " +
		    	 "accntcode, bankcode, accntnumber, accntdesc " +
		    	 "from accntmasterfile  " +
		    	 "where " +
		    	 "accntnumber like '"+like.trim()+"%' " +
		    	 "order by accntcode ";
			}else if (jcbsearchBy.getSelectedIndex() == 3){
				likeQuery = "select " +
		    	 "accntcode, bankcode, accntnumber, accntdesc " +
		    	 "from accntmasterfile  " +
		    	 "where " +
		    	 "accntdesc like '"+like.trim()+"%' " +
		    	 "order by accntcode ";
			}
			
			
			int n = 0;
			Statement stmt = DBConnect.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(likeQuery);
			actionCursor.startWaitCursor(jContentPane);
			if	(ok == false){
				while(jTable.getRowCount() != 0){
						tableSearch.removeRow(0);
				}
				ok = true;
			}
			while (rs.next()){
				tableSearch.addRow(new Object[]{rs.getRow(),rs.getString(1), rs.getString(2),rs.getString(3),rs.getString(4)});
				n++;
			}
			if	(n > 0){
				jTable.setRowSelectionInterval(0, 0);
				jTable.grabFocus();
			}
			rs.close();
			stmt.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
			actionCursor.stopWaitCursor(jContentPane);
	}
	
	private void setTable(){
		try	{
			Statement stmt = DBConnect.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(defaultQuery);
			int n = 0;
			while (rs.next()){
				tempTable.addRow(new Object[]{rs.getRow(), rs.getString(1), rs.getString(2),rs.getString(3),rs.getString(4)});
				tableSearch.addRow(new Object[]{rs.getRow(), rs.getString(1), rs.getString(2),rs.getString(3),rs.getString(4)});
				n++;
			}
			rs.close();
			stmt.close();
			if	(n > 0){
				jTable.setRowSelectionInterval(0, 0);
				jTable.grabFocus();
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void setenabledUpdate(){
		accntMasterfile.jtBcode.grabFocus();
		accntMasterfile.jbtnadd.setText("Update");
		accntMasterfile.jbtnadd.setMnemonic('U');
		accntMasterfile.jbtnedit.setText("Delete");
		accntMasterfile.jbtnedit.setMnemonic('D');
		accntMasterfile.jbtnExit.setText("Cancel");
		accntMasterfile.jbtnExit.setMnemonic('C');
		accntMasterfile.jtAcode.setEnabled(false);
		accntMasterfile.jtBcode.setEnabled(true);
		accntMasterfile.jtAccntNo.setEnabled(true);
		accntMasterfile.jtAccontDesc.setEnabled(true);
	}
	
	private void Cancel(){
		accntMasterfile.jtAcode.setText("");
		accntMasterfile.jtBcode.setText("");
		accntMasterfile.jtAccntNo.setText("");
		accntMasterfile.jtAccontDesc.setText("");
		accntMasterfile.jtAcode.setEnabled(false);
		accntMasterfile.jtBcode.setEnabled(false);
		accntMasterfile.jtAccntNo.setEnabled(false);
		accntMasterfile.jtAccontDesc.setEnabled(false);
	}
}
commented: Stop posting your rubbish! -3

oliver, just stop posting.

why?

  1. Don't talk with old or solved threads.
  2. Use BB code tags
  3. Read all posts of a thread and then put your comment.

4. stop hi-jacking threads
5. don't post complete code (at least initially)

commented: Thanks. +7

I am the one who made that code. just want to share it. why are you complaining... I just want to share... instead of saying that thing to me why don't you try to help those people who needs help. I think you are selfish.

thanks for the code
i am new to java
it took me a week to understand
but anyway thanks

Thanks,
Mark this thread as "Solved" if you get appropriate solution.

I am the one who made that code. just want to share it. why are you complaining... I just want to share... instead of saying that thing to me why don't you try to help those people who needs help. I think you are selfish.

Wrong!

You are just spoon-feeding the OP, is that what you want?
I can say you: it's against Daniweb's homework policy.
Ever heard of this? I would say: No.

And look, you're even so poor that you still can't use code tags correctly to post your crap, is it too difficult for you to just type the following on your keyboard?

[code=Java] // Your rubbish here

[/code]
(Reminder on how to use code tags: http://www.daniweb.com/forums/announcement9-3.html)

>I think you are selfish.
Well, do you think that? I don't.
He at least tries to help people learn, not to give them away a solution where they don't benefit from (in sense of: they don't learn anything from just copying that).
You don't have to think you're superior now because you've given away some code.
If you want to share this code, then I would rather go for a code snippet (to mention: you can't have problems with the code tags there, except in the comment(s)).

>instead of saying that thing to me why don't you try to help those people who needs help.
Do you think you've helped the OP then?
By just providing your code?
I don't think that.
My understanding of the word 'help' is that the OP will be able to do it himself the next time, and that he won't have to beg for some code.

>I am the one who made that code.
And what can we conclude then?

commented: thanks for the support :) +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.