0

I am trying to design an applet that stores data in an Oracle database.

There is no compilation error, but when I try to insert the record by clicking the ADD button, it throws an exception:

oracle.driver.OracleDriver

This is my applet code:

import java.applet.*;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
/*<applet code=registration width=400 height=400></applet>*/
public class registration extends Applet implements ActionListener
{
    Label name;
    TextField txt_name;
    Button btn_add;
    Connection con;
    PreparedStatement pstmt;

    public void init()
    {
        setLayout(null);
        name=new Label("Name");
        name.setBounds(10,20,50,20);
        add(name);

        txt_name=new TextField(20);
        txt_name.setBounds(80,20,120,20);
        add(txt_name);

        btn_add=new Button("ADD");
        btn_add.setBounds(10,50,50,20);
        add(btn_add);

        btn_add.addActionListener(this);
    }//end of init

    public void actionPerformed(ActionEvent e)
    {
      if(e.getSource()==btn_add) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","system");

            pstmt=con.prepareStatement("insert into test values(?)");
            pstmt.setString(1,txt_name.getText());
            pstmt.executeUpdate();
            System.out.println("saved");
        } catch(Exception c) {
            System.out.println(c.getMessage());
        }
      }
   }
}
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Aditya Jadhav
  • 71
  • 1
  • 2
  • 9
  • Could you post more information about the exception, for instance, the exception class, and the full exception message. Try to add a break point in the catch statement, and inspect the exception object `c` – Matthew Sainsbury Aug 03 '15 at 16:39
  • I tried to print a message before the try { }, it worked, but the problem comes when it enters the try block.The exception show driver problem,I aslso tried to print a message after the con. object, ,the message is not printed, there might be some driver loading problem.... – Aditya Jadhav Aug 03 '15 at 16:48
  • Do you have the jdbc driver in the classpath? – Daniel Jipa Aug 03 '15 at 16:52
  • 1) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. 2) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). .. – Andrew Thompson Aug 05 '15 at 11:21
  • .. 3) Change `} catch(Exception c) { System.out.println(c.getMessage());` to `} catch(Exception c) { c.printStackTrace(); System.out.println(c.getMessage());` for the full stack trace. – Andrew Thompson Aug 05 '15 at 11:21

1 Answers1

0

Check the following:

  • DB Driver is in your applet classpath.
  • You can only make DB connection to the same server that applet is loaded from.
aguser555
  • 1
  • 1
  • I am not getting it, can you please give me some details..I have set the class path of database driver as....D:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar; – Aditya Jadhav Aug 04 '15 at 15:06
  • You need to add this Jar ojdbc14.jar; and any needed jars in server path, and refer to it from Applet definition. For example: – aguser555 Aug 07 '15 at 18:27