`

How do I retrieve values from ResultSet?

 
阅读更多

Only for reference.

This example shows how to read data from a ResultSet returned by a executing an SQL query to a table in database.

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ResultSetExample {
	public static void main(String[] args) throws Exception
	{
		Connection connection = getConnection();
		try {
			String query = "select id, title, publisher, year, price from books";
			PreparedStatement ps = connection.prepareStatement(query);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				// Read values using column name
				String id = rs.getString("id");
				String title = rs.getString("title");
				String publisher = rs.getString("publisher");
				
				// Read values using column index
				int year = rs.getInt(4);
				float price = rs.getFloat(5);
				
				System.out.printf("%s. %s, %s, %d, %f\n", id, title, publisher, year, price);
			}
		} finally {
			closeConnection(connection);
		}
	}
	
	private static Connection getConnection() throws Exception {
		Connection connection = null;
		Class.forName("com.mysql.jdbc.Driver");
		connection = DriverManager.getConnection("jdbc:mysql://localhost/bookstore", "root", "");
		return connection;
	}
	
	private static void closeConnection(Connection connection) throws SQLException {
		if (connection != null && !connection.isClosed()) {			
			connection.close();
		}
	}
}

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics