How can i connect oracle database from java program ? explain the steps with one example?

Questions by vengal79

Showing Answers 1 - 4 of 4 Answers

PG

  • Aug 19th, 2006
 

 
Class.forName("oracle.jdbc.driver.OracleDriver");     
Connection con = DriverManager.getConnection("dburl", "id", "password");

  Was this answer useful?  Yes

Deepti

  • Aug 20th, 2006
 

Steps to follow in creation of JDBC program-

import java.sql.*;

public class driver1 {

public static void main() throws SQLException,ClassNotFoundException {

Connection con=null;

try {

class.forName("sun.jdbc.odbc.jdbcodbcDriver");

con=DriverManager.getConnection("jdbc:odbc:datasourcename","scott","tiger");

Statement stmt=con.CreateStatement();

Resultset rs=stmt.executeQuery("Select * from EMP");

while(rs.next()) {

System.out.println(rs.getInt(1));

System.out.println(rs.getString(2));

}

int i=stmt.executeUpdate("update EMP set sal=sal+100 where eno='101' ");

System.out.println(i +"rows updated");

con.close();

}

}

Class.forName ("oracld.jdbc.driver.OracleDriver");

Connection con = DriverManager.getConnection ("jdbc:oracle:thin@localhose:1521:xe","scott","tiger");

xe is for oracle 10g

orcl is for oralce 8i and 9i

  Was this answer useful?  Yes

Jayakanthan

  • Oct 26th, 2006
 

Hi friends,Class.forName("oracle:jdbc:driver.OracleDriver");Connection c=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:myoracle","scott","tiger");scott-usernametiger-passwordmyoracle-SID nameSid name we are giving at the time of oracle install.In oracle 8i we can convert classes111.zip into classes111.jar and put at server lib in tomcat.In oracle 9i we can convert classes12.zip into classes12.jar and put at server lib in tomcat.

  Was this answer useful?  Yes

Raghuram Edadala

  • Nov 24th, 2006
 

import java.io.DataInputStream;import java.sql.DriverManager;import java.sql.Connection;import java.sql.Statement;import java.sql.SQLException;public class CRUDOperations{ public static void main(String raghu[])throws SQLException { Connection con=null; try { DataInputStream dis=new DataInputStream(System.in); Class.forName("oracle.jdbc.driver.OracleDriver"); con=DriverManager.getConnection("dburl","user","pswd"); Statement st=con.createStatement(); System.out.println("Enter the Employ id : "); int id=Integer.parseInt(dis.readLine()); System.out.println("Enter the Employ Name : "); String name=dis.readLine(); System.out.println("Enter the Employ salary : "); float salary=Float.parseFloat(dis.readLine()); st.executeUpdate("insert values into Employee(id,'name',salary)"); } catch(Exception e) { e.printStackTrace(); } finally { con.close(); } }}

  Was this answer useful?  Yes

Supriya Ahire

  • Feb 2nd, 2007
 

Hello all,
Following are the simple steps of jdbc

1) Load the Driver using the statement Class.forName("Oracle.jdbc.driver.OracleDriver");

2) Establish connection to the oracle database.
Connection conn= DriverManager.getConnection("main prortocol:sub protocol:type4driver:name of oracle server:default port number(1521):service name", "username","password");

Here,
main protocol :Jdbc
Sub Protocol : Oracle
Type 4
Driver : thin

default port number is 1521
Service Name is Oracle9i
username is scott
password is Tiger

3) Design a query
example:
String query="insert into table table name(?,?,?)";

4)Create a statment Object Using prepared statement.

PreparedStatement pstat= con.preparestatment(query);
here we need to pass the query,
we are passing dynamic query using prepared statement.

5)Execute Query using pstat
pstat.executeQuery();

6) Close the connection

Regards,
Ms.Supriya Ahire

  Was this answer useful?  Yes

RajNair

  • Feb 9th, 2007
 

DriverManager.registerDriver(new Oracle:jdbc:OracleDriver());Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "scott", "tiger");

Step 1: Import JAVA Package:-
import java.sql.*;

Step 2: Register the driver with the following command:-
Class.For.Name("sun.jdbc.odbc.JdbcOdbcDriver");

Step 3:- Create the connection with database:-
Connection cn = DriverManage.getConnection("Jdbc:Odbc:name of dsn");

Step 4:- Open the connection:-
cn.open();

Step 5:- For Insertion of data:-
PreparedStatement ps = cn.prepareStatement("insert into <table name> values(?,?,?,?)");
ps.setInt(1,Integer.parseInt(t1.setText()));
ps.setString(2,t2.setText());

Step 6:- Execute the command:-
ps.executeUpdate();

Step 7:- Close the connection:-
cn.close();

NOTE:- Above step 7 is for only insertion of data.

"for retreival of data do same till step no 4 and then type the following command":-
ResultSet rs;

Rules of making DSN:-
Go to Control Panel -> Administrative Tools -> ODBC -> Add ->SQL Server -> Finish -> Name(any type) -> Next -> Next -> Folder name ->Next -> Finish -> Test -> Ok -> Finish.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions