Web Development

Our forums are currently in maintenance mode and the ability to post is disabled. We will be back up and running as soon as possible. Thanks for your patience!

General discussion

Locked

java & oracle

By sudeep.nigam ·
how to connect java with oracle database

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

by rlee In reply to java & oracle

Try starting here:
http://www.orafaq.com/faqjdbc.htm
-- RL

Collapse -

by kadsoft In reply to java & oracle

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();
}
}

}

Collapse -

by rajesh_m_vaghela In reply to java & oracle

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

Related Discussions

Related Forums