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);
}
}
先生..我在循环播放后没有得到平衡...当我尝试更新它时......它在控制台中显示零值以进行平衡...而它仍然包含该值的值我在创建一个/ c时首先插入... 请告诉我...... console output
答案 0 :(得分: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;
}