ResultSetExample.java Source code

Java tutorial

Introduction

Here is the source code for ResultSetExample.java

Source

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ResultSetExample {
    public static void main(String args[]) {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", "");

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery("SELECT SupplierName,ProductName, Price "
                    + "FROM ProductSuppliersView WHERE CategoryName LIKE '%BEVERAGES%' ");
            while (rs.next()) {
                String supplier = rs.getString("SupplierName");
                String product = rs.getString("ProductName");
                int price = rs.getInt("Price");

                System.out.println(supplier + " sells " + product + " for $" + price);
            }

            stmt.close();
            con.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}