JdbcDemo.java Source code

Java tutorial

Introduction

Here is the source code for JdbcDemo.java

Source

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

public class JdbcDemo {
    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()) {
            String name = rs.getString("Name");
            String desc = rs.getString("Description");
            int qty = rs.getInt("Qty");
            float cost = rs.getFloat("Cost");
            System.out.println(name + ", " + desc + "\t: " + qty + "\t@ $" + cost);
        }
        con.close();
    }
}