• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 "jdbc:oracle:thin", not "jdbc:odbc: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 jdbc:odbc 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a Million..
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Well THAT's new! Comfort me, reliable tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic