En Sun publicaron un nuevo tutorial, sobre el uso de JDBC en Java Studio Creator, importante su lectura y recomendable para los desarrolladores que se interesan en el uso.
Ver articulo
Parte del codigo del articulo:
Ver articulo
Parte del codigo del articulo:
Connection conn = null ;
Statement sqlStatement = null ;
ResultSet rs = null ;
try {
javax.naming.Context ctx = new javax.naming.InitialContext() ;
DataSource ds = (DataSource)ctx.lookup(" java:comp/env/jdbc/Travel" ) ;
conn = ds.getConnection() ;
// setup the connection
conn.setAutoCommit(false) ;
// execute the query
sqlStatement = conn.createStatement() ;
rs = sqlStatement.executeQuery(" select count(*) from TRIP" ) ;
rs.next() ;
int rows = rs.getInt(1) ;
conn.commit() ;
info(" Rows in table TRIP: " + Integer.toString(rows)) ;
} catch (Exception ex) {
error(" Error counting rows: " + ex.getMessage() );
try {
if ( conn != null ) {
conn.rollback() ;
}
} catch (SQLException sqle) {
log(" Error on rollback " + sqle.getMessage() );
}
}
finally {
// close the ResultSet
if ( rs != null ) {
try {
rs.close() ;
} catch (Exception ex) {
log(" Error Description" , ex);
}
}
// close the statement
if ( sqlStatement != null ) {
try {
sqlStatement.close() ;
} catch (Exception ex) {
log(" Error Description" , ex);
}
}
if ( conn != null ) {
// cleanup and close the connection.
try {
conn.close() ;
} catch (Exception ex) {
log(" Error Closing connection " , ex);
}
}
}
Comments