The Artima Developer Community
Sponsored Link

Java Seminars by Bill Venners
JDBC
Lecture Handout

This lecture gives an introductory overview of JDBC.

Agenda


JDBC


JDBC Tier Models


SQL Variety


Registering JDBC Drivers


Making a Connection


JDBC URLs


Using the Connection


Statements


A Basic Query


Transactions


Transaction Isolation Levels


JDBC Steps


The Student Database


The Student Sorter Application

// In file jdbc/ex1/StudentSorter.java
import java.sql.*;
public class StudentSorter {
    public static void main(String[] args) {
        try {
            Class.forName(
                "sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection(
                "jdbc:odbc:mydb", "", "");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(
                "SELECT LAST, FIRST, TOTAL " +
                "FROM students.csv mydb " +
                "ORDER BY TOTAL DESC");
            while (rs.next()) {
                String s = rs.getString("FIRST") + " "
                    + rs.getString("LAST") + ": "
                    + rs.getString("TOTAL");
                System.out.println(s);
            }
            stmt.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
Here's the the output of StudentSorter:
Joe Java: 400
Jane Java: 400
Bart Smart: 360
Lizzy Busy: 200
Joe Slow: 0

Sponsored Links

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use