Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
JComboBox comboBox = new JComboBox();
      comboBox.setModel(new DefaultComboBoxModel(new String[] {"DateSave", "Category", "Amount", "Content"}));
      comboBox.setBounds(250, 476, 107, 26);
      add(comboBox);
      
      textFieldSearch = new JTextField();
      textFieldSearch.addKeyListener(new KeyAdapter() {
      	@Override
      	public void keyPressed(KeyEvent arg0) {
      		try{
    			String selection = (String)comboBox.getSelectedItem();
    			Class.forName("org.gjt.mm.mysql.Driver");
    			String url ="";
    			url = "jdbc:mysql://localhost/project"; 
    			Connection con = DriverManager.getConnection(url, "ITUser", "user"); 
    			String query="Select * from saving where "+selection+" =?";
    			
    			PreparedStatement pst= (PreparedStatement) con.prepareStatement(query);
    			pst.setString(1,textFieldSearch.getText());
    			
    			ResultSet rs=pst.executeQuery();
    			
    			Savingtable.setModel(DbUtils.resultSetToTableModel(rs));
    			String search =	textFieldSearch.getText();
    			if(search.length() == 0){    //to make back normal when search is empty
    				setTableModelFromDB();
    				scrollPane.setViewportView(Savingtable);
    				scrollPane.setColumnHeaderView(Savingtable.getTableHeader());
    			}

    			while(rs.next())
    			{
    		        }

    			pst.close();
    	              }catch(Exception ex){
    		ex.printStackTrace();
    	}
      	}
      });
      textFieldSearch.setBounds(716, 474, 146, 26);
      add(textFieldSearch);
      textFieldSearch.setColumns(10);

I can search from my database from my textField but it must be typed in full text, the result will then appear.For example, the amount is 700,i must type 700 then the result can appear. How to make database result to appear when i search the first letter or number :(
Posted
Updated 22-Jan-16 23:04pm
v2

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900