Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

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);

            int rowSize = 0;
            while (rs.next()) {
                rowSize++;
            }

            System.out.println("Number of Rows in ResultSet is: " + rowSize);
            if (rowSize == 0) {
                System.out.println("Since there are no rows, exiting...");
                System.exit(0);
            }

            int cursorPosition = Math.round(rowSize / 2);

            System.out.println("Moving to position: " + cursorPosition);
            rs.absolute(cursorPosition);
            System.out.println("Name: " + rs.getString(2));

            rs.relative(-1);

            cursorPosition = rs.getRow();
            System.out.println("Moving to position: " + cursorPosition);
            System.out.println("Name: " + rs.getString(2));

            System.out.println("Moving to the first row");
            while (!rs.isFirst()) {
                rs.previous();
            }
            System.out.println("Name: " + rs.getString(2));
            connection.close();
        } catch (Exception e) {
            System.err.println(e);
        }
    }
}