Java Database Connectivity INTRODUCTION Dr. Syed Imtiyaz Hassan Assistant Professor, Deptt. of CSE, Jamia Hamdard (Deemed to be University), New Delhi, India. s.imtiyaz@jamiahamdard.ac.in
Agenda Introduction Product Components Architecture Drivers Processing SQL Summary Refrences
Introduction Java API for database-independent connectivity between the Java programming language and a wide range of databases
Introduction Helps to write Java applications that manage these three programming activities: Connect to a data source, like a database Send queries and update statements to the database Retrieve and process the results received from the database in answer to your query
Product Components The JDBC API java.sql javax.sql JDBC Driver Manager DataSource JDBC Test Suite JDBC-ODBC Bridge
Architecture 2-tier 3-tier N-tier
Architecture 2-tier SRC: https://docs.oracle.com/javase/tutorial/jdbc/overview/index.html
Architecture 2-tier (cont.) Client connects directly to server e.g. HTTP, email Pro: simple Con: fat client inflexible
Architecture 3-tier SRC: https://docs.oracle.com/javase/tutorial/jdbc/overview/index.html
Pros Architecture 3-tier (cont.) Flexible Specialization: presentation / business logic / data management Can cache queries Can implement proxies and firewalls Cons Higher complexity Higher maintenance Lower network efficiency More parts to configure
Architecture N-tier N-tier data applications are data applications that are separated into multiple tiers Presentation Tier/Middle Tier/Data Tier Adapted from: https://msdn.microsoft.com/en-us/library/bb384398.aspx
Drivers Type 1: JDBC-ODBC Bridge Driver Type 2: JDBC-Native API SRC: https://www.tutorialspoint.com/jdbc/jdbc-driver-types.htm
Driver (cont.) Type 3: JDBC-Net pure Java (Network Protocol) Type 4: 100% pure Java (Native-protocol) SRC: https://www.tutorialspoint.com/jdbc/jdbc-driver-types.htm
Video
Simplicity Simplicity is the ultimate sophistication. Leonardo da Vinci Simplest explanation is the right explanation. Occam's razor Everything must be made as simple as possible, but not simpler. Albert Einstein
Processing SQL 1. Establishing a connection. 2. Create a statement. 3. Execute the query. 4. Process the Result. 5. Close the connection.
Processing SQL (cont.) Establish Connection Create Statement Execute Query Process Result Close Connection
Processing SQL Establish Connection Establish Connection Datasource Create Statement DriverManager Register the driver Execute Query Connect to the database Process Result Close Connection
Processing SQL Establish Connection (cont.) Register Class.forName(drivername) Connect DriverManager.getConnection(URL) jdbc:<subprotocol>:<subname> Protocol Subprotocol Database identifier jdbc:mysql://localhost:3306/students
Processing SQL Establish Connection (cont.) class.forname( com.mysql.jdbc.driver ); Connection con=drivermanager.getconnection( jdbc:mysql://localhost:3306/students, root, passwd );
Processing SQL Establish Connection (cont.) import java.sql.*; class Select { public static void main( String argv[] ) { try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/students"; Connection con = DriverManager.getConnection(url, "root","password"); } }...... } catch (Exception e) { e.printstacktrace(); }
Processing SQL Create Statement Establish Connection Create Statement Statement stmt = con.createstatement(); Execute Query Process Result Close Connection
Processing SQL Execute Query Establish Connection Create Statement executequery() Execute Query ExecuteUpdate() Process Result ResultSet rs = stmt.executequery(statement); int count = stmt.executeupdate(statement); Close Connection
Processing SQL Execute Query (cont.) Select statement ResultSet rset = stmt.executequery ("select *from BTCSE"); Delete statement int rowcount = stmt.executeupdate ("delete from BTCSE where student_section = A );
Processing SQL Execute Query (cont.) import java.sql.*; class Select { public static void main( String argv[] ) { try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/students"; Connection con = DriverManager.getConnection(url, "root","password");. Statement stmt=con.createstatement(); String sql = "select * from BTCSE"; ResultSet rs=stmt.executequery(sql);... } catch (Exception e) { e.printstacktrace(); } } }
Processing SQL Process Result Establish Connection Create Statement Execute Query ResultSet Process Result Others Close Connection
Processing SQL Process Result ResultSet 1. Step through the ResultSet. while (rs.next()) { } 2. Use getxxx() to get each column value. String val = rs.getstring(colname); String val = rs.getstring(colindex); while (rset.next()) { String enrollment = rset.getstring( enrollment_number"); String stdname = rset.getstring(2); // Process or display the data }
Processing SQL Process Result (cont.) import java.sql.*; class Select { public static void main( String argv[] ) { try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/students"; Connection con = DriverManager.getConnection(url, "root","password"); Statement stmt=con.createstatement(); String sql = "select * from students"; ResultSet rs=stmt.executequery(sql); while ( rs.next() ) System.out.println(rs.getString( 1 )+","+rs.getstring( 2 ));. } catch (Exception e) { e.printstacktrace(); } } }
Processing SQL Close Connection Establish Connection Create Statement Execute Query Close the ResultSet Process Result Close the Statement Close Connection Close the Connection
Processing SQL Close Connection (cont.) 1. Close the ResultSet object. rs.close(); 2. Close the Statement object. stmt.close(); 3. Close the connection. con.close();
Program Example package advancedjava.jdbc; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.statement; public class SelectRecords { public static void main(string argv[]) { Connection con = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/students"; con = DriverManager.getConnection(url, "root", "password"); String sql = "select * from btcse"; stmt = con.createstatement(); rs = stmt.executequery(sql); System.out.println("Enrollment\tName\tSection"); while (rs.next()) { System.out.println(rs.getString(1) + "\t" + rs.getstring(2) + "\t" + rs.getint(3)); } rs.close(); stmt.close(); con.close(); } catch (Exception e) { System.out.println("** Error on data select. ** " + e); } } }
Summary
Reference JDBC Database Access https://docs.oracle.com/javase/tutorial/jdbc/index.html
Thank You Questions???