Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple jdbc is not simple #11

Open
homberghp opened this issue Feb 22, 2024 · 0 comments
Open

simple jdbc is not simple #11

homberghp opened this issue Feb 22, 2024 · 0 comments

Comments

@homberghp
Copy link
Contributor

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant