Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Sample Java code requested

Re: Sample Java code requested

From: Brad <nospam_at_arach.net.au>
Date: Thu, 13 Nov 2003 10:38:26 +0800
Message-ID: <3fb2eeec@funnel.arach.net.au>


Thanks for quick reply Andrew.

I copied your example and edited it slightly for the Oracle connection side. But am having some problems here. My code is listed below, together with the error that I get when running it.




import java.sql.*;

public class SQLTest
{

    public static void main(String[] args)     {

        String sSQL=""; // Used to build up longer SQL strings in chunks
        try

{
Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn =

DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:PROD","scott", "tiger");
            Statement st = conn.createStatement();
            sSQL = sSQL + "SELECT job FROM emp";
            sSQL = sSQL + " WHERE ename = '" + args[0] + "'";
            ResultSet rec = st.executeQuery(sSQL);
            System.out.println("job:");
            while (rec.next())
            {
                System.out.println(rec.getString(1));
            }
            st.close();
        }
        catch (ClassNotFoundException e)

{
e.printStackTrace(); System.err.println("Caught Exception: " + e.getMessage()); } catch (SQLException e)
{
e.printStackTrace(); System.err.println("Caught Exception: " + e.getMessage()); }

    }
}


It seems to compile OK when I use:

javac SQLTest.java

My CLASSPATH has been set as (I copied the Oracle .zip files into my $HOME directory as .jar files after reading somewhere that sometimes the difference could be in the file extension not being '.jar'???) :

./classes12.jar:./classes111.jar:/apps/oracle/8.1.7/jdbc/lib/classes111.zip: /apps/oracle/8.1.7/jdbc/lib/classes12.zip:/usr/java/jre/lib/rt.jar

And I execute the above using the following:

java -cp . SQLTest TURNER

I get the following error message:

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

        at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:140)
        at SQLTest.main(SQLTest.java:10)
Caught Exception: oracle.jdbc.driver.OracleDriver

Any ideas guys??

Cheers
Brad

"Andrew Jens" <> wrote in message
news:3fb2dd55$0$1746$5a62ac22_at_freenews.iinet.net.au...

> "Brad" <nospam_at_arach.net.au> wrote in message
> news:3fb2b407$1_at_funnel.arach.net.au...
> > Hi there all,
> >
> > I am new to Java and am wondering if anyone can help me with posting
some
> > sample code listings for the following.
> >
> > I would like to be able to perform a SELECT statement against an Oracle
> > database, except passing in the value of my WHERE clause as a paramter
to
> > the java program.
> >
> > For Example:
> >
> > java SimpleSelectJavaCode SMITH
> >
> > which would perform a connection to the database and also a SELECT
> statement
> > against Oracle something similar to ....WHERE ename = 'SMITH'
> >
> >
> > Ultimately, I'd like to have a JSP page taking the input from a HTML
FORM,
> > and passing this to a java bean where the SQL statement is executed and
> the
> > results returned to the browser.
> >
> > Can anyone help me with this?
> >
> > Thanks in advance.
> >
> > Brad
> >
> >
>
> Hi,
> Let me start by saying that I'm no expert - plus I don't know much about
> Oracle, but the following may get you some of the way there. Here is some
> source code in a file called SQLTest.java:
> ==================================================================
> import java.sql.*;
>
> public class SQLTest
> {
>     public static void main(String[] args)
>     {
>         String sSQL=""; // Used to build up longer SQL strings in chunks
>         try
>         {
>             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
>             Connection conn =
> DriverManager.getConnection("jdbc:odbc:Customer","", "");
>             Statement st = conn.createStatement();
>             sSQL = sSQL + "SELECT Firstname FROM Customers";
>             sSQL = sSQL + " WHERE ename = '" + args[0] + "'";
>             ResultSet rec = st.executeQuery(sSQL);
>             System.out.println("Firstnames:");
>             while (rec.next())
>             {
>                 System.out.println(rec.getString(1));
>             }
>             st.close();
>         }
>         catch (ClassNotFoundException e)
>         {
>             e.printStackTrace();
>             System.err.println("Caught Exception: " + e.getMessage());
>         }
>         catch (SQLException e)
>         {
>             e.printStackTrace();
>             System.err.println("Caught Exception: " + e.getMessage());
>         }
>     }
> }
> ==================================================================
> After compilation, this can be run with the command line: java SQLTest
SMITH
>
> This works under M$ Windows with a SystemDSN called Customer pointed at an
> Access Database. I don't see why you couldn't point the Customer DSN at an
> Oracle database instead.
> The above code loops through a result set, however I realise that if ename
> is unique you will only get back one record.
> I'll leave it to someone else to handle the web page version
> Cheers,
> Andrew Jens.
>
>
Received on Wed Nov 12 2003 - 20:38:26 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US