Java Plan To Connect Oracle Database Amongst Example - Jdbc Tutorial Sample Code

How to connect to Oracle database from Java Program using JDBC API is mutual ask for many Java programmer, though at that spot are lot of framework available which has simplified JDBC evolution e.g hibernate, Spring JdbcTempate in addition to many more, but creating Java computer program to connect to oracle database from apparently onetime Java is all the same nearly slow in addition to quickest method for testing in addition to debugging database connectivity. Database connectedness computer program is too a mutual Java programming exercise inwards many Java programming courses on school, colleges in addition to diverse preparation institutes. We convey been exploring  some advanced concepts in addition to best practices on JDBC inwards my previous articles similar  Why should yous role PreparedStatement inwards Java in addition to 4 JDBC functioning tips for Java application , which yous may similar if yous are on to a greater extent than advanced level. This uncomplicated coffee computer program is intended for beginners inwards Java which convey only started learning JDBC API.

If yous similar to read tutorials on database therefore yous may notice difference betwixt truncate in addition to delete10 instance of SQL SELECT queries in addition to how to cope database transaction useful in addition to 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 amongst Example - JDBC Tutorial Sample Coderunning this Java program to connect Oracle database brand certain your Oracle database server is running in addition to 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 number = 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 in addition to Exception spell connecting Oracle Database from Java Program:


1) Invalid Username in addition to 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 in addition to password provided to Java computer program 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 amongst javac -g amongst debug information) for Oracle 11g.

Also spell 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 role invalid index similar nothing to access ResultSet on diverse acquire in addition to laid method.

In this Java computer program Example nosotros convey seen how to connect to Oracle database using JDBC sparse driver , amongst sparse driver its much easier to connect to oracle database every bit yous don’t ask to do data-sources similar yous do if yous role JDBC ODBC Driver. Let me know if yous facial expression upwards whatever number spell connecting to Oracle database cast Java Program. Another worth noting request is connecting Oracle database using SSL from Java Program, which may come across inwards unopen to other coffee tutorial.

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

Sumber https://javarevisited.blogspot.com/

0 Response to "Java Plan To Connect Oracle Database Amongst Example - Jdbc Tutorial Sample Code"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel