简体   繁体   中英

Why won't my query run properly? When ever I pass a string variable through the executeQuery method I get an error

There is an error on the line that says ps = stmt.executeQuery();....the error says method executeQuery in interface Statement cannot be applied to given types; required: String found: no arguments reason: actual and formal argument lists differ in length

But whenever I pass a String through that line I get an error saying java.sql.SQLException:Method 'executeQuery(String)' not allowed on prepared statement...

This method is for a button that adds all the Integer values in a column of SQL table.

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

try{

String SQL = "SELECT SUM(PRICE) FROM MENU";

stmt = con.prepareStatement(SQL);

ps = stmt.executeQuery();

if(ps.next()) {

String total = ps.getString("SUM(PRICE)");

textTotalCost.setText(total);
      }                    
    }

catch (SQLException err) {

JOptionPane.showMessageDialog(null, err);
    } 

 }

Updated following the question change

There is no column name explicitly specified for the calculated column SUM(PRICE) in your SQL statement. It would hardly ever actually be "SUM(PRICE)". Try naming your column. Also prepareStatement is useful when passing parameters. Simple Select does not need it:

private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {                                             
    try{
         String SQL = "SELECT SUM(PRICE) As PRICE_TOTAL FROM MENU";
         Statement stmt = con.createStatement();
         ResultSet ps = stmt.executeQuery(SQL);
         if(ps.next()) {
             String total = ps.getString("PRICE_TOTAL");
             textTotalCost.setText(total);
         }                    
    }
    catch (SQLException err) {
        JOptionPane.showMessageDialog(null, err);
    } 
}

or access the value by column index rather than name:

private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {                                             
    try{
         String SQL = "SELECT SUM(PRICE) FROM MENU";
         Statement stmt = con.createStatement();
         ResultSet ps = stmt.executeQuery(SQL);
         if(ps.next()) {
             String total = ps.getString(0);
             textTotalCost.setText(total);
         }                    
    }
    catch (SQLException err) {
        JOptionPane.showMessageDialog(null, err);
    } 
}

Column name is must while getting a data

Thanks

I don't fully understand your description but I think you are trying to assign the PreparedStatement created in

stmt = con.prepareStatement(SQL)

to a variable of type Statement (stmt) and that's causing the error. The line of code above works because PreparedStatement is an extension (or implementation) of Statement but

ps = stmt.executeQuery();

fails because the class Statement doesn't have an executeQuery method without parameters, PreparedStatement does. @YB 's solution and the rest of recommendations are good (the alias for sum(price) or getting the value by column index) if it's ok using a regular Statement. The alternative is changing the type of stmt to PreparedStatement in your original code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM