/*** * File : accessDatabase.java * Author : Mayank Bawa * Written : 17 November, 2000 * Description : Simple program to illustrate concepts in tutorial */ import java.sql.* ; public class accessDatabase{ public static void main(String[] args) throws SQLException { String bar, beer ; float price ; Connection c = null ; try { Class.forName("oracle.jdbc.driver.OracleDriver"); c = DriverManager.getConnection( "jdbc:oracle:thin:@oracle-prod:1521:OPROD", "user", "cryptic"); Statement s = c.createStatement() ; c.setAutoCommit(false) ; s.executeUpdate("CREATE TABLE Sells " + "(bar VARCHAR2(40), beer VARCHAR2(40), price REAL)" ) ; s.executeUpdate("INSERT INTO Sells VALUES " + "('Bar Of Foo', 'BudLite', 2.00)" ) ; c.commit() ; c.setAutoCommit(true) ; ResultSet rs = s.executeQuery("SELECT * FROM Sells") ; while( rs.next() ){ bar = rs.getString("bar"); beer = rs.getString("beer"); price = rs.getFloat("price"); System.out.println(bar + " sells " + beer + " for " + price + " dollars."); } } catch (ClassNotFoundException ex){ System.out.println(ex); } catch (SQLException ex){ if ( c != null ){ c.rollback() ; c.setAutoCommit(true) ; } System.out.println("SQLException caught"); System.out.println("---"); while ( ex != null ){ System.out.println("Message : " + ex.getMessage()); System.out.println("SQLState : " + ex.getSQLState()); System.out.println("ErrorCode : " + ex.getErrorCode()); System.out.println("---"); ex = ex.getNextException(); } } } }