Friday, February 23, 2007

direct JDBC connection in java

Direct JDBC Connection to the database.

java.sql.Statement stmt = null;
ResultSet rs = null;
PreparedStatement ps = null;
List result = null;
try {

Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@mfn2asff4:10005:APPRDEV";
java.sql.Connection conn = DriverManager.getConnection(url,
"sgrtdbo", "sgrt123dbo");
stmt = conn.createStatement();
// TODO Auto-generated method stub
String maxDateInfo="select (max(To_Date(dateinfo,'DD MON YYYY HH24:MI:SS'))) as maxdate from srgt_metrics";
ResultSet resultSet=stmt.executeQuery(maxDateInfo);
while(resultSet.next()){
String maxDateFromDB=resultSet.getString(1);
System.out.println("Max date retrieved from the database is :"+maxDateFromDB);
}
}catch(Exception e){

System.out.println("IN Excepiton e");
e.printStackTrace();
}

No comments: