Discussion Locked

java & oracle

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
how to connect java with oracle database

3 replies

by kadsoft

import java.sql.*;

public class MyConnection {

/**
* You need the oracle's driver jar (classes12.jar)
*/
public static void main(String args[]) {
try {
//Attempts to establish a connection to the given database URL.
//The DriverManager attempts to select an appropriate driver from
//the set of registered JDBC drivers.
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection myConn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:MYDATABASE", "user", "password");

//Creates a Statement object for sending
//SQL statements to the database.
Statement myStatement = myConn.createStatement();

//Executes the given SQL statement, which returns a single ResultSet object.
ResultSet myResultSet = myStatement.executeQuery("SELECT * FROM table");

//Iterates the resultSet so you can get the info
while (myResultSet.next()) {
System.out.println(myResultSet.getString("column_table1"));
}
//close the ResultSet object
myResultSet.close();

//Releases this Connection object's database and JDBC resources
//immediately instead of waiting for them to be automatically released.
myConn.close();
}
catch (ClassNotFoundException cnfex) {
System.err.println("Can't load JDBC");
cnfex.printStackTrace();
System.exit(1);
}
catch (SQLException sqlex) {
System.err.println("No connection");
sqlex.printStackTrace();
}
}

}

by rajesh_m_vaghela

put oracle\ora92\lib\classes12.jar in your java application classpath. then load JDBC Driver
Class.formName("oracle.jdbc.OracleDriver");

user below url to connect database
url = jdbc:oracle:thin:@Hostname:Portnumber:Databasename
Connection c = DriverManager.getConnect(url).

I think this is enough

This discussion is closed to new replies.