The following source code shows you how to connect a DB2 database to a Java Applet. This is a simple example which allows the user to type in an SQL on the tables in a Database and return the results to a scrollable text region. It can easily be altered to work with any kind of database.

Here is an example of how you would call the applet in your HTML file:

Notice the “applet code” command in the above example. This is where you call your applet class and set the width and height of the applet. The class name is case sensitive.

Below is the source code for the applet. Notice the comments in the java source code. There are sometimes lines of code which are commented out and say that that is the code for the application version. DB2 requires different commands if you’re creating an applet, than if you’re creating an application. Note the differences. If you are using a different database language, this limitation should not apply.

// Here we create the objects we plan to use in the applet.
// The Connection object is part of the JDBC API, and is the
// how we link the JdBC’s function to the applet and
// register the driver with DriverManager

// since this data is published on the internet, you will need
// to setup a user which can be used anonymously by anyone who
// accesses this data. You can have that user setup with read
// only privileges as well.
// optionally, you can provide url, name, and password fields
// which the user must fill in before being granted access. You
// would not normally want to do this on an open web site.. ie:
// you are granting users the ability to look at a table full of
// products, real estate, etc.
// if this is used on an intranet, you might then want to setup
// name and password fields, but not necessarily the URL.

// show is a deprecated line (used in older java version)
// to use the new 1.1.1 command issue this instead:
// setVisible(true);

show();

} // init

public boolean handleEvent(Event evt)
{

// The standard format for this method includes the Event class
// where all the properties are set. This is deprecated and
// there is a better way of doing it now, but if you want to
// support the older Java versions, this will work on those and
// 1.1.1 as well.

if (evt.target == QueryField) // if the cursor is in the QueryField
{
char c = (char) evt.key ; // check if the Enter key is pressed
if (c == '\n')
{
// pass the contents of the queryfield to the select method
// the results will show in the outputfield:
OutputField.setText(Select(QueryField.getText()));
return true ;
}
else{ return false ; }
}

if (evt.target == DBFConnect)
{
// when the user clicks the “connect” button, connect to the database
// specified in the url and the user name specified in the name
// field.

// change the name of the button, to indicate the connection
// has been made
DBFConnect.setLabel("Reconnect to DB2");
}

// set exceptions
// an exception will be thrown if there is a problem connecting
// using the specified parameters. The exception will be printed
// to the outputfield and the console.
catch( Exception e)
{
e.printStackTrace();
OutputField.setText(e.getMessage());
}
return true ;
}
return false ;

}// handleEvent() end

public String Select(String QueryLine)
{

// Use this method to create the SQL query command
// we return a String

String Output="";
int columns ;
int pos ;

try
{

// this can throw exceptions if an invalid query is entered
// or if the connection breaks

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…

Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly.
Viewers learn about the third conditional statement …

Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop.
Define While Loop:
Basic Example:
Explanatio…