import java.io.*;
import java.sql.*;
import java.text.*;
public class GetXXXMethods {
Connection conn;
public GetXXXMethods() {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
}
catch (SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args)
throws Exception, IOException {
new GetXXXMethods().process();
}
public void process() throws IOException, SQLException {
double age = 0;
long person_id = 0;
String name = null;
Timestamp birth_date = null;
int rows = 0;
ResultSet rslt = null;
Statement stmt = null;
try {
stmt = conn.createStatement();
rslt = stmt.executeQuery(
"select person_id, last_name||', '||first_name name, " +
"birth_date, ( months_between( sysdate, birth_date ) / 12 ) age " +
"from PERSON where last_name = 'O''Reilly' and first_name = 'Tim'");
if (rslt.next()) {
rows++;
person_id = rslt.getLong(1);
name = rslt.getString(2);
birth_date = rslt.getTimestamp(3);
age = rslt.getDouble(4);
System.out.println("person_id = " +
new Long(person_id).toString());
System.out.println("name = " + name);
System.out.println("birth_date = " +
new SimpleDateFormat("MM/dd/yyyy").format(birth_date));
System.out.println("age = " +
new DecimalFormat("##0.#").format(age));
}
rslt.close();
rslt = null;
stmt.close();
stmt = null;
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (rslt != null)
try { rslt.close(); } catch (SQLException ignore) { }
if (stmt != null)
try { stmt.close(); } catch (SQLException ignore) { }
}
}
protected void finalize()
throws Throwable {
if (conn != null)
try { conn.close(); } catch (SQLException ignore) { }
super.finalize();
}
} |