2017-07-14 82 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

享受编程 –