Documente Academic
Documente Profesional
Documente Cultură
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.
The return type of the Class.forName (String ClassName) method is Class. Class
is a class in java.lang package.
1
2
3
4
5
6
try {
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); //Or any other driver
}
catch(Exception x){
System.out.println( Unable to load the driver class! );
}
try{
Connection dbConnection=DriverManager.getConnection(url,loginName,Password)
}
catch( SQLException x ){
System.out.println( Couldnt get connection! );
}
import javax.swing.JOptionPane;
public class TestJDBCDriverInstallation_Oracle {
public static void main(String[] args) {
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import
import
import
import
java.sql.Connection;
java.sql.DatabaseMetaData;
java.sql.DriverManager;
java.sql.SQLException;
return con;
34
}
JDBC drivers are divided into four types or levels. The different types of jdbc
drivers are:
Type 1: JDBC-ODBC Bridge driver (Bridge)
Type 2: Native-API/partly Java driver (Native)
Type 3: AllJava/Net-protocol driver (Middleware)
Type 4: All Java/Native-protocol driver (Pure)
4 types of jdbc drivers are elaborated in detail as shown below:
Type 1 JDBC Driver
JDBC-ODBC Bridge driver
The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the
ODBC driver. ODBC is a generic API. The JDBC-ODBC Bridge driver is
recommended only for experimental use or when no other alternative is available.
The JDBC-ODBC Bridge allows access to almost any database, since the
databases ODBC drivers are already available.
Disadvantages
1. Since the Bridge driver is not written fully in Java, Type 1 drivers are not portable.
2. A performance issue is seen as a JDBC call goes through the bridge to the ODBC
driver, then to the database, and this applies even in the reverse process. They are
the slowest of all driver types.
3. The client system requires the ODBC Installation to use the driver.
4. Not good for the Web.
Type 2 JDBC Driver
Native-protocol/all-Java driver
The Type 4 uses java networking libraries to communicate directly with the database
server.
3. You dont need to install special software on the client or server. Further, these
drivers can be downloaded dynamically.
Disadvantage
With type 4 drivers, the user needs a different driver for each database.
2.
3.
4.
5.
There are 5 steps to connect any java application with the database in java using JDBC.
They are as follows:
Register the driver class
Creating connection
Creating statement
Executing queries
Closing connection
1.
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
Statement stmt=con.createStatement();
1.
con.close();
1.
11
import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//step2 create the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
//step3 create the statement object
Statement stmt=con.createStatement();
//step4 execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
//step5 close the connection object
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
To connect java application with the Oracle database ojdbc14.jar file is required to be
loaded.
12
2) set classpath:
There are two ways to set the classpath:
temporary
permanent
Firstly, search the ojdbc14.jar file then open command prompt and write:
C:>set classpath=c:\folder\ojdbc14.jar;.;
13
4. Password: Password is given by the user at the time of installing the mysql
database. In this example, we are going to use root as the password.
Let's first create a table in the mysql database, but before creating table, we need to create
database first.
1.
2.
3.
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
The above example will fetch all the records of emp table.
To connect java application with the mysql database mysqlconnector.jar file is required to be
loaded.
14
2) set classpath:
There are two ways to set the classpath:
temporary
permament
import java.sql.*;
class Test{
15
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
import java.sql.*;
class Test{
public static void main(String ar[]){
try{
String url="jdbc:odbc:mydsn";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection(url);
Statement st=c.createStatement();
ResultSet rs=st.executeQuery("select * from login");
while(rs.next()){
System.out.println(rs.getString(1));
}
}catch(Exception ee){System.out.println(ee);}
}}
16