Jdbc tutorial, jdbc connection, java resultset, java, jdbc driver, java database connectivity, jdbc source code, tutorial, programming,JDBC driver, JDBC example, Jdbc examples!
Sycorax - complete tutorials
Programming Tutorials
" Java provides the industry - software companies and customer alike , an opportunity to create a true open computing environment where software is portable,
and customers benefit from increase competition. "
Java and the Future
December 1, 2008-LEJB 3.1: EJB New and Improved!
The EJB 3.0 specification was a huge improvement from what you were used to in the early versions of EJB. Available as an early draft, EJB 3.1 has many more features and is even easier to use.
December 1, 2008-Should Java Assert that Network I/O Can't Occur on the UI Thread?
Doing network I/O on the user interface (UI) thread is bad. Most developers know that and can tell you why; unfortunately, it's still done.
Register now to recieve special alert and latest technology news!
Java Database Connectivity Steps
you must first import the java.sql package.
import java.sql.*;
The star ( * ) indicates that all of the classes in the package java.sql are to be imported.
1. Loading database driver
In this step we load the driver class by calling Class.forName() with the Driver class name as an argument. Once loaded, the Driver class creates an instance of itself. A user can connect to Database Server through JDBC Driver. Since most of the Database servers support ODBC driver therefore JDBC-ODBC Bridge driver is commonly used.
The return type of the Class.forName (String ClassName) method is "Class". Class is in class java.lang package.
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // To use mysql driver use com.mysql.jdbc.Driver
} catch(Exception e) {System.out.println("Driver class not loaded!");
}
Your driver documentation provides the class name to use.
2. Creating a jdbc Connection
The JDBC DriverManager class defines objects which can connect Java applications to a JDBC driver. DriverManager is considered the backbone of JDBC architecture. DriverManager class manages the JDBC drivers that are installed on the system. Its getConnection() method is used to establish a connection to a database. It uses a username, password, and a jdbc url to establish a connection to the database and returns a connection object. A jdbc Connection represents a session/connection with a specific database. Within the context of a Connection, SQL, PL/SQL statements are executed and results are returned. An application can have one or more connections with a single database, or it can have many connections with different databases. A Connection object provides metadata i.e. information about the database, tables, and fields. It also contains methods to deal with transactions.
Connection URLs have the following form:
jdbc:mysql:<dbName>[propertyList]
String url = "jdbc:mysql:sycorax"; // sycorax is database name
Connection con = DriverManager.getConnection(url, "root", "mysql"); // root is user name and mysql is password
3. Creating a jdbc Statement object
Once a connection is established we can interact with the database. Connection interface defines methods for interacting with the database via the established connection. To execute SQL statements, you need to instantiate a Statement object from your connection object by using the createStatement() method.
Statement statement = dbConnection.createStatement();
A statement object is used to send and execute SQL statements to a database.
Three kinds of Statements
Statement: It execute simple sql queries without parameters. Statement createStatement() Creates an SQL Statement object.
Prepared Statement: It execute precompiled sql queries with or without parameters. PreparedStatement prepareStatement(String sql) returns a new PreparedStatement object. PreparedStatement objects are precompiled SQL statements.
Callable Statement: It execute for call to a database stored procedure. CallableStatement prepareCall(String sql) returns a new CallableStatement object. CallableStatement objects are SQL stored procedure call statements.
4. Executing a SQL statement with the Statement object, and returning a jdbc resultSet in case of executeQuery(), execute() and integer in case of executeUpdate() .
Statement interface defines methods that are used to interact with database via the execution of SQL statements. The Statement class has three methods for executing statements:
- executeQuery()
- executeUpdate()
- execute()
For a SELECT statement, the method to use is executeQuery . For statements that create or modify tables, the method to use is executeUpdate.
Note: Statements that create a table, alter a table, or drop a table are all examples of DDL
statements and are executed with the method executeUpdate. execute() executes an SQL
statement that is written as String object.
ResultSet provides access to a table of data generated by executing a Statement. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The next() method is used to successively step through the rows of the tabular results.
5. Cleaning up resources
Now all we have to do is close the connection. Actually we should close all of the instances of Connection, Statement and ResultSet and
it's done in reverse order from which they were created:
if (rs != null)
rs.close();
if (statement != null)
statement.close();
if (con != null)
con.close();
<<Types of JDBC Drivers || Test JDBC Driver Installation & Connection >>


