PrintResultSet.java Source code

Java tutorial

Introduction

Here is the source code for PrintResultSet.java

Source

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

public class PrintResultSet {
    public static void main(String args[]) throws Exception {
        String query = "SELECT Name,Description,Qty,Cost FROM Stock";
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            System.out.print(rs.getString("Name") + "\t");
            System.out.print(rs.getString("Description") + "\t");
            System.out.print(rs.getInt("Qty") + "\t");
            System.out.println(rs.getFloat("Cost"));
        }
    }
}