oracle.otnsamples.jdbc.IntervalDayToSecond (Java2HTML)
package oracle.otnsamples.jdbc;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;
import oracle.sql.INTERVALDS; import oracle.jdbc.OraclePreparedStatement; import oracle.jdbc.OracleResultSet;
public class IntervalDayToSecond {
public IntervalDayToSecond() { }
public static void main(String[] args) { IntervalDayToSecond intervalDayToSecond = new IntervalDayToSecond(); try {
intervalDayToSecond.getFlightDuration();
} catch (Exception ex) { System.out.println("Some Exception in Main method: "+ex.toString()); } } private void getFlightDuration() { Connection conn = null;
OraclePreparedStatement pstmt = null; OracleResultSet res = null; try {
conn = this.getConnection(); String sqlString ="SELECT name,route,duration FROM flight_duration";
pstmt = (OraclePreparedStatement)conn.prepareStatement(sqlString);
res = (OracleResultSet)pstmt.executeQuery();
System.out.println("Selected records before insert are as follows\n"); System.out.println("Flight Name Route Duration\n"); String name = null; String dest = null; String dur = null; while (res.next()) { name = res.getString(1); dest = res.getString(2); dur = res.getINTERVALDS(3).toString(); System.out.println(name+" "+dest+" "+dur); } if (res != null) res.close(); if (pstmt != null) pstmt.close();
sqlString = "INSERT INTO flight_duration VALUES(?,?,?)";
pstmt = (OraclePreparedStatement)conn.prepareStatement(sqlString); pstmt.setString(1,"AC-2001"); pstmt.setString(2,"Singapore to SFO");
pstmt.setINTERVALDS(3, new INTERVALDS ("2 03:15:15.0")); boolean bool = pstmt.execute();
sqlString ="SELECT name,route,duration FROM flight_duration";
pstmt = (OraclePreparedStatement)conn.prepareStatement(sqlString);
res = (OracleResultSet)pstmt.executeQuery(); System.out.println("Selected records after insert are as follows\n"); System.out.println("Flight Name Route Duration\n"); while (res.next()) { name = res.getString(1); dest = res.getString(2); dur = res.getINTERVALDS(3).toString(); System.out.println(name+" "+dest+" "+dur); } } catch (SQLException sqlEx) { System.out.println("SQL Exception: "+sqlEx.toString()); } catch (Exception ex) { System.out.println("Exception: "+ex.toString()); } finally { try { if (res != null) res.close(); if (pstmt != null) pstmt.close(); if ((conn!=null) || (!conn.isClosed())) conn.close(); } catch(SQLException sqlEx) { System.out.println("Exception while closing database connection: " +sqlEx.toString()); } } } private Connection getConnection() throws ClassNotFoundException, Exception{ Connection conn = null; String url = "jdbc:oracle:thin:@localhost:1521:orcl"; try { Class.forName("oracle.jdbc.OracleDriver"); conn = DriverManager.getConnection(url,"scott","tiger"); }catch (Exception ex) { System.out.println("Exception while getting database connection: " +ex.toString() ); } return conn; } }
|