JSP&DB Programming By:  Mr. PHUPHA PUNYAPOTASAKUL ( ภูผา ปัญญาโพธาสกุล )
Architecture
JDBC <ul><li>Connect to a data source, like a database  </li></ul><ul><li>Send queries and update statements to the databa...
Establish connection <ul><li>Initialize driver Class . forName ( driver ) ; </li></ul><ul><li>Create Connection Connection...
Driver class name and URL example <ul><li>Oracle 10i </li></ul><ul><li>driver = oracle . jdbc . driver . OracleDriver </li...
Create statement and execute SQL command <ul><li>Create statement Statement stmt  = con.createStatement( ResultSet.TYPE_SC...
ResultSet type <ul><li>TYPE_FORWARD_ONLY  — The result set is not scrollable; its cursor moves forward only, from before t...
Concurent Type <ul><li>CONCUR_READ_ONLY </li></ul><ul><li>CONCUR_UPDATABLE   </li></ul>
ResultSet movement <ul><li>next ()  - moves the cursor forward one row. Returns true if the cursor is now positioned on a ...
ResultSet movement <ul><li>beforeFirst()  -  positions the cursor at the start of the  ResultSet  object, before the first...
Retrieving data <ul><li>Data type conversion </li></ul><ul><ul><li>getString() </li></ul></ul><ul><ul><li>getInt() </li></...
Updating data <ul><li>Only if using CONCUR_UPDATABLE </li></ul><ul><li>Not all JDBC Driver support this mode </li></ul><ul...
Updating data <ul><li>To insert a new record rs.moveToInsertRow() rs.updateString(1,&quot;XXX&quot;); rs.insertRow(); </li...
Exception handling <ul><li>Connection conn = null; </li></ul><ul><li>Statement stmt = null; </li></ul><ul><li>ResultSet rs...
Prepared Statement <ul><li>Create Statement PreparedStatement pstmt  =  conn . prepareStatement ( sql,rtype,concur ) ; </l...
Prepared Statement <ul><li>Use parameter index only - can't use column name (start from 1) pstmt.setString(1,&quot;XXX&quo...
Question & Answer
Upcoming SlideShare
Loading in...5
×

KMUTNB - Internet Programming 6/7

788

Published on

Lecture for King Mongkut's University of Technology North Bangkok (KMUTNB) / Computer Science / Internet Programming Course by PHUPHA

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
788
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
33
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

KMUTNB - Internet Programming 6/7

  1. 1. JSP&DB Programming By: Mr. PHUPHA PUNYAPOTASAKUL ( ภูผา ปัญญาโพธาสกุล )
  2. 2. Architecture
  3. 3. JDBC <ul><li>Connect to a data source, like a database </li></ul><ul><li>Send queries and update statements to the database </li></ul><ul><li>Retrieve and process the results received from the database in answer to your query </li></ul>
  4. 4. Establish connection <ul><li>Initialize driver Class . forName ( driver ) ; </li></ul><ul><li>Create Connection Connection conn = DriverManager . getConnection ( url,user,password ) ; </li></ul><ul><li>Each JDBC Vendor may use different driver class name and url </li></ul>
  5. 5. Driver class name and URL example <ul><li>Oracle 10i </li></ul><ul><li>driver = oracle . jdbc . driver . OracleDriver </li></ul><ul><li>url = jdbc : oracle : thin : @ // {DBSERVER} : 1521 / {DBNAME} </li></ul><ul><li>MySQL 4 . x, 5 . x </li></ul><ul><li>driver = com . mysql . jdbc . Driver </li></ul><ul><li>url = jdbc : mysql :// {DBSERVER} / {DBNAME} </li></ul><ul><li>MS SQLServer 2000, 2005 </li></ul><ul><li>driver = com . microsoft . sqlserver . jdbc . SQLServerDriver </li></ul><ul><li>url = jdbc : sqlserver :// {DBSERVER} : 1433;databaseName = {DBNAME};integratedSecurity = false; </li></ul>
  6. 6. Create statement and execute SQL command <ul><li>Create statement Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY ) ; </li></ul><ul><li>Execute Update stmt.execut eUpdate ( sql ); </li></ul><ul><li>Execute Query ResultSet rs = stmt . executeQuery ( sql ) ; </li></ul>
  7. 7. ResultSet type <ul><li>TYPE_FORWARD_ONLY — The result set is not scrollable; its cursor moves forward only, from before the first row to after the last row. The rows contained in the result set depend on how the underlying database materializes the results. That is, it contains the rows that satisfy the query at either the time the query is executed or as the rows are retrieved. </li></ul><ul><li>TYPE_SCROLL_INSENSITIVE — The result set is scrollable; its cursor can move both forward and backward relative to the current position, and it can move to an absolute position . </li></ul><ul><li>TYPE_SCROLL_SENSITIVE — The result set is scrollable; its cursor can move both forward and backward relative to the current position, and it can move to an absolute position. </li></ul>
  8. 8. Concurent Type <ul><li>CONCUR_READ_ONLY </li></ul><ul><li>CONCUR_UPDATABLE </li></ul>
  9. 9. ResultSet movement <ul><li>next () - moves the cursor forward one row. Returns true if the cursor is now positioned on a row and false if the cursor is positioned after the last row. </li></ul><ul><li>previous () - moves the cursor backwards one row. Returns true if the cursor is now positioned on a row and false if the cursor is positioned before the first row. </li></ul><ul><li>first () - moves the cursor to the first row in the ResultSet object. Returns true if the cursor is now positioned on the first row and false if the ResultSet object does not contain any rows. </li></ul><ul><li>last() - moves the cursor to the last row in the ResultSet object . Returns true if the cursor is now positioned on the last row and false if the ResultSet object does not contain any rows . </li></ul>
  10. 10. ResultSet movement <ul><li>beforeFirst() - positions the cursor at the start of the ResultSet object, before the first row . If the ResultSet object does not contain any rows, this method has no effect . </li></ul><ul><li>afterLast() - positions the cursor at the end of the ResultSet object, after the last row . If the ResultSet object does not contain any rows, this method has no effect . </li></ul><ul><li>relative(int rows) - moves the cursor relative to its current position . </li></ul><ul><li>absolute(int row) - positions the cursor on the row - th row of the ResultSet object . </li></ul>
  11. 11. Retrieving data <ul><li>Data type conversion </li></ul><ul><ul><li>getString() </li></ul></ul><ul><ul><li>getInt() </li></ul></ul><ul><ul><li>getFloat() </li></ul></ul><ul><ul><li>getDate() </li></ul></ul><ul><ul><li>etc. </li></ul></ul><ul><li>2 ways to retrieve data e.g. select product_name from product </li></ul><ul><ul><li>By column name getString(&quot;product_name&quot;); </li></ul></ul><ul><ul><li>By column index (start from 1) getString(1); </li></ul></ul>
  12. 12. Updating data <ul><li>Only if using CONCUR_UPDATABLE </li></ul><ul><li>Not all JDBC Driver support this mode </li></ul><ul><li>Data type conversion </li></ul><ul><ul><li>upateString() </li></ul></ul><ul><ul><li>updateInt() </li></ul></ul><ul><ul><li>updateFloat() </li></ul></ul><ul><ul><li>updateDate() </li></ul></ul><ul><ul><li>etc. </li></ul></ul><ul><li>Be able to use both column name and index </li></ul>
  13. 13. Updating data <ul><li>To insert a new record rs.moveToInsertRow() rs.updateString(1,&quot;XXX&quot;); rs.insertRow(); </li></ul><ul><li>To update a record //move to expected record rs.updateString(1,&quot;XXX&quot;); rs.updateRow(); </li></ul><ul><li>To delete a record //move to expected record rs.deleteRow(); </li></ul>
  14. 14. Exception handling <ul><li>Connection conn = null; </li></ul><ul><li>Statement stmt = null; </li></ul><ul><li>ResultSet rs = null; </li></ul><ul><li>try{ </li></ul><ul><li>... </li></ul><ul><li>}catch ( SQLException ex ) { </li></ul><ul><li>String sqlErr =&quot;&quot; ; </li></ul><ul><li>while ( ex != null ) { </li></ul><ul><li>sqlErr = sqlErr + &quot; Message : &quot; + ex . getMessage () ; </li></ul><ul><li>sqlErr = sqlErr + &quot; SQLState : &quot; + ex . getSQLState () ; </li></ul><ul><li>sqlErr = sqlErr + &quot; ErrorCode : &quot; + ex . getErrorCode () ; </li></ul><ul><li>sqlErr = sqlErr + &quot; -----------------------------------------------------------&quot; ; </li></ul><ul><li>ex = ex . getNextException () ; </li></ul><ul><li>} </li></ul><ul><li>System . out . println ( sqlErr ) ; </li></ul><ul><li>}catch ( Exception ex ) { </li></ul><ul><li>ex . printStackTrace () ; </li></ul><ul><li>}finally{ </li></ul><ul><li>try{ if ( rs != null ) rs . close () ; }catch ( Exception e ) {} </li></ul><ul><li>try{ if ( stmt != null ) stmt . close () ; }catch ( Exception e ) {} </li></ul><ul><li>try{ if ( conn != null ) conn . close () ; }catch ( Exception e ) {} </li></ul><ul><li>} </li></ul>
  15. 15. Prepared Statement <ul><li>Create Statement PreparedStatement pstmt = conn . prepareStatement ( sql,rtype,concur ) ; </li></ul><ul><li>SQL with parameter using &quot;?&quot; E.g. select * from product where product_id=? and is_enable=? </li></ul><ul><li>Set parameter value </li></ul><ul><ul><li>setString() </li></ul></ul><ul><ul><li>setFloat() </li></ul></ul><ul><ul><li>setInt() </li></ul></ul><ul><ul><li>setDate() </li></ul></ul>
  16. 16. Prepared Statement <ul><li>Use parameter index only - can't use column name (start from 1) pstmt.setString(1,&quot;XXX&quot;); </li></ul><ul><li>Execute query or update normally ResultSet rs=pstmt.executeQuery(); pstmt.executeUpdate(); </li></ul><ul><li>Re-usable </li></ul>
  17. 17. Question & Answer
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×