|
Java™ by example!
|
|
|
How do I retrieve rows from a database table?
Check out the MySQL syntax for SELECT here. Here's an example that gets all the rows from the customer table, created in this answer. Basically, you need to create a Statement object from a Connection and invoke executeQuery on it, passing it the SQL SELECT command. It returns a ResultSet containing the database rows that match your query. You can then iterate over the rows with the next method. The first call to next will position the "cursor" to the first row. You can get a particular column in that row with the getXXX methods, specifying either the column index or the column name. Make sure the database column type and the resulting variable type match, or a proper conversion is done. If types don't match, a Bad format SQLException is thrown. Main.java:
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|