0 Replies - 628 Views - Last Post: 05 September 2009 - 06:07 AM

#1 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-September 09

Java JDBC and mySQL 5

Posted 05 September 2009 - 06:07 AM

Dear Sir / Ma'am

I'm a beginner of JDBC and mySQL5.

I had created a statement for the JDBC as shown below:


try{


Class.forName("com.mysql.jdbc.Driver")
// Establish the connection
c = DriverManager.getConnection
("jdbc:mysql://localhost/dbanimals", "root", "");

stmt1 = c.createStatement();

String sql1 = "SELECT * FROM animals WHERE " +
"word = '" + txtSearch.getText() + "'";

rs1 = stmt1.executeQuery(sql1);


How should i create a if and else statement for JDBC :


if a word from a Jtextfield (txtSearch.getText()) match with mySQL database field, it will execute the (rs1 = stmt1.executeQuery(sql1);) statement, else it will prompt a JOptionPane.showMessageDialog(null, "Invalid word! Please re enter other word"); to indicate that there is no such word found in mySQL database or incorrect word is entered.


I did try out other methods but it didn't works


This is the mySQL database:

+----+------+-------------------------
| no | word | explanation | num_of_searches |
+----+------+-------------------------
| 1 | Bee | An insect that produces honey. | 0 |
| 2 | Dog | An animal that bark. | 2 |
+----+------+-------------------------

Thanks in advanced

This post has been edited by [email protected]: 05 September 2009 - 06:09 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1