Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

SQL etc

Posted Nov 23, 1998

Introduction to Databases for the Web: Pt 4 - Page 2

By Selena Sol

JDBC Application Environment Setup

Assuming that you have already been working through this tutorial, you will already be set to run a JDBC application. However, before we get into the nitty gritty, let's do a recap.

Installing a Database

The first thing you will need, of course is an actual running database with a table that you can query and modify. In the SQL and CGI sections, we explained how to setup an MSAccess database and create a test table. We will continue to use that table and database here. In particular, we will use the CUSTOMERS table from the Test_db.mdb database that we gave a datasource name of "Access" in our 32-bit ODBC control panel.

Installing the JDK

You will also need to install the JDK. I recommend installing JDK 1.17, but that you code your application to the JDK 1.02 standard. That way your application will be accessible to a wider audience of web browsers, many of who may not have browsers that support JDK 1.1 APIs.

As we said before, most of the JDBC drivers you will need are installed by default so you already have them when you install the JDK. Drivers that are not installed by default can be downloaded by going to www.javasoft.com/products/jdbc/index.html. You will install them in the "lib" directory in the Java files tree. I put mine in "c:\java\sun\jdk1.1.7\lib\sun\jdbc\".

Installing a Local Web Server

Finally, you will need a web server to serve your Java application. In the CGI section, we discussed how you could setup the Sambar Web Server on your local workstation. Using a local web server means that you can do all your development and testing locally and then transfer the final application to the web server on the internet. This section assumes you have already installed Sambar and the rest.

Using JDBC to Connect to a Database

Once your environment is all set up, you are ready to start sending SQL commands to your database using JDBC. To do so, you will need to first connect to the database, which is a fairly simple process. Specifically, you will need to load the database driver and then request a connection.

These two things are achieved with the following lines of code:

Class.forName([LOCATION OF DRIVER]);
Connection jdbcConnection = 
      DriverManager.getConnection
      ([LOCATION OF DATASOURCE]);

For example, to connect to my Access (I specified the "Access" DataSource Name in the 32-bit ODBC control panel. The name "Access" corresponds to my Test_db.mdb file created by MsAccess) datasource I use the following:

Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);
Connection jdbcConnection = 
   DriverManager.getConnection
   (jdbc:odbc:Access);

Note that the JdbcOdbcDriver is part of the basic JDK distribution (You can find it in the classes.zip file in the "lib" directory probably). If you are using a driver other than ODBC, you should check to see what syntax it requires for specifying data source name. mSQL, for example, uses something like

Connection jdbcConnection = 
       DriverManager.getConnection
       (jdbc:msql://hostname:9999/test_db");

Preparing a Statement Object

After you have setup a connection to the database, you will need to instantiate a statement object that you can use to pass SQL back and forth between the database and your application. Creating a statement object is simple and follows the form:

Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);
Connection jdbcConnection = 
       DriverManager.getConnection
       (jdbc:odbc:Access);
Statement sql = jdbcConnection.createStatement();


SQL etc Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
SQL etc Forum
Topic By Replies Updated
MySQL rollback UAL225 0 August 21st, 09:56 PM
Complex Search Query Galway 0 May 20th, 10:04 PM
change collation at once supercain 2 May 15th, 06:18 AM
SQL Features, tools and utilities question Neomite 1 April 10th, 09:13 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date