简体   繁体   中英

while trying to save record into Oracle 10g using java Swing the program terminates without executing

Hi, I'm trying to save the record into Oracle 10g using the Java swing program. But when I run the program it is immediately terminated

import java.awt.event.*;

import java.sql.*;

@SuppressWarnings("serial")
class Insert extends JFrame implements ActionListener
 {

JLabel label,label1,label2,label3,label4,label5;
JTextField tf1,tf2,tf3,tf4,tf5;
JButton btn;
Connection con;

Insert()
{
    super("Inserting employee records");

        label1 = new JLabel("empID:");

        label1.setBounds(20,20,100,20);

        tf1 = new JTextField(50);

        tf1.setBounds(130, 120, 200, 20);

        label2 = new JLabel("employeeName:");

        label2.setBounds(20, 150, 100, 20);

        tf2 = new JTextField(100);

        tf2.setBounds(130, 150, 200, 20);

        label3 = new JLabel("Gender:");

        label3.setBounds(20, 180, 100, 20);

        tf3 = new JTextField(50);

        tf3.setBounds(130, 180, 200, 20);

        label4 = new JLabel("DOB:");

        label4.setBounds(20, 210, 100, 20);

        tf4 = new JTextField(50);

        tf4.setBounds(130, 210, 100, 20);

        label5 = new JLabel("DOJ");

        label5.setBounds(130, 240, 100, 20);

        tf5 = new JTextField(50);

        tf5.setBounds(130,270,100,20);

        btn = new JButton("Submit");

        btn.setBounds(130, 300, 100, 20);

        btn.addActionListener(this);

        add(label1);
        add(tf1);
        add(label2);
        add(tf2);
        add(label3);
        add(tf3);
        add(label4);
        add(tf4);
        add(label5);
        add(tf5);
        add(btn);

        tf1.setEditable(false);
        tf5.setEditable(false);

}



public void actionPerformed(ActionEvent ae)
{
    String id = tf1.getText();
    String name = tf2.getText();
    String gender = tf3.getText();
    String dob = tf4.getText();


try
{
    String url="jdbc:oracle:thin:@localhost:1521:XE";
    String u="ems2";
    String p="ems2";
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection(url,u,p);


    PreparedStatement st = con.prepareStatement("insert into employee"+"(emp_id,"+"employeename,"+"gender,"+"dob,"+"doj)"+"values(?,?,?,?,?)");
    st.setString(1, id);
    st.setString(2,name);
    st.setString(3,gender);
    st.setString(4, dob);
    st.executeUpdate();
    JOptionPane.showMessageDialog(null, "Data is successfully inserted into database");
    con.close();

}

catch(Exception ex)
{
    System.out.println(ex);
}
}
public static void main(String [] args)
{
    new  Insert();
}
}e

Your are not showing you insert JFrame . try to do:

new Insert().setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM