mardi 4 août 2015

inserting records in database through applet

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());
        }
      }
   }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire