-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
The simple jdbc demo is far from simple
I (hom) wanted to squeeze in too much.
The pull request that goes with this issue simplifies things to basic jdbc interaction
Add tests, so we can see how a test can interact with a database and that you should clean up afterwards.
For instance in StudentDemo we now have the straightforward code below with all fields in the record
being recognizable and have their proper marshall-in operations applied (sql.date to LocalDate)
public List<Student> listAll() {
var result = new ArrayList<Student>();
try ( Connection con = ds.getConnection(); PreparedStatement pst = con.prepareStatement( query ); ResultSet rs = pst.executeQuery(); ) {
while ( rs.next() ) {
Integer snummer = rs.getInt( 1 );
String firstname = rs.getString( 2 );
String lastname = rs.getString( 3 );
LocalDate dob = rs.getDate( 4 ).toLocalDate();
Integer cohort = rs.getInt( 5 );
String email = rs.getString( 6 );
String gender = rs.getString( 7 );
String student_class = rs.getString( 8 );
Boolean active = rs.getBoolean( 9 );
Student student = new Student( snummer, firstname, lastname, dob, cohort, email, gender, student_class, active );
result.add( student );
}
} catch ( Throwable ex ) {
Logger.getLogger( StudentDemo.class.getName() ).log( Level.SEVERE, null, ex );
}
return result;
}
This is way more understandable and conforms to normal jdbc usage guidelines.
Contrast with
var result = new ArrayList<Student>();
String query = "select * from students";
try ( Connection con = ds.getConnection();
PreparedStatement pst = con.prepareStatement( query );
ResultSet rs = pst.executeQuery(); ) {
while ( rs.next() ) {
Object[] params = new Object[rs.getMetaData().getColumnCount()];
for ( int i = 0; i < rs.getMetaData().getColumnCount(); i++ ) {
Object x=rs.getObject( 1 + i );
System.out.println( "x = " + x );
params[i] = rs.getObject( 1 + i );
}
result.add( Student.fromArray( params ) );
}
} catch ( Throwable ex ) {
Logger.getLogger( StudentDemo.class.getName() ).log( Level.SEVERE, null, ex );
}
return result;
// throw new UnsupportedOperationException( "Not supported yet." ); //To change body of generated methods, choose Tools | Templates.
}
where complex things with Objects and meta info is done,
Metadata
Metadata
Assignees
Labels
No labels