News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Steps involved for making a connection with a database

Started by Kalyan, Aug 24, 2008, 09:06 PM

Previous topic - Next topic

Kalyan

Steps involved for making a connection with a database


a)    Loading the driver : To load the driver, Class.forName( ) method is used.

Class.forName (  "sun.jdbc.odbc.JdbcOdbcDriver" );

When the driver is loaded, it registers itself with the java.sql.DriverManager class as an available database driver.

b)    Making a connection with database : To open a connection to a given database,

DriverManager.getConnection( ) method is used.

Connection con = DriverManager.getConnection ("jdbc:odbc:somedb", "user",  "password");

c)    Executing SQL statements : To execute a SQL query, java.sql.statements class is used.

createStatement( ) method of Connection to obtain a new Statement object.
Statement stmt  = con.createStatement( );

A query that returns data can be executed using the executeQuery( ) method of  Statement. This method executes the statement and returns a java.sql.ResultSet that encapsulates the retrieved data:

ResultSet rs = stmt.executeQuery("SELECT * FROM some table");

d)    Process the results : ResultSet returns one row at a time. Next( ) method of ResultSet  object can be called to move to the next row.  The getString( ) and getObject( ) methods are used for retrieving  column values:

while( rs.next( ) ) {
String event = rs.getString( "event" );
Object count = ( Integer ) rs.getObject( "count" );