Online Software Educational Training - IT Tutorials - Online Education Training for Computer Software


Home

JAVA JDBC

Working with Database Using JDBC

Connecting to a Database

The java.sql. package contains classes that help in connecting to a database, sending SQL statements to the database, and processing query results.

The Connection Object

The Connection object represents a connection with a database. You may have several Connection objects in an application that connects to one or more databases.

Loading the JDBC-ODBC Bridge and Establishing Connection 

To establish a connection with a database, you need to register the ODBC-JDBC Driver by calling the forName() method from the Class class and then calling the getConnection() method from the DriverManager Class.

The getConnection() method of the DriverManager() class attempts to locate the driver that can connect to the database represented by the JDBC URL passed to the getConnection() method.

Example :-

String url="jdbc:odbc:myDSN";                // Where myDSN is name of the DSN(Data Source Name)

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");    //Registering Driver

Connection con  DriverManager.getConnection(url);    // TO connect to the Database

 

Using Statement object

You can use the Statement object to send simple queries to the database.

executeUpdate()    To update the records using query

executeQuery()       This methods executes a query and returns a single ResultSet object.         

 

The ResultSet Object

The ResultSet object provides you with methods to access data from the table. Executing a statement usually generates a ResultSet object. It maintains a cursor pointing to its row data. Initially the cursor is positioned before first row.

 


Previous

Next