Search Function Aint Working! :(
im trying to get my search function working but it just doesnt wanna work! this is my searchContacts method:publicvoid searchContacts(String contactName, JPanel searchPanel){
try{
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
resultSet = statement.executeQuery("select * from " + contactName.toUpperCase().charAt(0) +" where NAME like '%" + contactName +"%'");
while(resultSet.next()){
searchName = resultSet.getString("NAME");
searchNumber = resultSet.getString("NUMBER");
searchEMail = resultSet.getString("EMAIL");
System.out.println("NAME\nNUMBER\nEMAIL");
searchPanel.add(new ContactPanel(searchName, searchNumber, searchEMail));
}
}catch (SQLException ex){
ex.printStackTrace();
}
}
it just searches the tables in my database for the contact. this is my buildSearchResultsDialog method:publicvoid buildSearchResultsDialog(){
searchResultsPanel =new JPanel();
searchResultsPanel.setLayout(new BoxLayout(searchResultsPanel, BoxLayout.Y_AXIS));
searchResultsScrollPane =new JScrollPane(searchResultsPanel);
searchResultsScrollPane.getVerticalScrollBar().setUnitIncrement(20);
searchResultsDialog =new JDialog(mainFrame,"Search Results",true);
searchResultsDialog.add(searchResultsScrollPane);
searchResultsDialog.pack();
searchResultsDialog.setLocationRelativeTo(null);
searchResultsDialog.setResizable(false);
searchResultsDialog.setVisible(true);
}
this method just gives a graphical representation of the contacts found. and this is how i use the methods:if(event.getSource() == searchField || event.getSource() == searchButton){
contactIO.searchContacts(searchField.getText(), searchResultsPanel);
buildSearchResultsDialog();
}
any1 know what could be wrong?
Message was edited by:
Alex1989
[3048 byte] By [
Alex1989a] at [2007-11-27 9:10:53]

> Well you're building the searchResultsPanel after
> your search effectively freeing your search results
> to be garbage collected, i.e. this
> linesearchResultsPanel = new JPanel();
is
> messing it up.
thnx! i would have never figured that one out
> Btw, am I getting this right? You place contacts in
> different tables based on the first letter of their
> name?
> What if I want to retrieve all the contacts? What if
> I want to search for any name that contains "ohn",
> that wouldn't search in the J table to give me John
> or Johnathan or Johnson?
>
> I would love to hear your reasoning for this setup.
im doing it so that i can load the contacts back in nicely with this method: public void loadContacts(JPanel[] loadPanelArray){
for(i = 65; i <= 90; i++){
try{
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
resultSet = statement.executeQuery("select ID, NAME, NUMBER, EMAIL from " + String.valueOf((char)(i)) + " order by NAME");
while(resultSet.next()){
loadID = resultSet.getInt("ID");
loadName = resultSet.getString("NAME");
loadNumber = resultSet.getString("NUMBER");
loadEMail = resultSet.getString("EMAIL");
loadPanelArray[i - 65].add(new ContactPanel(loadID, loadName, loadNumber, loadEMail));
}
}catch(SQLException ex){
ex.printStackTrace();
}
}
}
but yea i see what u mean, its not such a great design and im gonna change it, but ive first gotta figure out how to load the contacts back in properly with just one table
I'm not sure what you mean by properly.
But let's say you store all your contacts in a table called let's say contacts then you could do it like thispublic void loadContacts(JPanel[] loadPanelArray) {
try {
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
resultSet = statement.executeQuery("select ID, NAME, NUMBER, EMAIL from CONTACTS order by NAME");
while(resultSet.next()) {
loadID = resultSet.getInt("ID");
loadName = resultSet.getString("NAME");
loadNumber = resultSet.getString("NUMBER");
loadEMail = resultSet.getString("EMAIL");
loadPanelArray[loadName.toUpperCase().charAt(0) - 65].add(new ContactPanel(loadID, loadName, loadNumber, loadEMail));
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
// don't forget to release resources
statement.close();
resultSet.close();
}
}
You'll see substantial performance improvements because of only one database access instead of one for each letter in the alphabet. Not to mention you'll only have to manage one table instead of many.
And also, It's not a good idea to mix together your database access and your GUI. Rather have this method return a List of Contact objects and populate your panels outside of this method.
dwga at 2007-7-12 21:55:59 >
