Sie sind auf Seite 1von 5

JDBC (Java Database Connectivity) Use for database connectivity It is not part of language but it provides connection to connect

ect Database to java language. To connect and communicate with java language to Database, Java provide package called java.sql consist of class and set of method to connect and communicate with java. o Import java.sql

Architecture of JDBC Driver Manger layer(Java Driver API) Connection layer (Java API) Application Layer 1) Driver Manger layer If we want to communicate with Database form given language there should be some intermediate between Database and language this is nothing but it is driver you need to load driver. o Loading driver is occur in Driver Manger Layer Once driver is loaded then you communicate to driver and driver communicates with Database. 2) Connection layer When you communicate with Database you must have user name and password, then connection is possible or get it. You need to connect first that occur in connection Layer, once you have a connection then you provide commands, and execute the command (e.g. statement) o You create your statement. o Execute you statement o Exception handling o Process result set These are steps of execution for JDBC program as well. 3) Application layer o It provide interface through you can communicate with Database it contain set of business rule that are define by programmer some time also called client application so it is occur in application layer. What Types of driver available and how it load it. i) ii) iii) iv) Jdbc_odbc driver or bridge Thick/native driver Net protocol driver Thin/ pure driver

Attaullah M.Sc Final 2011-13

i)

Jdbc_odbc

Java API

Native API

DB

ii)

Thick/ Native Driver

Native API

DB

iii)

Net protocol ( is also called 3 tire Architecture) Java Application (Client s/w) ----> server---->server using above 1 of above types, o Server using 1 of 3 drivers to talk with Database. o This is also called 3 tire architecture, o You use this in case a lot of concurrency and traffic Thin Driver or pure driver Java API

iv)

DB

Note: Mostly use type three Driver

Attaullah M.Sc Final 2011-13

To communicate with Database from java language Using following steps 1) Loading specific database Driver such MySQL, MS Access, oracle, sun 2) Create connection or get connection. 3) Create Statement (e.g. SQL queries). 4) Execute statement. 5) Preparing Result set for processing result. 6) Closing the connection Step 1: To load driver we use
class.forName (String);

forName is method in class that load driver class and return class object take argument as String in case of type 1 driver it would be like that
String driver = sun.jdbc.odbc.JdbcOdbcDriver

in case of type 2 or 4 driver it would be Let assume oracle Database


String driver = oracle.jdbc.driver.OracleDriver

Or Let assume MySQL Driver


String driver =com.mysql.jdbc.Driver

If you define variable like String then you provide dynamically the value to class You may provide static value like define all string in class.forName (); Step 2: Then you need to get a connection
Connection con = DriverManger.getConnection (url+dbName,usrName,Pswrd);

Where Connection is interface in java.sql package (note: all most all .class file in java.sql package is interface) con is object DriverManger is class
getConnection() is method of DriverManger that return an object of Connection.

Whereas url=jdbc:odbc://DNS:PortNumber/ or url= jdbc:oracle://DNS:PortNumber/


or url=jdbc:mysql://DNS:PortNumber/

Jdbc: is main protocol and odbc: or oracle: or mysql: is sub protocol DNS: your server name and port# it may be your localhost e.g. localhost:3306/ dbName is your Database Name or Data source Name (eg Employee) and usrName and Pswrd is your database username and password by default user name is root and password is none .

Attaullah M.Sc Final 2011-13

Step 3: Then you need to create statement for further process


Statement stmt; Stmt = con.createStatement(); //Declare stmt of Statement Interface //create for sql statement and con is //obj of Connection

String SQL= Select * From Emp; //create Sql Statement and assigning to //SQL as a string

Step 4: Execute the statement


ResultSet rs; // ResultSet is Interface in Java.sql while rs may be any variable it

//could be used in further process


rs = stmt.executeQuery (SQL); // for update we use stmt.executeUpdate();

Step 5: Preparing ResultSet and read record 1 by 1 or row by row


While(rs.next()){ // we just read and write 2 fields from table int id = rs.getInt(e_id); String name = rs.getString(e_name); System.out.println(id : +id+\t+name); }

Step 6: At the end, connection should be disconnected.


con.close();

Attaullah M.Sc Final 2011-13

// before using this u must create database attamsc then create table student then create field for that such as // std_id, std_name, std_cgpa import java.sql.*; public class simple { public static void main(String a[]) { Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "attamsc"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = ""; try { Class.forName(driver).newInstance(); con = DriverManager.getConnection(url+dbName,userName,password); Statement st = con.createStatement(); String sql ="select * from student"; ResultSet rs = st.executeQuery(sql); System.out.println("ID\tCGPA\tName"); while( rs.next() ) { int id = rs.getInt("std_id"); int gpa = rs.getInt("std_cgpa"); String name = rs.getString("std_name"); System.out.println(id+"\t"+gpa+"\t"+name); } st.close(); } catch( Exception e ) { System.out.println(e.getMessage()); e.printStackTrace(); } } } Output ID 1 2 3 4 5

CGPA 4 3 3 4 3

Name Attaullah Awais Khan Mutahir Ikram Ullah Haroon

Attaullah M.Sc Final 2011-13

Das könnte Ihnen auch gefallen