Java ¡Programming ¡ ¡ Unit ¡12 ¡
Working ¡with ¡Rela7onal ¡Databases ¡ using ¡JDBC ¡ ¡ ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
Java Programming Unit 12 Working with Rela7onal - - PowerPoint PPT Presentation
Java Programming Unit 12 Working with Rela7onal Databases using JDBC (c) Yakov Fain 2014 Rela7onal Database Management Systems RDBMS store
(c) ¡Yakov ¡Fain ¡2014 ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
RDBMS ¡store ¡data ¡in ¡tables ¡as ¡rows ¡and ¡columns. ¡ ¡ One ¡row ¡is ¡one ¡record ¡(e.g. ¡Students, ¡Courses, ¡Customers, ¡Orders). ¡ ¡ A ¡column ¡represents ¡a ¡property ¡of ¡the ¡record ¡(e.g. ¡name, ¡address, ¡price). ¡
row ¡ columns ¡ table ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
where Students.ID = Takes_Course.ID and Name=“Matt”
where Students.ID = Takes_Course.ID and Takes_Course.classid = Courses.classid
(c) ¡Yakov ¡Fain ¡2014 ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
hZp://db.apache.org/derby/derby_downloads.html ¡ ¡ ¡ ¡ Unzip ¡it ¡in ¡any ¡directory ¡and ¡set ¡DERBY_HOME ¡and ¡PATH ¡variables ¡ according ¡to ¡instruc7ons ¡at ¡hZp://bit.ly/11pe06H ¡ ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
to ¡be ¡con7nued ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
to ¡be ¡con7nued ¡
¡
CREATE TABLE Employee ( EMPNO int NOT NULL, ENAME varchar (50)
NOT NULL, JOB_TITLE varchar (150) NOT NULL ); ¡
¡
INSERT INTO Employee values (7369,'John Smith', 'Clerk'), (7499,'Joe Allen','Salesman'), (7521,'Mary Lou','Director'); ¡
¡
Select * from Employee;
(c) ¡Yakov ¡Fain ¡2014 ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
For ¡example, ¡load ¡Oracle ¡Type ¡4 ¡JDBC ¡driver: ¡ ¡ ¡ ¡ ¡ ¡Class.forName(“oracle.jdbc.driver.OracleDriver”); In ¡the ¡case ¡of ¡JavaDB, ¡which ¡was ¡installed ¡and ¡registered ¡with ¡your ¡JDK, ¡you ¡can ¡skip ¡this ¡step. ¡
¡ ¡ ¡ ¡ ¡DriverManager.getConnection(url, user, password); In ¡the ¡case ¡of ¡Derby ¡DB ¡you ¡don’t ¡have ¡to ¡supply ¡the ¡user ¡and ¡the ¡password; ¡ ¡ ¡ ¡ ¡ ¡DriverManager.getConnection(“jdbc:derby:Lesson22”);
Connection.createStatement(); As ¡an ¡alterna7ve, ¡you ¡can ¡create ¡PreparedStatement ¡or ¡CallableStatement. ¡ ¡ ¡
Statement.executeQuery(“Select …”); ¡
¡ ¡ ¡ while (ResultSet.next()) {…} ¡ ¡
¡
(c) ¡Yakov ¡Fain ¡2014 ¡ class ¡EmployeeList ¡{ ¡ public ¡sta7c ¡void ¡main(String ¡argv[]) ¡{ ¡ ¡ ¡ ¡ ¡Connec7on ¡conn=null; ¡ ¡ ¡ ¡Statement ¡stmt=null; ¡ ¡ ¡ ¡ResultSet ¡rs=null; ¡ ¡ ¡ ¡try ¡{ ¡ ¡ ¡ ¡ ¡ ¡// ¡Load ¡the ¡JDBC ¡driver ¡-‑ ¡Class ¡ ¡ ¡ ¡ ¡ ¡// ¡This ¡can ¡be ¡skipped ¡for ¡Derby, ¡but ¡derbyclient.jar ¡ ¡ ¡ ¡ ¡ ¡//has ¡to ¡be ¡in ¡the ¡CLASSPATH ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Class.forName("org.apache.derby.jdbc.ClientDriver"); ¡ ¡ ¡ ¡ ¡ ¡conn ¡= ¡DriverManager.getConnec7on( ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡"jdbc:derby://localhost:1527/Lesson22"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Create ¡an ¡SQL ¡query ¡ ¡ ¡ ¡ ¡ ¡String ¡sqlQuery ¡= ¡"SELECT ¡* ¡from ¡Employee"; ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Create ¡an ¡instance ¡of ¡the ¡Statement ¡object ¡ ¡ ¡ ¡ ¡stmt ¡= ¡conn.createStatement(); ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Execute ¡SQL ¡and ¡get ¡obtain ¡the ¡ResultSet ¡object ¡ ¡ ¡ ¡ ¡rs ¡= ¡stmt.executeQuery(sqlQuery); ¡ ¡ ¡ ¡ // ¡Process ¡the ¡result ¡set ¡-‑ ¡print ¡Employees ¡ ¡ ¡ ¡ ¡while ¡(rs.next()){ ¡ ¡ ¡ ¡ ¡ ¡int ¡empNo ¡= ¡rs.getInt("EMPNO"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡eName ¡= ¡rs.getString("ENAME"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡job ¡= ¡rs.getString("JOB_TITLE"); ¡ ¡ System.out.println(""+ ¡empNo ¡+ ¡", ¡" ¡+ ¡eName ¡+ ¡", ¡" ¡+ ¡job ¡); ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡catch( ¡SQLExcep7on ¡se ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println ¡("SQLError: ¡" ¡+ ¡se.getMessage ¡() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡+ ¡" ¡code: ¡" ¡+ ¡se.getErrorCode()); ¡ ¡ ¡ ¡} ¡catch( ¡Excep7on ¡e ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println(e.getMessage()); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡e.printStackTrace(); ¡ ¡ ¡ ¡ ¡} ¡finally{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡clean ¡up ¡the ¡system ¡resources ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡try{ ¡ rs.close(); ¡ ¡ ¡ ¡ ¡ ¡ stmt.close(); ¡ ¡ conn.close(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡catch(Excep7on ¡e){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡e.printStackTrace(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ ¡} ¡ } ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
¡ ¡ ¡ ¡ ¡ ¡7369, ¡John ¡Smith, ¡Clerk ¡ ¡ ¡ ¡ ¡ ¡ ¡7499, ¡Joe ¡Allen, ¡Salesman ¡ ¡ ¡ ¡ ¡ ¡ ¡7521, ¡Mary ¡Lou, ¡Director ¡
Derby ¡may ¡have ¡already ¡booted ¡the ¡database”, ¡ ¡exit ¡the ¡ij ¡u7lity ¡with ¡exit; ¡command. ¡ ¡ You ¡can ¡read ¡more ¡about ¡specifics ¡of ¡running ¡embedded ¡Derby ¡DB ¡instance ¡at ¡ hZp://db.apache.org/derby/papers/DerbyTut/embedded_intro.html ¡ ¡ ¡ ¡ ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
With ¡PreparedStatement ¡you ¡can ¡create ¡SQL ¡with ¡parameters ¡that ¡are ¡ ¡ dynamically ¡passed ¡from ¡the ¡Java ¡program ¡to ¡DBMS. ¡ ¡ ¡ Suppose ¡you ¡need ¡to ¡execute ¡the ¡query ¡"SELECT ¡* ¡from ¡EMP ¡WHERE ¡empno=…" ¡ ¡ mul7ple ¡7mes ¡for ¡each ¡employee ¡from ¡the ¡array ¡empNumbers[]. ¡ ¡
// Using PreparedStatement PreparedStatement stmt=conn.prepareStatement( ” SELECT * from Employee WHERE empno=?”);
stmt.setInt(1,employees[i];) stmt.executeQuery(sqlQuery); } // Using Statement stmt = conn.createStatement(); for (int i=0;i<empNumbers.length; i++){ sqlQuery=”SELECT * from Employee WHERE empno=” + employees[i]; stmt.executeQuery(sqlQuery); }
ß ¡SQL ¡gets ¡compiled ¡ ¡once ¡ ß SQL ¡gets ¡compiled ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡each ¡employee ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
try{ conn.setAutoCommit(false);
"values(123, ‘Buy’,’IBM’,200)”); stmt.addBatch("insert into OrderDetail " + "values('JSmith', ‘Broker131’, ‘05/20/02’)"); stmt.executeBatch();
conn.rollback(); // Transaction failed e.printStackTrace(); }
¡ Transac7on ¡ ¡can ¡consist ¡of ¡several ¡ac7ons, ¡and ¡all ¡of ¡them ¡must ¡either ¡finish ¡successfully ¡or ¡ be ¡undone. ¡ ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
(c) ¡Yakov ¡Fain ¡2014 ¡
1. Download ¡and ¡install ¡DBMS ¡Oracle ¡11g ¡Express ¡edi7on ¡ ¡from ¡ ¡ hZp://bit.ly/Qmkzpt ¡. ¡During ¡the ¡installa7on ¡enter ¡(and ¡memorize) ¡the ¡ passwords ¡for ¡SYS ¡and ¡SYSTEM ¡accounts. ¡ ¡
¡
JDK ¡is ¡installed, ¡e.g ¡C:\Program ¡Files\Java\jdk1.7.0_55. ¡ ¡
hZps://www.youtube.com/watch?v=ZINT9tCl20g. ¡ ¡ ¡ ¡
¡