2017-07-14 1 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); 
    } 

}Erste Falsche Ausgabe in mysql und jdbc

sir..i nicht ich erhalte die Waage nach while-Schleife ... und wenn ich versuche zu up-date es ... es zeigt Null-Wert für das Gleichgewicht in der Konsole ... während es enthält noch diesen Wert, was ich während der Erstellung eingefügt, um ein auf dem ersten/c ... plz HLP me ...... console output

mysql workbench o/p

+0

Sie haben eine lokale Variable 'bal' innerhalb der Schleife und wahrscheinlich auch ein lokales Feld' bal'. Auch für das UPDATE: 'ps.setInt (2, acNo);' und so. –

+0

also ... welche Korrektur sollte ich in meinem codeto machen, um das korrekte o/p //// –

+0

zu erhalten, aber ich deklarierte das "bal" als Klassenvariable im Programm ... und ich betrete es ... so Ich denke, dass es als Klasse-Level-Variable an jedem beliebigen Punkt arbeiten kann ... –

Antwort

0

Beispiel Code. Try-with-resources kümmert sich um das Schließen, auch wenn eine Ausnahme ausgelöst wird.

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

Thnk sehr viel Herr ..... für Ihren Gefallen ..... –

+0

Genießen Sie die Programmierung –