Java Plan To Connect Oracle Database Alongside Illustration - Jdbc Tutorial Sample Code

How to connect to Oracle database from Java Program using JDBC API is mutual require for many Java programmer, though at that topographic point are lot of framework available which has simplified JDBC evolution e.g hibernate, Spring JdbcTempate together with many more, but creating Java plan to connect to oracle database from obviously one-time Java is all the same almost slow together with quickest method for testing together with debugging database connectivity. Database connectedness plan is too a mutual Java programming exercise inwards many Java programming courses on school, colleges together with diverse preparation institutes. We convey been exploring  some advanced concepts together with best practices on JDBC inwards my previous articles similar  Why should yous purpose PreparedStatement inwards Java together with 4 JDBC functioning tips for Java application , which yous may similar if yous are on to a greater extent than advanced level. This elementary coffee plan is intended for beginners inwards Java which convey simply started learning JDBC API.

If yous similar to read tutorials on database hence yous may notice difference betwixt truncate together with delete10 instance of SQL SELECT queries together with how to contend database transaction useful together with interesting.
 

How to connect Oracle Database from Java Program using JDBC - code example

How to connect to Oracle database from Java Program using JDBC API Java Program to connect Oracle Database alongside Example - JDBC Tutorial Sample Coderunning this Java program to connect Oracle database brand certain your Oracle database server is running together with yous convey JDBC sparse driver inwards your classpath similar ojdbc6.jar or ojdbc6_g.jar.


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 past times using Oracle JDBC sparse driver
 * Make certain yous convey Oracle JDBC sparse driver inwards your classpath earlier running this program
 * @author
 */

public class OracleJdbcExample {

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

        String sql ="select sysdate every bit current_day from dual";

        //creating PreparedStatement object to execute query
        PreparedStatement preStatement = conn.prepareStatement(sql);
   
        ResultSet consequence = 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-12 17:13:49
done


Error together with Exception piece connecting Oracle Database from Java Program:


1) Invalid Username together with Password
Exception inwards thread "main" java.sql.SQLException: ORA-01017: invalid username/password; logon denied
        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:452)

This Error comes when username together with password provided to Java plan connecting to Oracle database is non correct.

2)No suitable driver found

jdbc:oracle:thin:@localhost:1632:DEVROOT32
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:154)

This Error comes when JDBC sparse driver for relevant Oracle version is non inwards Classpath. e.g. ojdbc6.jar or ojdbc6_g.jar (compiled alongside javac -g alongside debug information) for Oracle 11g.

Also piece reading information from ResultSet ensure that yous are using proper column index to avoid Exception inwards thread "main" java.sql.SQLException: Invalid column index which comes when yous purpose invalid index similar zero to access ResultSet on diverse acquire together with laid method.

In this Java plan Example nosotros convey seen how to connect to Oracle database using JDBC sparse driver , alongside sparse driver its much easier to connect to oracle database every bit yous don’t require to do data-sources similar yous do if yous purpose JDBC ODBC Driver. Let me know if yous confront whatever number piece connecting to Oracle database shape Java Program. Another worth noting signal is connecting Oracle database using SSL from Java Program, which may encounter inwards about other coffee tutorial.

Further Learning
JSP, Servlets together with JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 together with 2
Java Program to opposite String using recursion

Komentar

Postingan populer dari blog ini

Difference Betwixt Struts Validatorform Vs Validatoractionform - Interview Question

How To Convert Inputstream To Byte Array Inwards Coffee - Two Examples

Difference Betwixt Fileinputstream Together With Filereader Inwards Coffee | Inputstream Vs Reader