File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Data from Oracle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Data from Oracle" Watch "Data from Oracle" New topic
Author

Data from Oracle

Navi Singh
Ranch Hand

Joined: Jul 24, 2002
Posts: 80
Hi! Guys,
Simple Q.
Code for extracting data from a table(emp)
from a servlet but have to use Oracle driver.
I did get them by using :
Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:somedb","user","passwd");
But I want by something like:
Class.forName("oracle.jdbc.driver.oracleDriver")
con=DriverManager.getConnection("jdbc racle:thin bhost:1528 rcl","scott","tiger");
This Is what i get:--
SQLException has OccurredNo suitable driver .
Thanks.
[ December 11, 2002: Message edited by: Parvin Palia ]
William Wild
Greenhorn

Joined: Dec 10, 2002
Posts: 23
Ok.
You have to connect to oracle in a slightly different way. This is the code I use to connect and the list of .jars you should use to access oracle db's. If memory serves these are for every oracle upto 9i + their XMLDB.
ojdbc14.jar (If using sun java1.4) This will handle the connection below. Check out the java doc that comes with the package (from http://ont.oracle.com/software/content.html)
There are several developers packages there to handle sql, sqlx and xquery requests to the db as well.


If you need anymore info on this, just ask.
Cheers
Bill


--<p>Bill<p>"Make it idiot proof,<br /> and someone will make a better idiot" -- ANON
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Wild Bill",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Thanks.
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Wild Bill:

I don't like this, since the code is no longer portable and this is not the purpose behind the JDBC framework.
Class.forName(...) is the preferred way to register drivers with the DriverManager, rather than instantiating a Driver.
Also, it is preferable not to cast back to the concrete classes, it is better to use the JDBC Interfaces.
I'm suspecting that your problems are related to the way Oracle package their classes. They zip rather than jar the files, and the ClassLoader doesn't look inside zip files and therefore doesn't find them.
It used to be possible to rename the classesXX.zip file to classesXX.jar, but understand this is no longer possible. You have to unzip then re-jar the files to be able to see them.
Depending on the server you are using, you may need to move the jar to a specific directory for it to be found. People think that adding it to the CLASSPATH is enough, but Application Servers don't use this, the files have to be inspecific directories and this determines the scope of the files.
Dave.
William Wild
Greenhorn

Joined: Dec 10, 2002
Posts: 23
Dave,
I had my code the same (almost) as Parvin's with the .zip as a .jar and it still didn't work. That's why I chose to be type specific with the driver. Could you not make a general connection to the db, check it's type (oracle, postgresql, infomix etc) and then chose the connection method accordingly?
Cheers
Bill
Navi Singh
Ranch Hand

Joined: Jul 24, 2002
Posts: 80
Thank's again for the info,

1)But the link which you(Bill) have given me is either not there anymore or ...something is wrong with the isp hosting it.
Another thing (i am really really juvenile to the concepts of programming)
.. U guy's were discussing on the *.jar and *.zip, Now what difference does it make :i am confused:
2)CODE FOR UPDATING DATABASE:--
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:info","scott","tiger");
stm=con.createStatement();
System.out.println()
stm.executeUpdate("UPDATE EMP SET HIREDATE=SYSDATE WHERE ENAME='ADAMS';");
catch(ClassNotFoundException e)
{
out.println("Could not Load Database Driver"+e.getMessage());
}
catch(SQLException e)
{
out.println("SQLException has Occurred"+e.getMessage());

}
finally
{
try
{
if(con!=null)con.close();
}
catch(SQLException ignore)
{
}
}
}
}
--CODE ENDS .
I am trying to update database thru servlet,but
it gives error
Internal Server Error
java.lang.IllegalAccessException: updat
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java:232)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:886)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:655)
at org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:415)
at org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)
at java.lang.Thread.run(Thread.java:479)
--------------------------------------------------------------------------------
Navi Singh
Ranch Hand

Joined: Jul 24, 2002
Posts: 80
Hi! Guys,
i have the answer to the problem (the second part)i.e connecting to the database and updating it.
I did not mention the servlet class as Public.
--- That's a bit of a relief.

The other part of the q still remains:
Using the Oracle driver...???
Navi Singh
Ranch Hand

Joined: Jul 24, 2002
Posts: 80
Hey Hellooooooo,
Ladies and Gentlemen don't forget me please!.
--Ur's Humbly.
 
Consider Paul's rocket mass heater.
 
subject: Data from Oracle
 
Similar Threads
Tomcat and Oracle, will i transfer any file?
DSN connection
newbie to both java and oracle.. how do i ...
Not connecting to database through log4j
types of drivers