?"; PreparedStatement pstmt = con.prepareStatement(queryStr); pstmt.setString(1, " "); pstmt.setInt(2, 26000); ResultSet rs = pstmt.executeQuery();"> ?"; PreparedStatement pstmt = con.prepareStatement(queryStr); pstmt.setString(1, " "); pstmt.setInt(2, 26000); ResultSet rs = pstmt.executeQuery();">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

JDBC – Java Database Connectivity

Similar presentations


Presentation on theme: "JDBC – Java Database Connectivity"— Presentation transcript:

1 JDBC – Java Database Connectivity

2 Querying with PreparedStatement
String queryStr = "SELECT * FROM employee " + "WHERE superssn= ? and salary > ?"; PreparedStatement pstmt = con.prepareStatement(queryStr); pstmt.setString(1, " "); pstmt.setInt(2, 26000); ResultSet rs = pstmt.executeQuery();

3 Statements vs. PreparedStatements: Be Careful!
Are these the same? What do they do? String val = "abc"; PreparedStatement pstmt = con.prepareStatement("select * from R where A=?"); pstmt.setString(1, val); ResultSet rs = pstmt.executeQuery(); String val = "abc"; Statement stmt = con.createStatement( ); ResultSet rs = stmt.executeQuery("select * from R where A=" + val);

4 ResultSet ResultSet objects provide access to the tables generated as results of executing a Statement queries Only one ResultSet per Statement can be open at the same time! The table rows are retrieved in sequence A ResultSet maintains a cursor pointing to its current row The next() method moves the cursor to the next row

5 executeQuery() no assumption is made on the validity of the query
ResultSet executeQuery(String sql) Executes the given SQL statement, which returns a single ResultSet object no assumption is made on the validity of the query if the SQL execute successfully it returns a ResultSet object containing rows from the database if the SQL fails it will raise a SQLException

6 Executing a Statement - Example
ResultSet rs = stmt.executeQuery(“select name from pets”); ResultSet: Initial cursor position Fluffy Claws Buffy Fang Chirpy Whistler Slim Puffball next() next()

7 ResultSet Object A table of data representing a database result set
maintains a cursor pointing to its current row of data Initially the cursor is positioned before the first row The next() method moves the cursor to the next row next() returns false when there are no more rows in the ResultSet object A default ResultSet object is not updatable and has a cursor that moves forward only table of data representing a database result set, which is usually generated by executing a statement that queries the database. A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable.

8 Moving Through the ResultSet -Example
while (rs.next()) { System.out.println(rs.getString((1))); } Fluffy Claws Buffy Fang Chirpy Whistler Slim Puffball

9 Basic Getter Methods int getInt(int columnIndex)
int getInt(String columnName) String getString(int columnIndex) String getString(String columnName) Date getDate(int columnIndex) Date getDate(String columnName)

10 ResultSet Methods boolean next() void close() activates the next row
the first call to next() activates the first row returns false if there are no more rows void close() disposes of the ResultSet allows you to re-use the Statement that created it automatically called by most Statement methods

11 ResultSet Methods Type getType(int columnIndex) returns the given field as the given type indices start at 1 and not 0! Type getType(String columnName) same, but uses name of field less efficient For example: getString(columnIndex), getInt(columnName), getTime, getBoolean, getType,... int findColumn(String columnName) looks up column index given column name

12 ResultSet Methods JDBC 2.0 includes scrollable result sets. Additional methods included are : ‘first’, ‘last’, ‘previous’, and other methods.

13 ResultSet Example Statement stmt = con.createStatement();
ResultSet rs = stmt. executeQuery("select lname,salary from Employees");     // Print the result while(rs.next()) {  System.out.print(rs.getString(1) + ":");  System.out.println(rs.getDouble(“salary")); }

14 Mapping Java Types to SQL Types
SQL type Java Type CHAR, VARCHAR, LONGVARCHAR String NUMERIC, DECIMAL java.math.BigDecimal BIT boolean TINYINT byte SMALLINT short INTEGER int BIGINT long REAL float FLOAT, DOUBLE double BINARY, VARBINARY, LONGVARBINARY byte[] DATE java.sql.Date TIME java.sql.Time TIMESTAMP java.sql.Timestamp

15 Cleaning Up After Yourself
Remember to close the Connections, Statements, Prepared Statements and Result Sets con.close(); stmt.close(); pstmt.close(); rs.close()

16 DEMO


Download ppt "JDBC – Java Database Connectivity"

Similar presentations


Ads by Google