eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

Introduction to Databases for Web Developers
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();

Previous | Next | Table of Contents