The JDBC ( Java Database Connectivity) API defines interfaces and classes for writing database applications in Java

by making database connections. Using JDBC you can send SQL, PL/SQL statements to almost any relational database. JDBC is a Java API for executing SQL statements and supports basic SQL functionality. It provides RDBMS access by allowing you to embed SQL inside Java code. Because Java can run on a thin client, applets embedded in Web pages can contain downloadable JDBC code to enable remote database access. You will learn how to create a table, insert values into it, query the table, retrieve results, and update the table with the help of a JDBC Program example. Although JDBC was designed specifically to provide a Java interface to relational databases, you may find that you need to write Java code to access non-relational databases as well.

JDBC Architecture

Java application calls the JDBC library. JDBC loads a driver which talks to the database. We can change database engines without changing database code.

JDBC Basics - Java Database Connectivity Steps
Before you can create a java jdbc connection to the database, 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 a database driver,
In this step of the jdbc connection process, 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 client 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 a class in java.lang package.

try { Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”); //Or any

other driver } catch(Exception x){ System.println( “Unable to load the driver class!” ). It uses a username. } 3. Within the context of a Connection. Once a connection is obtained we can interact with the database. JDBC URL Syntax:: jdbc: <subprotocol>: <subname> JDBC URL Example:: jdbc: <subprotocol>: <subname>•Each driver has its own subprotocol •Each subprotocol has its own syntax for the source. tables. and fields. you need to instantiate a Statement object from your connection object by using the createStatement() method. or it can have many connections with different databases. A jdbc Connection represents a session/connection with a specific database. SQL. so the DriverManager knows to use the sun. We’re using the jdbc odbc subprotocol. DriverManager is considered the backbone of JDBC architecture.odbc.jdbc.getConnection(url.JdbcOdbcDriver. information about the database. Connection interface defines methods for interacting with the database via the established connection. Creating a jdbc Statement object.out. To execute SQL statements.out.createStatement(). Its getConnection() method is used to establish a connection to a database. try{ Connection dbConnection=DriverManager. An application can have one or more connections with a single database.”Passwo rd”) } catch( SQLException x ){ System. Creating a oracle jdbc Connection The JDBC DriverManager class defines objects which can connect Java applications to a JDBC driver. A statement object is used to send and execute SQL statements to a database. DriverManager class manages the JDBC drivers that are installed on the system. Three kinds of Statements . It also contains methods to deal with transactions.println( “Couldn’t get connection!” ). and a jdbc url to establish a connection to the database and returns a connection object. password. } 2.”loginName”. PL/SQL statements are executed and results are returned. Statement statement = dbConnection. A Connection object provides metadata i.e.

alter a table. The table rows are retrieved in sequence. For statements that create or modify tables. try { String className = “sun. output. CallableStatement prepareCall(String sql) returns a new CallableStatement object. Prepared Statement: Execute precompiled sql queries with or without parameters. The Statement class has three methods for executing statements: executeQuery(). executeUpdate(). Callable Statement: Execute a call to a database stored procedure. the method to use is executeUpdate. the method to use is executeQuery .JdbcOdbcDriver”. public class TestJDBCDriverInstallation_Oracle { public static void main(String[] args) { StringBuffer output = new StringBuffer(). PreparedStatement prepareStatement(String sql) returns a new PreparedStatement object.jdbc. 4.Statement: Execute simple sql queries without parameters. For a SELECT statement. CallableStatement objects are SQL stored procedure call statements. and execute(). ResultSetMetaData Interface holds information on the types and properties of the columns in a ResultSet. Executing a SQL statement with the Statement object.append(”Testing oracle driver installation \n”). PreparedStatement objects are precompiled SQL statements.swing. Test JDBC Driver Installation import javax. ResultSet provides access to a table of data generated by executing a Statement.odbc. execute() executes an SQL statement that is written as String object. The next() method is used to successively step through the rows of the tabular results. and returning a jdbc resultSet. Statement interface defines methods that are used to interact with database via the execution of SQL statements. A ResultSet maintains a cursor pointing to its current row of data. It is constructed from the Connection object.JOptionPane. Note: Statements that create a table. . Statement createStatement() Creates an SQL Statement object. or drop a table are all examples of DDL statements and are executed with the method executeUpdate.

println(”Driver Version : “+meta. } catch (Exception e) { output = new StringBuffer().Connection. password = “tiger”.out. if(con!= null){ System. output.out. System.showMessageDialog(null. System.getMessage()).append(”Driver Installation Successful”).out. java.Class driverObject = Class. output). System. JDBC Driver Example import import import import java.println(”Got Connection.DriverManager.println(”Driver Name : “+meta. java.showMessageDialog(null. public static void main(String[] args) throws Exception { Connection con = getOracleJDBCConnection().sql.DatabaseMetaData. output. } } } Download JDBC Sample Code Java JDBC Connection Example. DatabaseMetaData meta = con.SQLException. java.sql.append(”Driver : “+driverObject+”\n”). static String url = “jdbc:odbc:bob”. JOptionPane. public class JDBCDriverInformation { static String userid=”scott”.getDriverName()). }else{ .forName(className).sql.getDriverVersion()).out.println(”Failed: Driver Error: ” + e.”).sql.getMetaData(). static Connection con = null. output). JOptionPane.append(”Driver Installation FAILED\n”). output.

Before the implementation of class we need to import a package java. } } public static Connection getOracleJDBCConnection(){ try { Class.JdbcOdbcDriver”).out.println(”SQLException: ” + ex.println(”Could not Get Connection”).ClassNotFoundException e) { System. System.sql.-----------For this program code we have a class Jdbc Connection.println(e.getConnection(url. userid.getMessage()).err.getConnection ( ) :This method returns you an connection object. We have a list of method required to connect and close the connection.err. } try { con = DriverManager.jdbc.print(”ClassNotFoundException: “). } catch(java.* needed for network interface between url and back end.odbc.getConnection built a connection with database. Loading a driver is done by calling a class.println print the connection is created .lang.forName ( ) and accept the driver class as argument. DriverManager.getMessage()).err. } catch(SQLException ex) { System. } return con.forName(”sun. Once the connection is built.DriverManager. Using this method we establish a connection between url and database.System. password). } } ----------------------------------------------------------------------------------------------------------------------------.

out.out. con = DriverManager..In case there is an exception in try block. System. String pass = "root".println("Connection url : " + url + db).getConnection(url + db. during the establishment of connection between the front end in java application(url) and backend.java import java.. String url = "jdbc:mysql://localhost:3306/". JdbcConnection.jdbc. user.out. try { Class.close(). } } } .close ( ) : An object of connection is used to close the connection between database and url."). pass).mysql.println("jdbc driver for mysql : " + driver). con..Driver". } catch (Exception e) { System."). String driver = "com.. System.*.con. The catch block is responsible for handling the exception.println(e). Finally the println print the connection is closed. System. String user = "root".println("Connection is created.out. System. public class JdbcConnection { public static void main(String args[]) { Connection con = null.sql.out.forName(driver).println("Connection is closed. String db = "komal".

.33907.07 094330.89843839.48:-3.-.94384-9.4  84907.9.30/0.898438:-57494.-.9020398 4:300/94389.9 574.943 :7 43.0430472470.902039  89.9  3.9.94389/1107039/.943   <  70.4 W..:90/.9020398 .3/10/8 9.38 2094/894/0.0 0 70:8390/-.1479084:7.9020394-0..8:-57494.3..80 9.943.$9.$"89.7000..20W.-80/.3.3.4330.0 /013082094/81473907.0.943 /-4330.93.-..70.20 !.80 4330.3. $".917424:7. 0 31472.4..8:-57494.80  %7003/841$9.20 &#.250/-.9438  &#$39.90089.9.439.-08 .:90$"89.48:-3.9020394-0. /-.943 4-0.39.:90$"89..70709:730/ 3.7.9-:8390.943.902039/-4330.93990/..3/ 708:98.9434-0.84.4/-.9.90203989. /-.3.9.-. 4/-.9437.55.80 479./.943 %4 00.4330../-.02.902039894./7.-.07.07.9...902039 2094/  $9..0734894:80908:3 /-.$9.90.884 7/  < .4330.3/00.-4:990/.90$9.830 /.9020394-0.4330.943 .90$9..808 4330.997.94339071.9.94389.80.07.8:-57494.4330./08209.9020398.07  97 4330./-.8:-57494..4330.9990/.38.70.-.9./.98:80/94803/.05943   $8902 4:9 57393  4:/3 909.

9.:90 47.7 708:98  #08:9$0909....4250/866:070894794:95.70/$9.08894.209078  !705.90 00.. 708:9$09  $9..70.943 25479.70. %0309 2094/8:80/948:.90 .-0$9.4250/ $"89.7070970.:90570.$9.:90&5/.39071.9020394-0.902039 %0 9.-0$9.39.9.98...9020394-0.209078  $9.9.3/00.20 8:3 /-.88%0897.90 490$9..90203989. 4/-.9..70.0307.9020390.8089470/574.9020390.9020394-0.38.:90&5/.:90":07 00.902039 902094/94 :80800.9  !705.90203939071.90203989.8$9734-0.9879903.70$"89470/ 574./-.30!705.07 389./.9433    97    $973..-0 .9.:93.0/:70  ./08.$"89.9  #08:9$09574.9.94.:908250866:070894:95.4/2.:90 00.07  .07389..9020395705..9.250841 89.70/$9.7.30   5:-...-0$9.0/:70.04/831472.:90.9..90/1742904330.088.:94341$"89.:90&5/.3$" 89.897002094/814700.9020399.70/$9..#08:9$09 98.-.88.3/709:733.-0748..0/3806:03.3$"$9.9020394-0.$9.07389.9020394-0.-0.43897:.70$9.9020398 %0$9.904724/19.9020398 00.7000.3 $973(..98. 83  5943!.-:.0 #08:9$092.902039 $97386  709:738.:7847543939498.9434-0.9020398.70/$9.:90/9902094/00.902039. $97386  709:738.89.-0 47/745. 0.902039  70.70570.902039990$9.902039..-0$9..9 ..9020398   0..70:80/943907.7.3/5745079084190 .90.:77039 7441/..0/013082094/89.0   5:-.30.7.-041/.:90":07 4789.:93 89.89.4:2383.08905974:9074841909.:93.9..9 .70.70.9.9020390.88.9020394-0.9 !705.9..90 00.9020398  .3/.$%89.. /-.90/-00...90$9.907.943* 7.:908.0/7.99/.80.9020395705.9  %0897.-08 902094/94:808 00.94343909508. /-.9.78    $973:11074:95:930 $973:1107    4:95:9 .5503/ %089347.9.908.-.

30 84088.0/7..9.07 -0..07 25479./$.5503/ 7.433:   $8902 4:9 57393 494330.2504/0 .20 209..943.0    <  <  < 434.04330..07.943   1 .0881:      5943!.89.43 0909.4/-...05943  4330.9.943   .943 25479.4 3:  4:95:9   $8902 4:9 57393 ..9.433:  5:-.-4.07..4330.07. . 097.. 86 4330.4/2.$973:807/ 8.209.9 3     4:95:9 .499 5.9. 25479..07'07843    <080 .88 147..88/7.9.4330.0.8009.4309 7.20     4:95:9 ..0..$973:7 /-.5503/ 7.07389.887.-.07 /7.9.07 .0731472.88.5503/ 7..05943  5:-.3 $973(.20 .07389.20   $8902 4:9 57393 7.250 25479.30 84088. 86 7.3.07'07843 209.8847/ 907   89.250 7.9...943..9 .   ..9..-.059430     4:95:930$973:1107     4:95:9 .   89.. 097...4 3:  4:95:9    <.   $8902 4:9 57393 7.943 $:. 86 . 86 $".78 9748..8009.07 -0.943 3      5943!.9.943  89.077747   0 09088.9.943.

3.9.943 01470902502039.20  ...05943   0 09088.. /-.059430     $8902 077 5739 .0    <    97   .07    <.4330.88 147./7.943   <  <   5:-.. $".07 094330.8.0.3 .-.3.04330.9.4330..88 147..437.20 8:3 /-.943 4-0.43  <  <   47985747.3/-.-.89412094/706:70/94..07. .0.94341.88494:3/. 86  300/0/1473094739071.48090.7:2039  7....88.4330.3.4330..880300/9425479.-8.9438.9.88/-.4330..9 7.943-09003:7.9438 -:9 57393573990....70.059430     $8902 077 57393 $".943 :7 :807/  5.943 0.4330.94309 7..05943     $8902 077 57393 0 09088.90/ .3//.7.07.0-09003:7.3/.3/.. .  $8902 4:9 57393 4:/349094330...2.07 094330.8847/    <.9439/..05990/7..5.943-:9.3./3.9.943  %82094/709:7384:. 4/-.9 ..3.07 094330.4330.0    <    709:73.07.0.9.80  &83982094/0089.88494:3/..4330.80  3.07.03/  4.89.090.4/00.943     97    . /-.078/430-..4330.

943 .4330.-8203941.78(  4330.480  34-0.-.943-09003901743903/3.943 :7 .9.943 5:-.4330.30.286. 86  5:-..89..05943  /-. /:7390 089.9438...80.43 .941.8090708.  25479...943-09003/.48090.05943397-4..4330. 87085438-0147...9057393573990..-4..03/ %0.3/3900.943.4330.480/ 3.55..88/-..9.9438:80/94..4330.4330.9.4/2.3 $973.3/:7  3.433: $973:7/-.3/-..

.

4.489 ..

/7.88 147.42 286 /-.70.943 :7 /.943:7 :7 /.07  $8902 4:9 57393 4330.43 .20 /7.07147286 /7. 7.07.480/   <.3. .887449 97 .07 094330. $973/-42.9.90/   .88  $8902 4:9 57393 /-.07 $973:8077449 $9735.9438.9438.059430  $8902 4:9 57393 0  < < <  .. $973/7.07  . $8902 4:9 57393 4330.437.07.480  $8902 4:9 57393 4330.:807 5.

Sign up to vote on this title
UsefulNot useful