2012-03-19 11 views
0

我需要在插入另一個表成功後自動更新表。 我正在使用準備好的語句來做到這一點,並嘗試了幾種方法來完成工作。有人可以幫我解決這個問題。代碼給出以下在java中插入成功後自動更新

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

對我來說,執行得到p.executeUpdate後stucks();

+0

你是什麼意思卡住?有什麼樣的錯誤或什麼? – Danny 2012-03-19 13:18:55

回答

2

你不執行第一預備語句

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

你只是簡單地覆蓋它,並執行第二個。

1

您在此處發佈的內容不起作用,因爲您正在重複使用變量p。您爲插入設置語句,然後將其替換爲更新語句,然後執行該操作。您從不執行插入語句。

爲了解決這個問題,無論是做p=con.prepareStatement("update loan set t_os=t_os-? where accno=?");,或(更好)之前調用p.executeUpdate();,使用不同的變量兩種說法,並呼籲兩國executeUpdate()

0
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 
    } 
+0

再次您的問題不清楚...是表相關? – mykey 2012-03-19 16:11:47

相關問題