Engineeringserver.com

A community for computer science students & developers about software development, game development, game design, games, anime preview & reviews and more!


How to find an item in a MySQL database using Java

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4 out of 5)
Loading ... Loading ...
1,461 views

Here's a method i wrote to find an item in a MySQL database and to show the found item. I wrote this class for someone so the text might be confusing =) details of this application + screenshot can be found here: http://www.engineeringserver.com/blog/2007/12/28/tenant-application/

JAVA:
  1. public void findItemFromDatabase(){
  2. boolean add = true;
  3. int duplicate = 0;
  4. try {
  5. Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  6. Connection conn = DriverManager.getConnection (url, userName, password);
  7. Statement checkTenants = conn.createStatement();
  8. String queryCheckTenants = "select name from tenant";
  9. ResultSet resultCheck = checkTenants.executeQuery(queryCheckTenants);
  10. String find = findTf.getText();
  11. while(resultCheck.next()){
  12. if(resultCheck.getString("Name").equals(find)){
  13. duplicate++;
  14. }
  15. }
  16. if(duplicate>0){
  17. resultBtn.setVisible(true);
  18. resultFoundLbl.setText("Tendant found," + "\n" + "click detail(s) for more info.");
  19. resultFoundLbl.setVisible(true);
  20. searchBtn.setEnabled(false);
  21. findTf.setEditable(false);
  22. found = true;
  23. add = false;
  24. }
  25. else{
  26. resultBtn.setVisible(true);
  27. resultBtn.setEnabled(true);
  28. resultBtn.setText("Add tenant");
  29. resultFoundLbl.setText("No match found, " +"\n" + "please try again or add new tenant.");
  30. resultFoundLbl.setVisible(true);
  31. searchBtn.setEnabled(false);
  32. findTf.setEditable(false);
  33. found = false;
  34. add = true;
  35. }
  36. //obsolette
  37. if (add){
  38. }
  39. if (!add){
  40. }
  41. e.printStackTrace();
  42. e.printStackTrace();
  43. e.printStackTrace();
  44. } catch (SQLException e) {
  45. e.printStackTrace();
  46. }
  47. }


Your Ad Here