如何从java

时间:2018-05-03 20:20:39

标签: java mysql

我正在尝试从列中检索特定的整数值' Balance'在我的数据库中。但是,我继续在try-catch中捕获异常,这是输出;

有例外!
在开始结果集之前

这是我的代码。任何建议都将不胜感激。

 private void OKbtnActionPerformed(java.awt.event.ActionEvent evt) 
{         

    try{
        String password = PassJP.getText();
        int passint = Integer.parseInt(password);
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/banking application","root","");
              String query = "SELECT Balance FROM user WHERE Password =" +passint;

  Statement st = conn.createStatement();
  ResultSet rs = st.executeQuery(query);
    int balance = rs.getInt("Balance");

     String WAmmount = WithAmmTF.getText();
    int WAmmountint = Integer.parseInt(WAmmount);

    int newbalance = balance - WAmmountint;
        Statement s=conn.createStatement();
s.executeUpdate("INSERT INTO `user`(Balance) VALUE ('"+newbalance+"') where Password="+ passint);

    conn.close();

    }
        catch (SQLException e) { 
        System.err.println("Got an exception! "); 
        System.err.println(e.getMessage()); 
    }        

编辑:

我也试过添加一个while循环,但这又带来了另一个错误:

有例外!
您的SQL语法有错误;查看与您的MariaDB服务器版本对应的手册,以便在'其中使用密码= 111'在第1行

这是代码

 private void OKbtnActionPerformed(java.awt.event.ActionEvent evt) 
{         

    try{
        String password = PassJP.getText();
        int passint = Integer.parseInt(password);
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/banking application","root","");
              String query = "SELECT Balance FROM user WHERE Password = "+passint;

  Statement st = conn.createStatement();
  ResultSet rs = st.executeQuery(query);

  while(rs.next()) { 
      int balance = rs.getInt("Balance");
  String WAmmount = WithAmmTF.getText();
    int WAmmountint = Integer.parseInt(WAmmount);

    int newbalance = balance - WAmmountint;
        Statement s=conn.createStatement();

s.executeUpdate("INSERT INTO `user`(Balance) VALUE ('"+newbalance+"') where Password="+ passint);
  }

    conn.close();

    }
        catch (SQLException e) { 
        System.err.println("Got an exception! "); 
        System.err.println(e.getMessage()); 
    }        

0 个答案:

没有答案