?
Solved

java and Oracle

Posted on 2003-03-19
6
Medium Priority
?
363 Views
Last Modified: 2010-03-31
hi friends,i hava  database ib Oracle,i wanto know how i can read data from it in java.
how to establish connection and "bridge".
i know how to read values from MS access database.its very imp,can some one post some
sample code.thanku


riv
0
Comment
Question by:riv9394
5 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8165958
 Is it the same as MS Access, you jsut need to load a different driver. The rest should be the same.
0
 

Accepted Solution

by:
smilidon earned 320 total points
ID: 8165962
Hi,

if you want to use JDBC, you need the oracle driver. You get it from the oracle web site. download the classes12.zip and put it in your classpath. maybe you must rename it to .jar

you establish the connection with DriverManager.getConnection(
"jdbc:oracle:thin:@url:port:sid", "user", "pw");


I found this example with google(there are many more)

import java.sql.*;

public class DBConnection

{

/** JDBC connection used by the application.
*/
static Connection st_conn;

public static void init(String conn, String usr, String pwd)
{

try {

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
st_conn = DriverManager.getConnection(
"jdbc:oracle:thin:@feast.ucsd.edu:1521:XML", "groupX", "groupXpwd");

} catch (SQLException exc) {

throw new ConversionException( "DB Connection Error.\n" + exc.toString() );
}
}

 

/** Executes SQL updates, inserts, deletes, and DDL statements.
*/
static public boolean write(String U)
{
try {
Statement stmt = st_conn.createStatement();
stmt.executeUpdate(U);
stmt.close();
return true;
} catch (SQLException exc) {
System.out.println("DB Error: " + exc.toString() );
System.out.println("Statement " + U);
return false;
}
}

 

/** Executes SQL query and returns result as JDBC ResultSet object.
*/
static public ResultSet query (String Q)
{

ResultSet rset = null;

try {
Statement stmt = st_conn.createStatement();
rset = stmt.executeQuery(Q);
} catch (SQLException exc) {
System.out.println("DB Error: " + exc.toString() );
System.out.println("Statement " + Q);
return null;
}
return rset;
}
}


Hope this helps...

 

0
 
LVL 35

Expert Comment

by:TimYates
ID: 8166054
> maybe you must rename it to .jar

No need :-)  Just make sure it's in your classpath
0
 

Expert Comment

by:CleanupPing
ID: 9058603
riv9394:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9727203
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept smilidon's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month7 days, 6 hours left to enroll

592 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question