Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;

public class Main {

    public static void main(String[] args) throws Exception {
        try {
            String url = "jdbc:odbc:yourdatabasename";
            String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
            String user = "guest";
            String password = "guest";

            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, user, password);
            Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP";
            ResultSet rs = stmt.executeQuery(sqlQuery);
            rs.last();

            // Move the cursor backwards through the ResultSet
            while (rs.previous()) {
                String nbr = rs.getString(1);
                String name = rs.getString(2);
                String job = rs.getString(3);
                String mgr = rs.getString(4);
                Timestamp hireDate = rs.getTimestamp(5);
                System.out.println(name);
            }

            rs.close();
            stmt.close();
            connection.close();
        } catch (Exception e) {
            System.err.println(e);
        }
    }
}