Because the SQL SELECT query here is static so we just create a
Statement object from the connection. The while loop iterates over the rows contained in the result set by repeatedly checking return value of the
ResultSet’s
next() method. The
next() method moves a cursor forward in the result set to check if there is any remaining record. For each iteration, the result set contains data for the current row, and we use the
ResultSet’s
getXXX(column index/column name) method to retrieve value of a specific column in the current row, for example this statement:Retrieves value of the second column in the current row, which is the
Sql Front 5.1 Build 4.16 Serial Idm
username field. The value is casted to a String because we know that the
username field is of type VARCHAR based on the database schema mentioned previously. Keep in mind that the column index here is 1-based, the first column will be at index 1, the second at index 2, and so on. If you are not sure or don’t know exactly the index of column, so passing a column name would be useful:For other data types, the
ResultSet provide appropriate getter methods:
- getString()
- getInt()
- getFloat()
- getDate()
- getTimestamp()
- …
TIPS: Accessing column’s value by column index would provide faster performance then column name.
7. JDBC Executing UPDATE Statement Example
The following code snippet will update the record of “Bill Gates” as we inserted previously:This code looks very similar to the INSERT code above, except the query type is UPDATE.
8. JDBC Execute DELETE Statement Example
The following code snippet will delete a record whose
username field contains “bill”:So far we have one through some examples demonstrating how to use JDBC API to execute SQL INSERT, SELECT, UPDATE and DELETE statements. The key points to remember are:
- Using a Statement for a static SQL query.
- Using a PreparedStatement for a parameterized SQL query and using setXXX() methods to set values for the parameters.
- Using execute() method to execute general query.
- Using executeUpdate() method to execute INSERT, UPDATE or DELETE query
- Using executeQuery() method to execute SELECT query.
- Using a ResultSet to iterate over rows returned from a SELECT query, using its next() method to advance to next row in the result set, and using getXXX() methods to retrieve values of columns.
You can download source code of sample demo programs for each type of query in the attachments section. For more interactive hands-on with JDBC CRUD operations, watch this video:
NOTE: If you use Spring framework to acces relation database, consider to use Spring JdbcTemplate that simplifies and reduces the code you need to write.
JDBC API References:
Sql Front 5.1 Build 4.16 Serial Key
Related JDBC Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.
Sql Front 5.1 Build 4.16 Serial Download
Attachments:[Demo program for DELETE statement] | 0.9 kB |
[Demo program for INSERT statement] | 1 kB |
[Demo program for SELECT statement] | 1 kB |
[Demo program for UPDATE statement] | 1 kB |
[MySQL script to create database and table] | 0.2 kB |