Java JDBC不适用于数据插入?
我从另一个JFrame类调用的JFrame的用户输入中插入数据。我已经在Oracle 11g express edition中检查了该查询,它显示没有插入任何数据。任何人都可以通过在此代码中添加main方法来修改该查询Java JDBC不适用于数据插入?,java,oracle,jdbc,Java,Oracle,Jdbc,我从另一个JFrame类调用的JFrame的用户输入中插入数据。我已经在Oracle 11g express edition中检查了该查询,它显示没有插入任何数据。任何人都可以通过在此代码中添加main方法来修改该查询 class Insert extends JFrame{ JLabel name,code,address,telephone,email; JTextField tname,tcode,ttelephone,temail; JButton save,ex
class Insert extends JFrame{
JLabel name,code,address,telephone,email;
JTextField tname,tcode,ttelephone,temail;
JButton save,exit;
JTextArea taddress;
Insert(){
//properties of frame
super("CUSTOMER MASTER");
setVisible(true);
setLayout(null);
setSize(1100,500);
setResizable(false);
//Initialisation
name=new JLabel("Name:");
code=new JLabel("Code:");
address=new JLabel("Address:");
telephone=new JLabel("Telephone:");
email=new JLabel("E-mail:");
tname=new JTextField();
tcode=new JTextField();
taddress=new JTextArea(4,20);
ttelephone=new JTextField();
temail=new JTextField();
save=new JButton("SAVE");
exit=new JButton("EXIT");
//settingBounds
name.setBounds(10,50,70,25);
tname.setBounds(90,50,150,25);
code.setBounds(300,50,70,25);
tcode.setBounds(380,50,150,25);
address.setBounds(10,100,70,25);
taddress.setBounds(90,100,150,75);
taddress.setBorder(BorderFactory.createLineBorder(Color.BLACK));
telephone.setBounds(300,100,70,25);
ttelephone.setBounds(380,100,150,25);
email.setBounds(10,225,70,25);
temail.setBounds(90,225,150,25);
save.setBounds(900,425,100,25);
exit.setBounds(1000,425,100,25);
//adding on frame
add(name);
add(tname);
add(code);
add(tcode);
add(address);
add(taddress);
add(telephone);
add(ttelephone);
add(email);
add(temail);
add(save);
add(exit);
//ActionListener
save.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
//getting data from user
String demail,checkdemail;
int dcode,dtelephone;
String dname=tname.getText();
String daddress=taddress.getText();
checkdemail=temail.getText();
demail=validateEmail(checkdemail);
try{
String stelephone=ttelephone.getText();
dtelephone=Integer.parseInt(stelephone);
String scode=tcode.getText();
dcode=Integer.parseInt(scode);
addData(dname,dcode,daddress,dtelephone,demail);
}catch(Exception e){
e.printStackTrace();
}
dispose();
new CustomerMaster();
}
});
exit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
dispose();
new CustomerMaster();
}
});
}
public void addData(String name,int code,String address,int telephone,String email){
Connection con;
String str;
PreparedStatement ps;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","PROJECT","PROJECT");
str="insert into CUSTOMERMASTER values(?,?,?,?,?)";
ps=con.prepareStatement(str);
ps.executeUpdate();
ps.setString(1,name);
ps.setInt(2,code);
ps.setString(3,address);
ps.setInt(4,telephone);
ps.setString(5,email);
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
private String validateEmail(String email){
String vemail=null;
Pattern p1=Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"+"[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
Matcher m1=p1.matcher(email);
Boolean b=m1.matches();
boolean cb=b;
if(email.equals("")){
vemail=email;
}
else{
if(b==true){
vemail=email;
}
else{
Object message="Enter Valid Email.";
JOptionPane.showMessageDialog(null,message);
}
}
return vemail;
}
}
下面是SQL开发人员生成的数据库代码
代码:
假设正确调用了
addData
方法,ps.executeUpdate()代码>语句应在设置值后调用,即在ps.setString(5,电子邮件)之后调用代码>代码中的行。您的addData()方法应该是这样的顺序(我已经删除了集群代码)
我发现它没有将值存储在我在获取文本时声明的变量中。在这里,我介绍了问题所在的侦听器的操作。
代码
//ActionListener
save.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
//getting data from user
try{
String demail,checkdemail;
int dcode,dtelephone;
String dname=tname.getText();
String daddress=taddress.getText();
checkdemail=temail.getText();
demail=validateEmail(checkdemail);
String stelephone=ttelephone.getText();
dtelephone=Integer.parseInt(stelephone);
String scode=tcode.getText();
dcode=Integer.parseInt(scode);
System.out.println(dname+daddress+demail+dtelephone+dcode);
addData(dname,dcode,daddress,dtelephone,demail);
}catch(Exception e){}
}
});
如果您实际执行插入,插入可能会工作得更好:ps.executeUpdate()
您需要在关闭连接之前执行commit()。顺便说一句,您的ps.setXXX()是在ps.executeUpdate()之后完成的代码>这显然是错误的。亲爱的先生,我已经这样做了,并通过添加主函数来执行它,然后它也没有将数据插入到database@Karangupta两件事:首先,您需要在关闭连接之前执行准备好的语句,即ps.executeUpdate();con.close()代码>。其次,我建议不要在每次查询执行时打开和关闭连接,因为这样会很慢。最好打开连接,重复使用它,直到数据库处理完毕,然后关闭连接。@Karanguptan并移动了ps.executeUpdate()代码>ps.setString后的声明(5,电子邮件)代码>?好的,先生,我也会这么做。在上面的代码中,我还添加了数据库代码。@JamesB先生,我发现了错误,感谢大家的帮助。没有为类型java.sql.Connection
调用execute的方法-也许你的意思是con.commit()
?这不应该是必需的,因为默认情况下连接处于自动提交模式,所以ps.executeUpdate()
应该足以提交事务。
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","PROJECT","PROJECT");
str="insert into CUSTOMERMASTER values(?,?,?,?,?)";
ps=con.prepareStatement(str);
ps.setString(1,name);
ps.setInt(2,code);
ps.setString(3,address);
ps.setInt(4,telephone);
ps.setString(5,email);
ps.executeUpdate();
con.commit();
ps.close();
con.close();
} catch(Exception e){
e.printStackTrace();
}
//ActionListener
save.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
//getting data from user
try{
String demail,checkdemail;
int dcode,dtelephone;
String dname=tname.getText();
String daddress=taddress.getText();
checkdemail=temail.getText();
demail=validateEmail(checkdemail);
String stelephone=ttelephone.getText();
dtelephone=Integer.parseInt(stelephone);
String scode=tcode.getText();
dcode=Integer.parseInt(scode);
System.out.println(dname+daddress+demail+dtelephone+dcode);
addData(dname,dcode,daddress,dtelephone,demail);
}catch(Exception e){}
}
});