You are on page 1of 2

driver=oracle.jdbc.driver.

OracleDriver url=jdbc:oracle:thin:@localhost:1521:xe unm=system pwd=mateen

Properties jdbcProp; public ItemDAO() { jdbcProp=new Properties(); try { jdbcProp.load(new FileInputStream("jdbc.properties")); } catch (Exception e) { System.out.println("unable to load a file"); } } private Connection getConnection() { Connection con=null; try { Class.forName("oracle.jdbc.driver.OracleDriver" );//driver loading String url="jdbc:oracle:thin:@localhost:1521:xe";// establishing connection String unm="system"; String pwd="mateen"; con=DriverManager.getConnection(url,unm,pwd);//crea ting statement } catch (Exception e) { System.out.println("Error while connecting"); } return con; } Types of statements in jdbc 1)Callable Statements Once you have established a connection to a database, you can use the Connection .prepareCall method to create a callable statement. A callable statement lets yo u execute SQL stored procedures. This next example creates a CallableStatement object with three parameters for s toring account login information. CallableStatement cs = con.prepareCall("{call accountlogin(?,?,?)}"); cs.setString(1,theuser); cs.setString(2,password); cs.registerOutParameter(3,Types.DATE); cs.executeQuery(); Date lastLogin = cs.getDate(3);

2)Statements The Statement interface lets you execute a simple SQL statement with no paramete rs. The SQL instructions are inserted into the Statement object when the Stateme nt.executeXXX method is called. Query Statement: This code segment creates a Statement object and calls the Stat ement.executeQuery method to select text from the dba database. The results of t he query are returned in a ResultSet object. How to retrieve results from a Resu ltSet object is explained in Result Sets below. Statement stmt = con.createStatement(); ResultSet results = stmt.executeQuery( "SELECT TEXT FROM dba "); Update Statement: This code segment creates a Statement object and calls the Sta tement.executeUpdate method to add an email address to a table in the dba databa se. String updateString = "INSERT INTO dba VALUES (some text)"; int count = stmt.executeUpdate(updateString);

ResultSet.TYPE_FORWARD_ONLY Default behavior in JDBC 1.0, application can only call next() on the result set . ResultSet.SCROLL_SENSITIVE ResultSet is fully navigable and updates are reflected in the result set as they occur. ResultSet.SCROLL_INSENSITIVE Result set is fully navigable, but updates are only visible after the result set is closed. You need to create a new result set to see the results. The update type parameter can be one of the following two values: ResultSet.CONCUR_READ_ONLY The result set is read only. ResultSet.CONCUR_UPDATABLE The result set can be updated. You can verify that your database supports these types by calling con.getMetaDat a().supportsResultSetConcurrency() method as shown here. Connection con = getConnection(); if(con.getMetaData().supportsResultSetConcurrency( ResultSet.SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)) { PreparedStatement pstmt = con.prepareStatement( "select password, emailaddress, creditcard, balance from registration where theuser = ?", ResultSet.SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); }

You might also like