As an assignment I have written a program using an HTML form, a servlet, an applet and a simple database with two tables. The idea behind this program is to enter a name in a text box of the html file, get the name in a servlet, search for the name in the database and inform the user whether or not the name is in the database.
The database is MySQL and consists of two tables; tableA has three names entered into it via insert when the table was created, John Doe, Jane Doe, and David Paterson. TableB is used for the name being searched (whatever is entered in the text field of the HTML). If the name is found a question mark is inserted followed by the name. If it is not found an exclamation mark followed by the name. For example the name: David Doe is not in tableA therefore !David Doe is inserted in tableB. But for John Doe ?John Doe is inserted in tableB. The applet is then using tableB to display a message telling whether the name is (or is not) in tableA.
It's fairly convoluted. You enter a name in the HTML text field and click submit; that takes you to a servlet that displays the message/link "Click here to see the applet". When you click the link it displays another page that says, for example, "John Doe is in the table".
I have that all working correctly. Now the last step that I can't seem to get to work is to have that applet message "John Doe is in the table" display in a green oval w/white text, or if the name is not one of those three names in tableA the message should display in a red oval with black text. It just continues to display as regular text on a plain white background.
I am including the applet code only, does anyone see what I have done wrong? I wrote the same code as a standalone applet just to see if I could get a message to display in a green oval with white text and it worked fine, so I'm not sure if it's where I'm placing the code. BTW, this professor is unhelpful I have asked him for advice but as an on-line student it's hard to pin him down and I'm quite new to Java.
The Applet code:
import java.applet.*;
import java.awt.Color;
import java.awt.Graphics;
import java.sql.*;
public class MyApplet extends Applet {
public void paint(Graphics page){
String name = null, message = null;
//page.drawString("XXXXXXXXXXXXXXX", 20, 20);
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/databasea?user=root&password=root");
Statement stmt = con.createStatement();
stmt.executeQuery("use databaseA");
ResultSet rs = stmt.executeQuery("Select * from tableB");
while ( rs.next())
name = rs.getString(1);
if(name.charAt(0) == '?') {
page.setColor(Color.GREEN);
page.fillOval(10, 10, 180, 50);
page.setColor(Color.WHITE);
page.drawString(message, 30, 40);
message = name.substring(1) + " is in the table";
} else {
page.setColor(Color.RED);
page.fillOval(10, 10, 180, 50);
page.setColor(Color.BLACK);
page.drawString(message, 30, 40);
message = name.substring(1) + " is not in the table";
}
String s = "delete from tableB where name = \'" + name + "\'";
stmt.executeUpdate(s);
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
System.out.println("Message: " + e);
//System.exit(0);
}
}
}