Java code to connect with oracle 10g database
Download ojdbc14.jar and add to the project library.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCConn {
/**
* @param args
*/
public static void main(String[] args) {
String userName = "nataraj";
String password = "nataraj";
System.out.println("User Name: "+userName);
System.out.println("Password: "+password);
Connection connection;
PreparedStatement prest;
try {
Class.forName("oracle.jdbc. driver.OracleDriver");
connection = DriverManager.getConnection(" jdbc:oracle:thin:@localhost: 1521:xe",userName ,password );
System.out.println(" Successfully connected to Database");
String sql = "select * from emp";
prest = connection.prepareStatement( sql);
ResultSet rs1 = prest.executeQuery();
while (rs1.next()){
String name = rs1.getString(1);
int number = rs1.getInt(2);
System.out.println(name + "\t- " + number);
}
} catch (ClassNotFoundException e) {
System.out.println("Unable to connect to Database");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Unable to connect to Database");
e.printStackTrace();
}catch (Exception e) {
System.out.println("Unable to connect to Database");
e.printStackTrace();
}
}
}
Hi Nataraj, nice program ...i have similar program how to connect Oracle using JDBC...
ReplyDeletehttp://javabynataraj.blogspot.in/2013/06/oracle-jdbc-connection-test.html