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.PreparedStatement;
import java.sql.ResultSet;

public class Main {
    private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:YourDatabase";
    private static final String DB_USER = "user";
    private static final String DB_PASSWORD = "password";

    public static void main(String[] argv) throws Exception {
        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;
        Class.forName(DB_DRIVER);
        dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);

        String selectSQL = "SELECT USER_ID, USERNAME FROM Person WHERE USER_ID = ?";
        preparedStatement = dbConnection.prepareStatement(selectSQL);
        preparedStatement.setInt(1, 1001);

        ResultSet rs = preparedStatement.executeQuery();

        while (rs.next()) {

            String userid = rs.getString("USER_ID");
            String username = rs.getString("USERNAME");

            System.out.println("userid : " + userid);
            System.out.println("username : " + username);

        }

        preparedStatement.close();
        dbConnection.close();

    }
}