嗨,我正在尝试使用Java swing程序将记录保存到Oracle 10g中。但是当我运行该程序时,它会立即终止
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
答案 0 :(得分:1)
您没有显示插入JFrame
。尝试做:
new Insert().setVisible(true);