JDBC by Attaullah
JDBC by Attaullah
JDBC by Attaullah
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
i)
Jdbc_odbc
Java API
Native API
DB
ii)
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
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
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.
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 .
String SQL= Select * From Emp; //create Sql Statement and assigning to //SQL as a string
// 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