jdbc2.ppt
TRANSCRIPT
![Page 1: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/1.jpg)
JDBC
CS-328
![Page 2: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/2.jpg)
JDBC
• Java API for accessing RDBMS
• Allows use of SQL for RDBMS programming
• Can be used for:– embedded SQL– execution of stored queries
![Page 3: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/3.jpg)
SQL Statement Execution
• Four primary classes used to– load a driver
• (java.sql.DriverManager)
– connect to the DB • (java.sql.Connection)
– create a SQL statement• (java.sql.Statement)
– execute a SQL statement• (java.sql.ResultSet)
![Page 4: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/4.jpg)
Example
try {try {
Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);
String url = “jdbc:odbc:msaccessdb”;
Connection con = DriverManager.getConnection(url,””,””);
// create sql statement
String qs = “select * from loadtest”;
Statement stmt = con.createStatement();
// execute the statement
ResultSet rs = stmt.executeQuery(qs);
// process the result set
boolean more = rs.next;
while (more) {
System.out.println ............}}
![Page 5: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/5.jpg)
Driver Manager Object
• Loads the proper driver into your java object– multiple drivers may be loaded to allow
connections to multiple databases
• Provides a common interface to RDBMSs for JDBC Drivers
![Page 6: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/6.jpg)
Driver Manager Object
JAVA ApplicationJAVA Application
Driver ManagerDriver Manager
Driver ‘A’Driver ‘A’ Driver ‘B’Driver ‘B’
ConnectionConnection
StatementStatement
Result SetResult Set
ConnectionConnection
StatementStatement
Result SetResult Set
![Page 7: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/7.jpg)
Driver Manager Class Methods
• getConnection(String url)• getDriver• registerDriver• deregisterDriver• getDrivers• setLoginTimeout• getLoginTimeout• setLogStream• getLogStream
![Page 8: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/8.jpg)
Connection Object
• Establish link between the JAVA application and RDBMS
• allows application to select proper driver when it needs to
• uses the database URL to make the connection– jdbc:<subprotocol>:<subname)
• jdbc:odbc:Mydatabase
• jdbc:db2:157.287.43.11/Mydatabse
![Page 9: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/9.jpg)
Connection Class Methods
• createStatement
• prepareStatement
• prepareCall
• NativeSQL
• setAutoCommit
• GetAutoCommit
• commit
• rollback
• close
• isclosed
![Page 10: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/10.jpg)
Statement Object
• Wrapper of a string containing a SQL statement
• allows JDBC to decompose the SQL into a set of steps to present to the database via the driver
• the connection object forwards the statement object to the database to obtain a results set object
![Page 11: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/11.jpg)
Statement Class Methods
• executeQuery
• executeUpdate– insert, update, delete
• close
• getResultSet
![Page 12: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/12.jpg)
ResultSet Object
• A container for the rows and columns (a table) acquired as a result of presenting a statement object to the RDBMs using the “executeQuery” statement method
![Page 13: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/13.jpg)
ResultSet Class Methods
• next
• close
• wasNull
• getString
• getBoolean
• getByte
• getShort
![Page 14: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/14.jpg)
ResultSet Class Methods(cont.)
• getInt
• getLong
• getFloat
• getDouble
• getNumeric
• getBytes
• getDate
![Page 15: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/15.jpg)
ResultSet Class Methods (cont.)
• getTime
• getTimeStamp
• getAsciiStream
• getUnicodeStream
• getBinaryStream
• getMetaData
• etc...
![Page 16: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/16.jpg)
JDBC Type Mapping
• Char - String• Varchar - String• Longvarchar - String• Numeric -
java.sql.Numeric• Decimal -
java.sql.Numeric• Bit - boolean
• Tinyint - byte• Smallint = short• Integer - int• Bigint - long• Real - float• Float - double• Double - double• Binary - byte[ ]
![Page 17: jdbc2.ppt](https://reader036.vdocuments.pub/reader036/viewer/2022082512/554a30e9b4c90520578b518c/html5/thumbnails/17.jpg)
JDBC Type Mapping (cont.)
• Varbinary - byte[ ]• Longvarbinary - byte[ ]• Date - java.sql.Date• Time - java.sql.Time• Timestamp -
java.sql.Timestamp