Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

newbie to both java and oracle.. how do i ...

 
Kevin Rauer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a java program to connect to an Oracle server and return a record set..
I keep getting the following error..
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified..
here is the code I am trying to get to work..
import oracle.sql.*;
import oracle.jdbc.driver.*;
protected void loadTestDataDB(ArrayList CIFList)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e)
{
logError("Error: Failed to load JDBC DBC Driver!");
}
try
{

DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver()) ;
Connection con = DriverManager.getConnection("jdbc dbc:thin:@172.25.5.70:1521:aixedev","user","password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM agents");
...
...
... etc
What am I doung wrong.. the oracle client is installed on the machine and i can connect to the above with sqlplus..
Any help would be apreciated..
 
Joe Ess
Bartender
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get rid of the try-catch block where you register sun.jdbc.odbc.JdbcOdbcDriver. You don't need ODBC to connect to Oracle, and if you wanted to use it, you obviously haven't set up an ODBC data source to do so. The rest should work as is.
 
Kevin Rauer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to connect to the oracle database.. if I don't have to use odbc then thats fine..
could you give me a code example to just return a recordset from an oracle database..
Thanks in advance
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are almost there. Try this:

When you do a "Class.forName(...)" on a JDBC driver (such as the oracle thin driver), it will automatically register the driver with the DriverManager class. Also, the URL for the thin driver starts out "jdbcracle:thin", not "jdbcdbc:thin".
It's a very good idea to close all of your database resources (connection, statement, resultset) in a "finally{ }" block to make sure they are closed regardless of what happens in the try/catch sections of the code. There is no need to import any of the "oracle.*" classes.
As Joe pointed out, there is no need to use the jdbcdbc driver and, in fact, with the thin driver (type 4) it's not necessary to have the Oracle client installed at all. So you can run on any machine as long as you have the thin driver jar/zip file installed.
[ November 11, 2003: Message edited by: Wayne L Johnson ]
 
Kevin Rauer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a Million..
 
Andy Grove
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd recommend downloading a free trial of our FireStorm/DAO product - you can point it at a database schema and have it generate complete Java/JDBC code for insert/update/delete and query operations for your tables. It's a great way to learn about the best way to write this sort of code. It also generates a ResourceManager utility class for creating/destroying connections.
More info at http://www.codefutures.com/firestorm
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic