TOC PREV NEXT INDEX



A Servlet which connects to a Database


In this section, we will change the Servlet that you previously created so that it will connect to a database and display information from it.

Setting up the Servlet

First you must set up a few variables in the declaration code.

  1. In the Servlet Composer, choose 'Goto declaration code' from the Code menu.
  2. Type the following statements. You could create them using the Sourcerer if you desire.
import java.sql.*;
Connection con = null;
String yourSales = null;

Next, we must set up your servlet to open a connection to a database. Since this is something which must only be done once when the Servlet is started, the code should be entered into the init method of the Servlet. We will create the code using the Code Sourcerer.

  1. Choose the 'Servlet Methods' tab of the Servlet Composer.
  2. Choose 'init' from the choicebox to the left of the Code Sourcerer button to select the init section of the Servlet Composer.
  3. Press the Code Sourcerer button. You will see the Code Sourcerer main menu.
  4. Choose 'JDBC operations...' and press Next.
  5. Choose 'Connect to a database using the DriverManager...' and press Next. You will now see a page with spaces for the JDBC driver and the URL to connect to. These will be set up correctly for the PointBase database, although you can change them if desired.

In this tutorial we will be connecting to the PointBaseTM database. Simplicity Professional includes a demonstration version of the PointbaseTM database server. (The demonstration is limited to databases of 5 megabytes in size.) You can start the database server by choosing "Start Pointbase Server" from the Database menu in the Composer.

  1. Enter "PUBLIC" in the area for User Name.
  2. Enter "PUBLIC" in the area for Password.
  3. After you have entered this information, press Next. You will now be prompted for the return value.
  4. Clear the left hand box, leaving only 'con' on the right. You have already set up this variable in the declarations area, so you do not need to specify that it is a Connection object.
  5. Press done.
  6. The code that you have generated will look like
import java.sql.*;
try {
Class.forName("com.pointbase.net.netJDBCDriver").newInstance();
con = DriverManager.getConnection(
"jdbc:pointbase://127.0.0.1/sample","PUBLIC","PUBLIC");
}
catch ( SQLException excpt0 ) {
}
catch ( ClassNotFoundException excpt1 ) {
}
catch ( InstantiationException excpt2 ) {
}
catch ( IllegalAccessException excpt3 ) {
}

Next, we must create the code the Servlet will execute when it is shut down.

  1. Choose 'destroy' from the choicebox to the left of the Code Sourcerer button.
  2. Press the Code Sourcerer button.
  3. Choose 'JDBC operations...' and press Next.
  4. Choose 'Close a connection to a database...' and press Next.
  5. Press done to accept the default value for the name of the Connection object.

Data Representations, Inc.
http://www.datarepresentations.com
support@datarepresentations.com
sales@datarepresentations.com
TOC PREV NEXT INDEX