回答

收藏

检查Java数据库中是否存在值(Accountnumber)

技术问答 技术问答 267 人阅读 | 0 人回复 | 2023-09-12

我在名为save该类有以下方法,正在使用中JDBC保存和查看数据库Java应用程序中的数据。
8 d9 P! N( R5 H# s# s, R: P我的数据库 ZaiLab中    有SAVINGS表,包括以下字段,
" P  ?- `1 ?' ^- R$ \+ Y* s) v: eID,ACCOUNTNUMBER,CUSTOMERNAME,BALANCE,MINMUM)用户将使用JOPtionPane输入以下值。  D2 S9 M6 l4 `8 c1 L" m4 l
(id,accountNumber,customername,balance,minmum);然后,应用程序应检查用户输入的 accountNumber
( g- {3 V. c; T' J7 I是否已经存在;如果不存在,则应将记录保存到表SAVINGS中;如果是,应显示相应的信息。账户已经存在。: L- v: u- i' p
public void openSavingsAccount(int Id,int Amount)    try        String host = &quot;jdbc:derby://localhost:1527/ZaiLab&quot;;        String uname = &quot;siduduzo&quot;;        String upass = &quotassword01&quot;;        Connection con = DriverManager.getConnection(host,uname,upass);        Statement stmt = con.createStatement();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;String SQL = &quot;SELECT * FROM SAVINGS&quot;;        ResultSet rs = stmt.executeQuery(SQL);        int minmum =   balance = minmum;        while (rs.next())            int acc_col = rs.getInt(&quot;ACCOUNTNUMBER&quot;);            if (acc_col == accountNumber)                JOptionPane.showMessageDialog(null,&quot;Sorry,account &quot;   accountNumber                          &quot; aready Exist&quot;);        else if (Amount < minmum)                    JOptionPane.showMessageDialog(null,&quot;Can not Open the Account,Minimum amount to deposit must be R1000&quot;);        else                    balance = balance   Amount;                id = Id;                stmt.executeUpdate(&quot;INSERT INTO `SAVINGS`(ID,ACCOUNTNUMBER,CUSTOMERNAME,BALANCE,MINMUM)VALUE ('&quot;   id   &quot;','&quot;   accountNumber   &quot;','&quot;   customername   &quot;',&quot;   balance   &quot;,'&quot;   minmum   &quot;')&quot;);                  catch (SQLException err)              System.out.println(err.getMessage();               
) [6 M+ r# q5 I: M7 ^, `    解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则