Can any one please tell me how to connect a simple java web application to Oracle10g Express edition?
Which driver should I use?
Thanks in advance.

Recommended Answers

All 8 Replies

i think mostly type-4 driver is best to use for connecting to databse"

Do you mean Oracle in XE or / Microsoft ODBC for Oracle?

if you use the bellow statement for connecting to database (in the above given url)

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

that says you are using type-1 driver which is provided by Microsoft ODBC for Oracle

incase, if you use the bellow statement for connecting to database

Class.forName("oracle.jdbc.driver.OracleDriver") 

that says you are using type-4 driver (ojdbc.jar and ojdbc14.jar files are needed to connect to database) which comes with Oracle10g Express Edition

note : check bin folder of oracle10g installation for those jar files

in java there are 4 ways to connect to database . the following url explains description of all the 4 ways

http://www.roseindia.net/jdbc/jdbc-driver-and-its-types.shtml

let me know if you have any doubts in my clarification

happy coding

Well, when I am using type4 driver using the following code:

import java.sql.*;
public class jbc
{
    public static void main(String args[])
    {
        int i=0;
        try
        {
            String str="Select * from adi";
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:driver:OracleDriver:ff","SYSTEM","monika");
            System.out.println("Connection open");
            Statement stmt=con.createStatement();
            ResultSet rs= stmt.executeQuery(str);
            System.out.println("first Name");
            while(i<3)
            {
                rs.next();
                String lname=rs.getString("name");
                System.out.println(lname);
                i++;
            }
            con.close();
        }
        catch(Exception ex)
        {
           ex.printStackTrace();
        }
    }
}

It is giving me the same error :

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at jbc.main(jbc.java:10)
BUILD SUCCESSFUL (total time: 2 seconds)

While . when I tried using type 1 driver, the code is running fine only for retrieving values from the table i.e only for **Select ** queries. If I make certain changes to
the line

     String str="Insert into adi values('ram')";

then it is giving the following error:

run:
Connection open
java.sql.SQLException: No ResultSet was produced
    at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:259)
    at jbc.main(jbc.java:14)
BUILD SUCCESSFUL (total time: 3 seconds)

Please help me..:(

For inserting values in the table through code, I have modified lines as was doing a little mistake of not updating it.

  try
        {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con =     DriverManager.getConnection("jdbc:odbc:sd","SYSTEM","monika");
            System.out.println("Connection open");
            Statement stmt=con.createStatement();
            String str1="INSERT INTO adi (name) values ('Ram') ";
            stmt.executeUpdate(str1);
            String str2="Select * from adi";
            ResultSet rs2= stmt.executeQuery(str2);
            System.out.println("first Name");
            while(i<3)
            {
                rs2.next();
                String lname=rs2.getString("name");
                System.out.println(lname);
                i++;
            }

But still i t is not working.It is only retrieving the already saved contents of the table.

Connection open
first Name
monika
aditya
shonu
BUILD SUCCESSFUL (total time: 2 seconds)

hai adikimicky

there is no mistakes in the program except some logical mistakes which is unknown to you

i think you are in initial stages of learning concept. no problem

please change the while loop (in the previous post) as follows

while(rs2.next())
{
String lname=rs2.getString("name");
System.out.println(lname); 
}

so that program will run as you expects

what you have done is you fixed the while loop for running 3 times only thats the reason you got 3 names in the output

all remaining things are good

check it once and let me know the status

happy coding

in one of the previous post

you said you are using type-4 driver

but in the coding part you got the connection object like as follows

Connection con = DriverManager.getConnection("jdbc:driver:OracleDriver:ff","SYSTEM","monika");

but if you use type_4 driver the connection string should be like as follows

Connection con = DriverManager.getConnection("jdbc:driver:thin","SYSTEM","monika");

no need of OracleDriver:ff simply remove that

and then also you got the ClassNotFoundException

for this you need to set (ojdbc.jar file and ojdbc14.jar ) files to your current classpath

i told you that thing also please hava alook at previous post so that you get the proper reason for the problem you have

happy coding

Hey thanks a lott madam.
it worked by using type 1 driver.
Thanks a lott..:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.