注释解决问题sql.java.sql.SQLException

时间:2019-01-30 23:32:40

标签: java mysql netbeans-8

private void totaleActionPerformed(java.awt.event.ActionEvent evt) {                                       
     PreparedStatement ps;
    ResultSet rst;
 String query="SELECT SUM( montant_m)  FROM `mnd` ";
   String num_m = jTF1.getText();
    try {
        ps=Connecteur_db.connecterDB().prepareStatement(query);
        // ps.setString(1, num_m);
         rst=ps.executeQuery();
         if(rst.next()){
            String som_t = rst.getString("SUM(montant_m)");
           jLabe_resultat.setText(""+som_t);
           JOptionPane.showMessageDialog(null,""+som_t);
          }
         } catch (SQLException ex) {            java.util.logging.Logger.getLogger(noveau_j.class.getName()).log(Level.SEVERE, null, ex);
    }
      } 

在尝试执行此操作时,我收到类似“由于:java.sql.SQLException引起的错误:找不到列'SUM(montant_m)'”。     在这里是什么问题?请帮我.. 对不起,我英语不好 这是myconnecteur_db()类的连接

public static Connection connecterDB() {
    Connection conx = null;
    String pilot = "com.mysql.cj.jdbc.Driver";
    try {
        Class.forName(pilot);//chargement de driver
        System.out.println("Driver ok");
        String url = "jdbc:mysql://localhost:3307/tc";
        String user = "root";
        String pw;
        pw = "root";
        Connection con = DriverManager.getConnection(url, user, pw);
        System.out.println("la connection est bien etablir");
        return con;
    } catch (Exception e) {
        System.out.println("Echec connection!!");
        e.printStackTrace();
        return null;

    }
}

1 个答案:

答案 0 :(得分:1)

我认为这是在行rst.getString("SUM(montant_m)")

上发生的

SUM(montant_m)montant_m前没有空格。

要使其更容易,请使用查询:

SELECT SUM(montant_m) AS total FROM mnd

然后是rst.getString("total")