Docsity

Database Application Development using JDBC: A Step-by-Step Guide - Prof. Kristen R. Lefev, Study notes of Database Management Systems (DBMS)

Partial preview of the text

Download Database Application Development using JDBC: A Step-by-Step Guide - Prof. Kristen R. Lefev and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

Database Application Developmentusing JDBC

EECS 484^ Winter 09

Overview • Steps in an Application interacting with adatabase system – Databases accept queries, return^ lists

of results

-^ Applications must^ •^ Create queries^ •^ Submit them to the DB^ •^ Process the results (usually a list)^ •^ Handle errors

JDBC Driver Management • All drivers are managed by the DriverManager^

class

•^ Loading a JDBC driver:^ –^ In the Java code:^ Class.forName("oracle.jdbc.driver.OracleDriver");^ –^ When starting the Java application:^ - Djdbc.drivers=oracle/jdbc.driver

Connections in JDBC • We interact with a data source through sessions. Eachconnection identifies a logical session. • JDBC URL:jdbc:: Example: String url="jdbc:oracle:thin:@db8.engin.umich.edu:1521:muscle"; Connection con; try{ con = DriverManager.getConnection(url,userId,password); } catch (SQLException e) {…}

Multiple queries of the same format^ String query = "SELECT name FROM Sailors where rating = 0.1";^ ResultSet rs = stmt.executeQuery(query);^ …^ …^ …^ query = “SELECT name FROM Sailors where rating = 0.2”;^ ResultSet rs = stmt.executeQuery(query);

How is the query processed? • Application creates query • Application sends query (delay) • DBMS receives query • Compiles and optimizes the query (delay) • Computes the results • Results must be marshalled into a list by a driver

Use PreparedStatement instead(precompiled) String query = "SELECT name FROM Sailors where rating = ?"; PreparedStatement st = conn.prepareStatement(query); // precompile st.setFloat(1, 0.1); ResultSet rs = st.executeQuery(); // process resultset rs for rating = 0.1 st.clearParameters(); st.setFloat(1, 0.2); ResultSet rs = st.executeQuery(); // process resultset rs for rating = 0.2 ... This is also called a^ dynamic SQL query.

Another example String sql=“INSERT^ INTO^ Sailors^ VALUES(?,?)”; PreparedStatment^ pstmt=con.prepareStatement(sql); pstmt.clearParameters(); pstmt.setString(1,sname); pstmt.setFloat(2,^ rating); // we know that^ no^ rows^ are^ returned,

thus^ we^ use executeUpdate() int^ numRows^ =^ pstmt.executeUpdate();

ResultSets A ResultSet is a very powerful cursor: • previous(): moves one row back • absolute(int^ num):^ moves to the rowwith the specified number • relative(int^ num):^ moves forward orbackward • first() and last()

Matching Java and SQL Data Types

java.sql.TimeStamp^ getTimestamp()

TIMESTAMP

java.sql.Time^ getTime()

TIME

getDate()java.sql.Date

DATE

getFloat()Double

REAL

getInt()Integer

INTEGER

getDouble()Double

FLOAT

getDouble()Double

DOUBLE

getString()String

VARCHAR

String^ getString()

CHAR

Boolean^ getBoolean()

BIT

ResultSet get methodJava class

SQL Type

Commit/rollback structure: try { connection.setAutoCommit(false); //^ do^ queries^ (one^ or^ more) … connection.commit(); }^ catch^ (java.sql.SQLExeception

e)^ { connection.rollback(); }

IMPORTANT Change line in FileFinder.java: public static String url ="jdbc:oracle:thin:@db3.engin.umich.edu:1521:engin";