简体   繁体   English

在Java中插入成功后自动更新

[英]update automatically after insert is successful in java

i need to update a table automatically after the insert of another table is successful. 插入另一个表成功后,我需要自动更新一个表。 i am using prepared statement to do this, and tried couple of ways to do but dint work out. 我正在使用准备好的语句来执行此操作,并尝试了几种方法来完成工作,但努力工作。 can some one please help me out on this. 有人可以帮我这个忙吗? the code is given following 代码如下

            try
                {   
                    p=con.prepareStatement("insert into receipt values(?,?,?,?,?,?,?,?,?)");
                    p.setInt(1, rn);
                    p.setDate(2, new java.sql.Date(rd.getTime()));
                    p.setInt(3, ag);
                    p.setInt(4, an);
                    p.setString(5, name);
                    p.setString(6, street);
                    p.setString(7, city);
                    p.setInt(8, pinno);
                    p.setInt(9, ar);
                    p=con.prepareStatement("update loan set t_os=t_os-? where accno=?");
                    p.setInt(1, ar);
                    p.setInt(2, an);
                    p.executeUpdate();

                    /*try
                    {
                        p=con.prepareStatement("update loan set t_os=t_os-? where accno=?");
                        p.setInt(1, Integer.parseInt(art.getText()));
                        p.setInt(2, Integer.parseInt(ant.getText()));
                        p.executeUpdate();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }*/

                    status.setForeground(Color.BLUE);
                    status.setText("Successfully Added");
                }
                catch (Exception e) 
                {
                    e.printStackTrace();
                    status.setForeground(Color.RED);
                    status.setText("Enter proper values");
                }

for me it execution get stucks after p.executeUpdate(); 对我来说,它在p.executeUpdate()之后执行陷入困境。

You don't execute the first prepared statement 您不执行第一个准备好的语句

  p.setInt(9, ar);
  //MISSING: p.executeUpdate();
  p=con.prepareStatement("update loan set t_os=t_os-? where accno=?");

You just overwrite it and execute the second one. 您只需覆盖它并执行第二个。

You should be using transactions for this: http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html 您应该为此使用事务: http : //docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html

try {
 con.setAutoCommit(false);

 p=con.prepareStatement(....)
 ....
 p.executeUpdate();

 //Second statement
 p=con.prepareStatement(....)
 ....
 p.executeUpdate();

 con.commit();

 catch(..) {
  con.rollback();
 }

What you've posted here won't work, because you're reusing the variable p . 您在此处发布的内容无效,因为您正在重用变量p You set up the statement for the insert, then replace it with the statement for the update, then execute that. 设置插入语句,然后将其替换为更新语句,然后执行该语句。 You never execute the statement for the insert. 您永远不会执行插入语句。

To fix that, either call p.executeUpdate(); 要解决此问题,请调用p.executeUpdate(); before doing p=con.prepareStatement("update loan set t_os=t_os-? where accno=?"); 在执行p=con.prepareStatement("update loan set t_os=t_os-? where accno=?"); , or (better), use different variables for the two statements, and call executeUpdate() on both. ,或(更好),对这两个语句使用不同的变量,然后对两个语句都调用executeUpdate()

you can first successfully execute the first query then you continue to the update...
you can use


    try{
    //work first execution
    ///while true
    try{

 //update code
    }finally{


    }
    }finally{
//close resources
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 成功批量插入后更新Kafka提交偏移 - Update Kafka commit offset after successful batch insert 插入后如何自动更新视图 - How to update automatically view after insert 如何使用Java和MySQL确定插入或更新是否成功? - How do you determine if an insert or update was successful using Java and MySQL? 如何使用Java SDK查看dynamoDB中的更新或插入是否成功? - how do I find out if an update or insert was successful in dynamoDB using the Java SDK? 注册成功后自动登录用户 - Login user automatically after successful registration 成功登录后会自动重新创建JSESSIONID - JSESSIONID is getting recreated automatically after successful login maven更新后Java版本自动更改为java 1.5 - Java version automatically change to java 1.5 after maven update Java和HSQLDB,如何在更新后自动添加列 - Java and HSQLDB, how to add column automatically after update SQLite Java:自动删除项目后更新表ID - SQLite Java: update table ID's after deleting item automatically 即使在IE9中成功安装了JRE 7 update 9之后,测试Java applet仍在Windows 7中启动 - test java applet is getting started in windows 7 even after successful install of JRE 7 update 9 in IE9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM