2017-07-14 61 views
0
public void Deposite() throws Exception 
{ 
    try 
    { 
     Class.forName("com.mysql.jdbc.Driver"); 

     String url = "jdbc:mysql://localhost:3306/bank"; 

     Connection con = DriverManager.getConnection(url,"root","admin"); 

     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.print("Enter your A/c no. : "); 
     acNo = Integer.parseInt(br.readLine()); 

     String sql = "SELECT Name,Ac_No,Balance FROM CUSTOMER WHERE Ac_No=?"; 
     PreparedStatement ps = con.prepareStatement(sql); 
     ps.setInt(1,acNo); 

     ResultSet rs = ps.executeQuery(); 

     while(rs.next()) 
     { 
      String name = rs.getString("Name"); 
      int acNo = rs.getInt("Ac_No"); 
      float bal = rs.getFloat("Balance"); 

      System.out.println(" "+name+"  "+acNo+"  "+bal); 
     } 
     System.out.println("Current Bal : "+bal); 

     BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.print("Enter Deposite Amt : "); 
     amt = Float.parseFloat(br1.readLine()); 

     bal = bal + amt; 
     //System.out.println("Current Bal : "+bal); 

     String sql1 = "UPDATE CUSTOMER SET Balance = ? WHERE Ac_No =?"; 
     ps = con.prepareStatement(sql1); 
     ps.setInt(1,acNo); 
     ps.setFloat(2,bal); 
     int i = ps.executeUpdate(); 
     System.out.println("New Balance updated.... "+i); 
     System.out.println("Transaction Successful...."); 

     ps.close(); 
     con.close(); 
    } 
    catch(Exception e) 
    { 
     System.out.println(e); 
    } 

}獲取輸出錯誤在MySQL和JDBC

sir..i我沒有得到平衡後while循環......當我嘗試了最新它...它顯示零值在控制檯的平衡......而它仍然包含創建過程中的值就是我插在第一/ C ... PLZ HLP我...... console output

mysql workbench o/p

+0

你在循環中有一個局部變量'bal',並且可能還有一個本地字段'bal'。也用於UPDATE:'ps.setInt(2,acNo);'等等。 –

+0

所以...我應該在鱈魚eto中做出什麼樣的修正才能得到正確的o/p //// –

+0

但我在程序中聲明瞭「bal」作爲類變量......並且我正在訪問它...所以我認爲它可能在埃夫裏點作爲課堂級別變量工作... –

回答

0

示例代碼。即使發生異常,Try-with-resources也會關閉關閉。

static class Customer { 
    int acNo; 
    String name; 
    BigDecimal bal; 
} 

public static void main(String[] args) 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.print("Enter your A/c no. : "); 
    int acNo = Integer.parseInt(br.readLine()); 

    String url = "jdbc:mysql://localhost:3306/bank"; 
    try (Connection con = DriverManager.getConnection(url, "root", "admin")) { 
     Customer customer = loadCustomer(acNo); 
     if (customer == null) { 
      System.out.println("Wrong account"); 
     } else { 
      System.out.printf("Current balance for %s, account %d: %12.2f%n", 
       customer.name, customer.acNo, customer.bal); 
     } 
    } catch (SQLException e) { 
     e.printStacktrace(); 
    } 
} 

private static Customer loadCustomer(int accNo) throws SQLException { 

    String sql = "SELECT Name, Balance FROM CUSTOMER WHERE Ac_No = ?"; 
    try (PreparedStatement ps = con.prepareStatement(sql)) { 
     ps.setInt(1, acNo); 

     try (ResultSet rs = ps.executeQuery()) { 
      if (rs.next()) { 
       Customer customer = new Customer(); 
       customer.acNo = acNo; 
       customer.name = rs.getString(1); 
       customer.bal = rs.getBigDecimal(2); 
       return customer; 
      } 
     } 
    } 
    return null; 
} 
+0

非常多先生.....爲你的青睞..... –

+0

享受編程 –