Академический Документы
Профессиональный Документы
Культура Документы
JDBC(Contd.)
IETE
Objectives:
Steps of JDBC Code. Statement. ResultSet.
Prepared Statement.
Callable Statement.
IETE
the driver.
2. Define the Connection URL 3. Establish the Connection 4. Create a Statement object 5. Execute a query 6. Process the results 7. Close the connection
IETE
{ Class.forName("com.mysql.jdbc.Driver"); DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); } catch (ClassNotFoundException cnfe) { System.out.println("Error loading driver: " cnfe);
IETE
Connection URL
Getting Connection
The purpose of the java.sql.DriverManger class in JDBC is to provide a common access layer on top of different database drivers used in an application.
IETE
IETE
Example:
String query = select col1, col2 from mytable; ResultSet rs = st.executeQuery(query);
Execute SQL Query
IETE
IETE
column indices
IETE
yes
More results ? no closeStatment closeConnection
import java.sql.*; public class jdbctest { public static void main(String args[]){ try{ Class.forName("org.postgresql.Driver"); Connection con = DriverManager.getConnection ("jdbc:postgresql://lsir-cispc8:5401/pcmdb", "user", "passwd"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery ("select name, number from pcmtable where number < 2"); while(rs.next()) System.out.println( rs.getString(1) + " (" + rs.getInt(2) + ")"); stmt.close() con.close(); } catch(Exception e){ System.err.println(e); IETE }}}
Example
ResultSet rs = statement.executeQuery("SELECT NAME, PRICE FROM PRODUCT"); while ( rs.next( ) ) { String name = rs.getString( "NAME" ); float price = rs.getFloat( "PRICE" ); System.out.println("Name: "+name+", price: "+price); }
IETE
Prepared Statement
The PreparedStatement interface, extending Statement, is used to execute a precompiled SQL statement with or without parameters. It is efficient for repeated execution. A PreparedStatement object is created using the preparedStatement method in the Connection interface. For example PreparedStatement pstmt = connection.prepareStatement ("insert into Student (firstName, mi, lastName) " + "values (?, ?, ?)");
IETE
Transactions
A transaction is a collection of DML statements that are executed as if they are a single operation. Transaction might need to be grouped in situations where multiple updates are needed and the entire set of transactions is to be committed or entire set undone in case of a single failure. Transaction service basically include beginning the transaction, executing the SQL statement that make up the transaction, and either perform a commit on overall success of each SQL statement or rollback the transaction as a whole if one of the SQL statements fails.
IETE
Transactions.
Transaction management in JDBC is handled using the Connection object. When a new connection is opened, the transaction autocommit mode is turn on. To execute multiple SQL statements as part of a single transaction, the auto-commit should be disabled as follows : connection.setAutoCommit(false); Auto-commit mode is turn off, an explicit COMMIT or ROLLBACK should be done to commit any unsaved database changes by commit() and rollback() methods of the connection object.
IETE
CallableStatement
Oracle 8i supports two types of stored procedures, namely, PL/SQL and java. PL/SQL stored procedures are called with in JDBC programs by means of the prepareCall() method. For example.
CallableStatement stproc_stmt = con.prepareCall({call procname});
The input parameter are bound to this instance using the setXXX() methods on the CallableStatement object. The output parameter is bound to this object instance using registerOutParameter() method.
IETE
CallableStatement.
executeUpdate method is used to execute stored procedure. Calling Stored function: The syntax for calling stored function
CallableStatement stproc_stmt = conn.prepareCall({?=call stored Procedure(?,?,?) / function(?,?,?)});
IETE
Q&A
IETE