Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Saturday, 6 January 2018

Oracle database connection with Java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Simple Java Program to connect Oracle database by using Oracle JDBC thin driver
 * Make sure you have Oracle JDBC thin driver in your classpath before running this program
 * @author
 */

public class OracleJdbc {

    public static void main(Stringargs[]) throws SQLException {
        //URL of Oracle database server
        String url ="jdbc:oracle:thin:@localhost:1632:DEVROOT32";
   
        //properties for creating connection to Oracle database
        Properties props = newProperties();
        props.setProperty("user","sarfraz");
        props.setProperty("password","tiger");
   
        //creating connection to Oracle database using JDBC
        Connection conn =DriverManager.getConnection(url,props);

        String sql ="select sysdate as current_day from dual";

        //creating PreparedStatement object to execute query
        PreparedStatement preStatement = conn.prepareStatement(sql);
 
        ResultSet result = preStatement.executeQuery();
   
        while(result.next()){
            System.out.println("Current Date from Oracle : " +         result.getString("current_day"));
        }
        System.out.println("done");
   
    }
}

Output:
Current Date from Oracle : 2012-04-1217:13:49
done

No comments:

Post a Comment