Wednesday, January 19, 2011

Java Connection To Oracle Database

In this chapter we will connect between Java and Oracle. Open your IDE to run java extension in this case I will use Netbeans 6.8.
1. Create a new java project (Click File -> New Project-> Java Application -> Project Name (For Instance:ConnectionDatabase)->Finish).



2. Create class ConnectionDatabase.java (At Source Package->Right Click->New->Java Class).



3. This is the source code to connect between Java and oracle (Note:Please input ojdbc14.jar in library of project). The oracle database is "ORCL" with user name "dbPercakapan" and Password "percakapan12345".


import java.sql.*;
public class ConnectionDatabase {
Connection conn=null;

public Connection getConnection() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
String _user="dbPercakapan";
String _pass="percakapan12345";
conn=DriverManager.getConnection(url,_user,_pass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch ( SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main (String[] args) {
new ConnectionDatabase().getConnection();

}
}


Note :
1. At Line 6 statement is use to call driver name of oracle.
2. At Line 7 -10 is use to open connection in oracle.

If in the console display "Connection Successfully" you have made it connect it. ^^

No comments:

Post a Comment