Join David Gassner for an in-depth discussion in this video Using generic getter methods in Java SE 7, part of Java Database Integration with JDBC.
- View Offline
So far in this chapter, I have been describing how to retrieve data from ResultSet using…specifically-typed Getter methods.…For example, in this version of my project, GenericGetters, in my Tours class, I have…calls to the methods getInt, getString, and getDouble, and I'm selecting the method to…match the data type of the column.…So, the tour ID is an integer, so I call getInt, tour name is a string column, so I'll call getString, and so on.…In Java 7, there is a new approach, a Generic Getter method called getObject that you can…use on every column of ResultSet, and you explicitly data type the return value using a different syntax.…
Now again, this new Generic Getter method is only available in Java 7.…So if you're using an older version of Java, you should stick with these explicitly data typed method calls.…But if you are working with Java 7 and therefore JDBC 4.1, this new approach is just another…option, another tool in your toolkit.…I'll start this exercise by re-factoring this code a bit.…I'm going to take the expression that's retrieving the tourId, and I'll extract it to a local…
Author David Gassner explains how to choose a JDBC driver and connect to one or more databases. He also provides detailed instructions on reading, selecting, and updating data; calling stored procedures; managing data via JavaBean classes or with prepared statements; and working with metadata.
- Importing a MySQL database
- Connecting to databases
- Handling JDBC exceptions
- Looping through result sets
- Limiting the number of fetched rows
- Filtering data with prepared statements
- Calling stored procedures
- Inserting, updating, and deleting rows with prepared statements
- Using a persistent database connection
- Committing and rolling back transactions