JAVA SUM SQL语句怎么拼 带动态传参的 一直报错
publicStringWaresCollect(Stringstr){Connectionconnection=null;PreparedStatementprepar...
public String WaresCollect(String str){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String number = null;
try {
//获得链接对象
connection = CiteUtil.getConnection();
//针对商品信息表聚合查询拼写SQL语句
String sql = "select SUM ? from SPMXTable";
//将SQL语句发送数据库获得语句对象
preparedStatement = connection.prepareStatement(sql);
//通过语句对象实现动态传参数
preparedStatement.setString(1,"("+str+")");
//返回结果集
resultSet = preparedStatement.executeQuery();
//拿到总和结果值
while(resultSet.next()){
number = resultSet.getString(1);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
CiteUtil.closeAllSource(connection, preparedStatement, resultSet);
}
return number;
}
这是我代码说什么字符串不能用聚合函数 在数据库里面不是可以带列名么!求助! 展开
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String number = null;
try {
//获得链接对象
connection = CiteUtil.getConnection();
//针对商品信息表聚合查询拼写SQL语句
String sql = "select SUM ? from SPMXTable";
//将SQL语句发送数据库获得语句对象
preparedStatement = connection.prepareStatement(sql);
//通过语句对象实现动态传参数
preparedStatement.setString(1,"("+str+")");
//返回结果集
resultSet = preparedStatement.executeQuery();
//拿到总和结果值
while(resultSet.next()){
number = resultSet.getString(1);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
CiteUtil.closeAllSource(connection, preparedStatement, resultSet);
}
return number;
}
这是我代码说什么字符串不能用聚合函数 在数据库里面不是可以带列名么!求助! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询