Search code examples
javasqldatabasederby

Derby Query is very slow


I am writing a query to load my data in a jlist

public void showtitle(){
  DefaultListModel model = new DefaultListModel();
  booklist.setModel(model);
  try{
    Class.forName("org.apache.derby.jdbc.ClientDriver");    
    Connection conn = DriverManager.getConnection("jdbc:derby://"+X, "APP", "app");
    Statement stmt = conn.createStatement();
    String query="SELECT TITLE FROM BOOK WHERE ISBN LIKE '%"
      + code.getText().toUpperCase()+"%' OR "
      + " TITLE LIKE '%"+name.getText().toUpperCase()+"%' ";
    ResultSet rs = stmt.executeQuery(query);
    while(rs.next()){
      String isbn = rs.getString(1);
      model.addElement(isbn);
   }
  }
  catch ( ClassNotFoundException | SQLException ex) {
    JOptionPane.showMessageDialog(null, "Unknown Error!! Data cannot be displayed!" + ex);
  }
}  

I am calling this method like this :

private void codeKeyReleased(java.awt.event.KeyEvent evt)
{                                 
  showtitle();
}  

After inserting 1000 data my query is running very slow. Is my procedure not good? Is there some fatal mistake? What should I do?


Solution

  • You're doing more than just executing a query in your method.

    You're also creating the JDBC connection, which is probably a much more expensive operation.

    Trying creating the JDBC connection once and saving it somewhere in your application.

    Then when the user runs your key-released event, just run your query and fetch the results.