i am not able to connect to the oracle database using JDBC

I am trying to perform insert operation in the oracle database thru applet ...i want to enter data in the database thru applet....but i am not able to establish connection to database.... Perhaps there is a problem with the line given below....

Connection con = DriverManager.getConnection("jdbc:oracle:shubh","scott","tiger");

Please suggest me some resolution...

Here is my Code....

import java.applet.*;

import java.awt.event.*;

import java.awt.*;

import java.sql.*;

public class form extends Applet implements ActionListener

{

Button b1=new Button("submit");

TextField f1=new TextField();

Label l1=new Label("enter name");

public void init()

{

setLayout(null);

setBackground(Color.orange);

l1.setBounds(100, 100, 100, 80);

f1.setBounds(200, 130, 100, 20);

b1.setBounds(100, 200, 100, 20);

add(b1);

add(f1);

add(l1);

b1.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==b1)

{

try

{

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

Connection con = DriverManager.getConnection("jdbc:oracle:shubh","scott","tiger");

Statement s = con.createStatement();

s.execute("insert into shubh values ('f1.getText()')");

//System.out.println("One record inserted");

con.close();

}

catch(Exception ex){}

}

}

}

/*<applet code="form" width=600 height=800>

</applet>*/

[1566 byte] By [shubhanshu_javaa] at [2007-11-27 10:54:35]
# 1

Is the machine running the applet capable of resolving the tnsname "shubh"?

I doubt it, at least not in an applet (probably).

Use the <host>:<port>:<sid> version of the connection url rather than just the <tnsname>.

Also, is the DB and the web server hosted from the same server? Because if the DB is located on a different host than the host that delivered the applet, then, per default, the applet will not be able to connect to it. Per default, an applet can only connect back to the host from which it was served.

masijade.a at 2007-7-29 11:51:07 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Connection pool good, DriverManager bad.

That being said, when you do connect,

I think this:

s.execute("insert into shubh values ('f1.getText()')");

should be:

s.execute("insert into shubh ("column1Name") values ('f1.getText()')");

Also, in your catch blocks, you might want to put this so you it prints out what the exception is:

e.printStackTrace();

George123a at 2007-7-29 11:51:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> Connection pool good, DriverManager bad.

A connection pool is not normally available to an applet, and even if it were, a DriverManager would still be involved somewhere (he just wouldn't have to write it himself, the connection pool software would), and so the connection url problem still exists.

masijade.a at 2007-7-29 11:51:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

"A connection pool is not normally available to an applet,"

Correction then:

Applet bad, separate persistent layer good.

George123a at 2007-7-29 11:51:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

I think what we all mean is "Connection to DB from an Applet bad"

SoulTech2012a at 2007-7-29 11:51:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

Both of which still does not solve his actual problem, which, aside from a poor design choice, is that the connection url needs to be fixed. But until he tries that recommendation, and reports on how it went, he can not be helped any further.

masijade.a at 2007-7-29 11:51:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

Hi masijade,

Sorry for late reply...how can I come to know that my machine is running on applet capable of resoving the TNS name ....

I am running DB and webserver hosted from the same server...

shubhanshu_javaa at 2007-7-29 11:51:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

Hi George,

I have tried what u suggested....but it is still not able to connect to database...even i am not able to connect to databse through a simple java code....i dont know ...what is wrong with my code....please suggest me some solution....

shubhanshu_javaa at 2007-7-29 11:51:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9

If i am able to understand you guys....u mean to say that it is not possibe to connect DB with an applet...is it so....please tell me...

shubhanshu_javaa at 2007-7-29 11:51:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10

Dear masijade,

I have designed this applet on a test basis ....i just wanted to know whether i would be able to connect an applet with the databse or not...if it is poor design ..than please some suggestion also...on how to design an applet properly....coz i am gonna design an application through applet....it means a lot to me...i will be highl thankfull to u...

shubhanshu_javaa at 2007-7-29 11:51:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 11

You don't need to worry about whether or not the host where the applet runs can resolve the tnsname if you use the "<host>:<port>:<sid>" version of the connection url rather than just the "<tnsname>" version, as I said in my first reply.

It is perfectly okay to connect to a DB from applet (not necessarily a good practice, but there is nothing technically wrong with it). The DB needs to running on the same server that hosted the applet, however. Either that or we have to get into things such as SecurityPolicies and Signed Applets, both of which I don't believe you are ready for.

So, make that connection url change and try again.

masijade.a at 2007-7-29 11:51:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 12

hi shubhanshu_java ,

there are two things where you are going wrong..

1]Its not recommended to use applet to connect to database.

2]The format of URL that you have used to get connection is not correct.

well..the solution for first problem would be to use a servlet between your applet and database.

But since your code is just for practice...its ok to use applet

now the solution for the second problem is that you should URL inthis format to get connectio

"http://10.0.6.54:8080/***********"

and also while accesing you applet through you browser you should should type the address in similar format and not like "http://loalhost:8080"->this wont work with applet

Mr.VijayDesai@OPSPLa at 2007-7-29 11:51:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...