jdbc connections

jdbc connections

How can I coonect with oracle9i with java program using both thin driver and sun driver. plz send sample code for both the drivers

View Answers

January 21, 2011 at 11:00 AM

Hi Friend,

Follow these steps:

1) Import the following packages in your java file:

import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;

2) Load and Register the JDBC driver:

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

or you can use Class.forName("oracle.jdbc.driver.OracleDriver");

3) Connect to database:

a) If you are using oracle oci driver,you have to use:

Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:
@oracle.world", "root", "root");

where oracle.world is the TNSNAMES entry and root is the username and password.

b) If you are using oracle thin driver,you have to use:

Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:3306:roseindia",
"root", "root");

where localhost is the host,3306 is the port, roseindia is the database and root is the username and password.

4) Querying the database:

a)create statement:

Statement st = conn.createStatement();

b)write query and execute the query:

ResultSet rs = st.executeQuery("SELECT * from student");

5) Close the statement,resultset and connection:

rs.close();
st.close();
conn.close();

Try the following code:

import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;

public class OracleExample {
public static void main (String[] args) {
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection("jdbc:oracle:thin: @localhost:3306:Oracle", "rose", "rose");
Statement st = conn.createStatement();
ResultSet rs = sql_stmt.executeQuery("SELECT * from student");
String str = "";
while (rs.next())
{
System.out.println(rset.getInt(1)+" "+ rs.getString(2)+" "+ rset.getFloat(3)+"\n";
}
rs.close();
st.close();
conn.close();
}
catch(Exception e){}
}
}

Ads









Related Tutorials/Questions & Answers:
jdbc connections
jdbc connections  How can I coonect with oracle9i with java program... oracle.sql.*; 2) Load and Register the JDBC driver: DriverManager.registerDriver... oci driver,you have to use: Connection conn = DriverManager.getConnection("jdbc
how to implements jdbc connections using awt
how to implements jdbc connections using awt  sir, My name... valuable information about how to implements jdbc connections in awt.please give me sample example awt with jdbc. Thanking you sir.  
Advertisements
how to implements jdbc connections using awt?
how to implements jdbc connections using awt?   My name is Aditya... information about how to implements jdbc connections in awt.please give me sample example awt with jdbc.   We are proving you a simple application
ModuleNotFoundError: No module named 'connections'
ModuleNotFoundError: No module named 'connections'  Hi, My Python... 'connections' How to remove the ModuleNotFoundError: No module named 'connections' error? Thanks   Hi, In your python environment
closing connections in finally block
closing connections in finally block  Hi, What will happen if I close connection before closing the resultset in the finally clock? e.g. finally... { ... conn.close(); rs.Close(); } If i do as above ... what will happen
httpd.conf max connections
connections? Thanks   Hi, You can add following configuration in httpd.conf file to increase the number of connections to be handled by apache server
DOubt in Mysql connections
DOubt in Mysql connections  i have some problem regarding data base connections, i have created a hostname, username and password in filezilla server but after i give those names also it is showing error like this: mysql
ModuleNotFoundError: No module named 'django-connections'
ModuleNotFoundError: No module named 'django-connections'  Hi, My... named 'django-connections' How to remove the ModuleNotFoundError: No module named 'django-connections' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'mypy-boto3-codestar-connections'
ModuleNotFoundError: No module named 'mypy-boto3-codestar-connections' ...: ModuleNotFoundError: No module named 'mypy-boto3-codestar-connections' How to remove the ModuleNotFoundError: No module named 'mypy-boto3-codestar-connections' error
ModuleNotFoundError: No module named 'nw-connections'
ModuleNotFoundError: No module named 'nw-connections'  Hi, My... named 'nw-connections' How to remove the ModuleNotFoundError: No module named 'nw-connections' error? Thanks   Hi, In your python
JDBC access database
JDBC access database       JDBC is a Java Database Connectivity. The JDBC Connectivity provides API classes and interfaces for connecting the front end in Java application with database connections
ModuleNotFoundError: No module named 'django-user-connections'
ModuleNotFoundError: No module named 'django-user-connections'  Hi...: No module named 'django-user-connections' How to remove the ModuleNotFoundError: No module named 'django-user-connections' error? Thanks   
ModuleNotFoundError: No module named 'django-user-connections'
ModuleNotFoundError: No module named 'django-user-connections'  Hi...: No module named 'django-user-connections' How to remove the ModuleNotFoundError: No module named 'django-user-connections' error? Thanks   
Interact with connection pools to obtain and release connections
and release connections Prev Chapter 4. Demonstrate understanding... to obtain and release connections Connection handles... application Data Source</description> <res-ref-name>jdbc
JDBC
JDBC  why we use batch in jdbc
jdbc
jdbc  display the records using index in jdbc
jdbc
jdbc  Hai , Give a steps for jdbc connectivity
JDBC
JDBC  How to add set of queries in a single query in JDBC
jdbc
jdbc   how to write program to save data and retrieve data from the form in Java
jdbc
jdbc  why do we need to load jdbc drivers before connecting to database
JDBC
JDBC  how to set classpath for eclipse, java, where to copy jdbc connector
JDBC
JDBC  in class.forname which driver name we are writing for the connection from jdbc to sqlserver 2008
jdbc
jdbc  is it possible to use doget & dopost method with jdbc to call in a servlet programe
Jdbc
Jdbc  A java program with jdbc connectivity with insert,delete,update options for name,regno,mark1,mark2,total
JDBC
JDBC  can u send me the code of jdbc how to join two tables that are in relation
jdbc
jdbc define batch updates  define batch updates?exp   JDBC... links: http://www.roseindia.net/jdbc/Jdbc-batch-update.shtml http://www.roseindia.net/tutorial/java/jdbc/batchupdateresultsetexample.html
jdbc
jdbc  Write a web based student registration application where the students can register online with their enrollment no. You are required to use JSP, Servelet and JDBC
JDBC
JDBC save a data in the database  I need a code to save a data... between java and mysql using JDBC and saves the data into the database. import... con = null; String url = "jdbc:mysql://localhost:3306/"; String db
JDBC
JDBC code to save a data in the database  I need a code to save... the connection between java and mysql using JDBC and saves the data into the database...!"); Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db
jdbc
how can we set transaction level through jdbc api  how can we set transaction level through jdbc api   Use Connection.setTransactionIsolation(int) to set the desired tansaction level, which takes one of the 5 arguments
jdbc
how can we call stored procedure using jdbc  how can we call stored procedure using jdbc   Call a Stored Procedure using...(); For more information, visit the following link: JDBC call Stored Procedure

Ads