Class.forName(driverName); This will load the Driver.
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3879
posted
0
Warning -- Danger Will Robinson! Don't try to load a Driver with the DriverManager class if you are working in an EJB container. Instead, set up a DataSource using the tools provided by your container vendor and use the DataSource to obtain connections. If you use a DriverManager the database driver's connections will NOT be enlisted in EJB transactions... Kyle ------------------ Kyle Brown, Author of Enterprise Java (tm) Programming with IBM Websphere See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
I am running Oracle8 and JDK1.3.1_01. What drivers should I be using, and where might I find the name of it in my directories... I have been running this code: import java.sql.*; import java.math.*; class testJDBC { public static void main(String []args) { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection ("jdbc:oracle:oci8:@mydatabase", "scott", "tiger"); String createTableCoffees = "CREATE TABLE COFFEES " + "(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, " + "SALES INTEGER, TOTAL INTEGER)";
Statement stmt = conn.createStatement(); stmt.executeUpdate(createTableCoffees); }// *********end try catch (Exception e) { e.printStackTrace(); }//**********end catch }//**********end main }//**********end class Is " @mydatabase" the correct syntax, and if not, where do if find the name of my database? I keep getting the error: java.lang.ClassNotFoundException:oracle.jdbc.driver.OracleDriver with an associated stack of errors. If anybody can help out here, I could really use a hand. All I want to do is write to this Oracle DB. Reilly (Editted by Dave to get rid of the smilies) [This message has been edited by David O'Meara (edited November 14, 2001).]
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
This doesn't seem like an EJB question but rather a JDBC question. I am moving this to the JDBC forum where you will get better responses. ------------------ Tom Sun Certified Programmer for the Java� 2 Platform Moderator of the forums: J2EE and EJB Other Java APIs
Thanks alot Jamie! Looks like I have some work to do. Didn't want anyone to solve it for me... just wanted some research resources to do it myself! Thanks again, Reilly
your welcome! took me a while to find these, but now I use them all the time. 90% of my answers to posts come from these links! If you come across any roadblocks, let us know. Jamie
Reilly Morris
Ranch Hand
Joined: Sep 11, 2001
Posts: 44
posted
0
Hi again... Okay, I have the correct driver. Now when I try to create a connection, it keeps throwing an SQL Exception but it is because I am not configuring the JDBC URL correctly. The Thin driver syntax should follow: Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@<mc-name>:<port-no>:<sid>","scott", "tiger"); What is <mc-name>,<port-no> and <sid>, and how would I find out what they are specifically on my local machine. I have no experience with Oracle other than what I have managed to do so far with it in JDBC, and as you can see I am having troubles. Whatever you could volunteer would be great. Reilly- tired and Oracled out (all I want to do is write Java!) (Editted by Dave to fix smilies and angle brackets) [This message has been edited by David O'Meara (edited November 14, 2001).]
Reilly Morris
Ranch Hand
Joined: Sep 11, 2001
Posts: 44
posted
0
Hi again... Okay, I have the correct driver. Now when I try to create a connection, it keeps throwing an SQL Exception but it is because I am not configuring the JDBC URL correctly. The Thin driver syntax should follow: Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@mcname:port:sid","scott", "tiger"); What is mcname, port and sid, and how would I find out what they are specifically on my local machine? I have no experience with Oracle other than what I have managed to do so far with it in JDBC, and as you can see I am having troubles. Whatever you could volunteer would be great. Reilly- tired and Oracled out (all I want to do is write Java!) (Editted by Dave to fix those damn smilies) [This message has been edited by David O'Meara (edited November 14, 2001).]
you may not have to use all the parameters that are specified since it is on your local machine. Usually Oracle is running on a separate server, so you would need to know the domain name/IP address, port number and instance name. Since it is local my guess would be to just use the instance name(name of the database) I would guess this: Connection conn = DriverManager.getConnection ("jdbcracle:thin:@sid","scott", "tiger"); I have also found this technique on google.com: DriverManager.getConnection("jdbcracle:thin:scott/tiger"); I have also seen this: conn = DriverManager.getConnection("jdbcracle:thin:@127.0.0.1:1521:yourDBname","scott","tiger"); So try em out, and see if it works. What is the Exception that is being thrown and what is the URL that you are trying now?
Jamie those damn smilies again! can they have a default of being off for this specific forum?
[This message has been edited by Jamie Robertson (edited November 15, 2001).]
Reilly Morris
Ranch Hand
Joined: Sep 11, 2001
Posts: 44
posted
0
Here is the code I am using, just trying to make a connection and when I java it, it throws the exception: class jdbcPract { public static void main(String []args) { try { //register the driver (this compiles fine) DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver ()); //create connection (once I added this exceptions began) Connection conn = DriverManager.getConnection("jdbc: oracle:thin:@127.0.0.1:1521: oracle","scott","tiger"); //Just create a statement Statement stmt = conn.createStatement(); //Close the "existing" connection(which has yet to happen ) conn.close(); }//try catch(Exception e) { System.out.println(e); }//catch }//main } The code is simply meant to load the driver, then create a connection, then a statement, then close the connection. I didn't want to get into ResultSet creation and trying to send data to the db before I figure out how to get a viable connection. Anyway, the above code throws: java.sql.SQLException: 20 I could find nothing on Google about this error or what it indicates. I have tried all three of your options with the same error. I have spent the last three days getting the driver to load, which seems to work fine now. But now creating this connection is causing more problems. I am reading Java Enterprise in a Nutshell (O'Reilly), and it makes this look so easy on paper. Reilly
Reilly Morris
Ranch Hand
Joined: Sep 11, 2001
Posts: 44
posted
0
More specifically... java.sql.SQLException: 20 at oracle.jdbc.dbaccess.DBError.check_error(DBError.java:407) at oracle.jdbc.driver.OracleConnection.<init>OracleConnection.java:152) at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:214) at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:193) at java.sql.DriverManager.getConnection(DriverManager.java:517) at java.sql.DriverManager.getConnection(DriverManager.java:177) at jdbcPract.main(jdbcPract.java:20)
Originally posted by Jamie Robertson: those damn smilies again! can they have a default of being off for this specific forum?
When you create a post you can check the "Disable Smilies in This Post." checkbox, but otherwise the answer is "No, not really". We've tried a few solutions with the UBB software that Javaranch runs on but no luck so far. It isn't such a problem in other forums so I guess we'll just have to make do here in the JDBC forum Dave.
to capture more useful information on the error try this error catching technique:
I'm going to guess that maybe you don't have permissions for the user "scott" with password "tiger". To ensure that you can use this combination, open a SQL*Plus session using "oracle" as the database and scott and tiger as the username/password combination. Jamie
I also noticed an extra space in your connection url just before your database name: Connection conn = DriverManager.getConnection("jdbc: oracle:thin:@127.0.0.1:1521: oracle","scott","tiger"); by the way, did you name your database "oracle"? Just wondering, I don't want you to think that the field is for the database vendor name. If you create a "payroll" database then you should use Connection conn = DriverManager.getConnection("jdbc: oracle:thin:@127.0.0.1:1521:payroll","scott","tiger"); Jamie
Reilly Morris
Ranch Hand
Joined: Sep 11, 2001
Posts: 44
posted
0
Yeah, the spaces were there to avoid the smilies, and the name of the db is oracle. So other than that, I think I had the syntax correct. I have permissions, firstly it's Oracle personal I jsut installed, and I verified through the SQL*Plus tool. So other than that, I don't know how to proceed. This really does not appear to be that difficult, or at least it shouldn't be. I will try the Exception message method you provided. Chat later, Reilly
there is a little checkbox labeled "Disable Smilies in This Post" that you can check so that you don't have to throw in extra spaces. Anyways, post the error message that you get using the new try/catch procedure I showed you. "20" isn't a very useful error message to work with. You should get "ORA-99999 - some error message" followed by the Stack Trace. This will be much more helpful in telling you what your error is. Jamie
Any luck Reilly? or did you put the project on hold for the time being?
Reilly Morris
Ranch Hand
Joined: Sep 11, 2001
Posts: 44
posted
0
Hi Jamie, Actually, I have just got the Oracle 8i complete reference from Oracel Press. There is a complete section on JDBC configuration, which I will cover tonight. I will let you know how it works out. I still think it is somewhere in the Connection params. Chat soon, Reilly
I will try the Exception message method you provided.
you never did inform us as to what the ORA-XXXXX error number/message was using the getMessage() in your catch block. This can be very helpful in diagnosing the exact problem. try to post this ASAP so we can help rid you of this problem! Jamie