Tags

, , , ,


Here, I will show you the steps to make a connection between Oracle database and your java Program.

1) Set your  ORACLE_HOME

Assume the ORACLE_HOME is D:\OracleApp\product\11.2.0\dbhome_1

set ORACLE_HOME is  D:\OracleApp\product\11.2.0\dbhome_1\jdbc\lib directory.

2)  Here is the complete code.

package oracle;
import java.sql.*;

public class Oracle {

public static void main(String[] args) throws SQLException, ClassNotFoundException {
Class.forName(“oracle.jdbc.driver.OracleDriver”);

String serverName = “192.168.20.189”;
int port = 1521;
String user = “system”;
String password = “epillars”;
String SID = “oracle”;

String URL = “jdbc:oracle:thin:@” + serverName + “:” + port + “:” + SID;
Connection conn = DriverManager.getConnection(URL, user, password);
String SQL = “SELECT * from XXUSER.TBL_DEMO”;
Statement stat = conn.createStatement();
ResultSet output = stat.executeQuery(SQL);

while (output.next()) {
System.out.println(output.getInt(1)+”\t” + output.getInt(2));
}
System.out.println(“SuccessFull”);

stat.close();
conn.close();

}
}

Advertisements