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>*/
# 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.
# 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();
# 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.
# 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