Saturday, July 30, 2011

Running Code for a GUI Applet

On my first attempt at running the modified Applet from within a web page, I was delighted to observe that it worked. I then noticed the following lines of code:

/**
* These are implementation variables
*/
boolean dbDEBUG = false;
boolean mathDEBUG = false;
boolean LIVE = false;

I had very cunningly (in my opinion) put in a safety check. By default, no attempt is made to attempt to connect to a database, and the applet just runs without it. So I had not been so clever with the coding change as I initially thought. The applet had run, but without attempting to implement the new connection code. So with great trepidation I changed both boolean LIVE and boolean dbDEBUG to true, recompiled, and tried again.

Again to my delight, the applet opened, and there was even a debug message saying:

starting... driver succeeds...

However my joy subsided when I clicked the "Click to begin" button, entered an answer clicked "Check Answer" and the debug field read:

SQLException: nullSQLException: null.

The original text file class, used to create the database in Derby, included some code to populate a few lines. On reflection, I could see why I included this, and that perhaps it would have been a good idea to run it first.

So I modified the class to connect to MySQL, renamed it to ItemdbTest, and compiled it. I also modified another text file/command line class, ItemdbQuery, which runs a simple query and displays the table contents. The SQL for the insert was:

"INSERT INTO Items " +
"(Partid, OpCode, Itemdet, Raw, Rate) " +
"VALUES (1, 1, '1+1=', 1, 1), " +
"(1, 1, '2+2=', 1, 1), " +
"(1, 1, '3+3=', 1, 1)";

That ran fine. The SQL for the simple query was:

"SELECT * FROM Items"

and the output was:

try connection
connection succeeds
try query
1 1 1 1+1= 1 1
2 1 1 2+2= 1 1
3 1 1 3+3= 1 1
query succeeds
disconnection succeeds

Now the SQL used here is supposed to reflect that in the Applet, so it was strange that the Applet is falling over, when a similar command from the command line was working.

On further investigation, the answer lay on the What Applets Can and Cannot Do page in the Java Tutorial. I remember when I was first trying to use the Derby embedded driver, an applet can't make changes on the local machine. So I shall have to set up IIS on either this or another machine, set MySQL to accept outside connections, and start again.

No comments: