Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Using JDBC to Connect to a Database

November 23, 1998

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();

JDBC Application Environment Setup
Introduction to Databases for the Web | Table of Contents
Using JDBC to Query a Database


Up to => Home / Authoring / DB / Intro




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers