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;

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 insertTableSQL = "INSERT INTO Person" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
                + "(?,?,?,?)";
        preparedStatement = dbConnection.prepareStatement(insertTableSQL);

        dbConnection.setAutoCommit(false);

        java.util.Date today = new java.util.Date();

        preparedStatement.setInt(1, 101);
        preparedStatement.setString(2, "101");
        preparedStatement.setString(3, "system");
        preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime()));
        preparedStatement.addBatch();

        preparedStatement.setInt(1, 102);
        preparedStatement.setString(2, "102");
        preparedStatement.setString(3, "system");
        preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime()));
        preparedStatement.addBatch();

        preparedStatement.setInt(1, 103);
        preparedStatement.setString(2, "103");
        preparedStatement.setString(3, "system");
        preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime()));
        preparedStatement.addBatch();

        preparedStatement.executeBatch();

        dbConnection.commit();

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

    }
}