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: Andrew Jens <andrewjens_at_hotmail.com>
Date: Thu, 13 Nov 2003 12:25:41 +1100
Message-ID: <3fb2dd55$0$1746$5a62ac22@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 - 19:25:41 CST

Original text of this message

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