Sunday, March 4, 2012

Java Program to Connect to Oracle DB

1. Include the library Ojdbc6.jar ..
2. Write the Java Code
3. Execute the Code


1. Include the library Ojdbc6.jar
Write click on Application ,Properties, ,Libraries and Class Path -> Add JAR/Directory .click Add ...

2. Write the Java code below .

package project1;

import java.sql.*;

class class1 {
public static void main(String args[]) throws SQLException {
DriverManager.registerDriver(
new oracle.jdbc.driver.OracleDriver()
);
String serverName = "mohammed-pc.in.oracle.com";
int port = 1521;
String user = "ashraf";
String password = "ashraf";
String SID = "orcl";
String URL = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + SID;
Connection conn = DriverManager.getConnection(URL, user, password);
String SQL = "SELECT * from em ";
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(SQL);
while (rs.next()) {
System.out.println(
rs.getString(1) +
"\t" +
rs.getString(2)
);
}
stat.close();
conn.close();
}
}

No comments:

Post a Comment