Reputation: 137
String query = "SELECT SUM(totalcost) FROM"+m+""; //where totalcost is my column name
ResultSet rs=st.executeQuery(query);
while(rs.next())
{
System.out.println("SUM(user_id)="+rs.getString(1));
}
what is wrong in this code? my exception is:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'totalcost' in 'field list'
Upvotes: 0
Views: 1389
Reputation: 3759
try adding a space after the from
tag;
String query = "SELECT SUM(totalcost) FROM "+m+"";
Upvotes: 2
Reputation: 1
Can you confirm you have a column in the database called "totalcost" for the table you are referencing? If so, I'm thinking that the table name may be incorrect when you substitute the "m" variable.
Also, I would use a prepared statement to build the MySQL query. Let me know if you need more help.
You don't need to worry about using "AS totalcost" on the SUM(totalcost) part of the query, as when selecting the result, you are selecting: "rs.getString(1)" which will return the value in the first column of the row in the resultset.
Upvotes: 0