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.SQLException;
import java.sql.Statement;

public class Main {

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        boolean executeResult;
        try {
            String driver = "oracle.jdbc.driver.OracleDriver";
            Class.forName(driver).newInstance();
            System.out.println("Connecting to database...");
            String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
            conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
            stmt = conn.createStatement();
            conn.setAutoCommit(false);
            if (!conn.getAutoCommit())
                System.out.println("Auto-commit is set to false");
            String sql = "INSERT INTO Location VALUES(715,'Houston')";
            stmt.executeUpdate(sql);
            sql = "INSERT INTO Employees VALUES" + "(8,'K','4351',{d '2000-02-00'},715)";
            stmt.executeUpdate(sql);
            conn.commit();
        } catch (SQLException se) {
            String msg = se.getMessage();
            msg = "SQLException occured with message: " + msg;
            System.out.println(msg);
            System.out.println("Starting rollback operations...");
            try {
                conn.rollback();
            } catch (SQLException se2) {
                se2.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}