package Water;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class QueryDesc {
public static List<UserVo> showUser(String M ) throws ClassNotFoundException{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<UserVo> list = new ArrayList<UserVo>();//声明一个List集合,用于存放查询出的数据
try {
String Str=null;
Str=String.format(" %s ","str"); // 格式化字符串
conn = JDBC_Connection.getConnection();// 连接数据库
stmt = conn.createStatement();// 建立Statement对象;
rs = stmt.executeQuery("select %s from MONTHAVERAGE" );
while(rs.next())// 结果集存在,则进行循环遍历
{
UserVo userVo = new UserVo();
userVo.setDD(rs.getInt("dd"));
list.add(userVo);
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBC_Connection.free(rs, conn, stmt);// 关闭连接
}
return list;
}
public static void main(String[] args) throws ClassNotFoundException {
System.out.print("请输入要计算的月份:");
Scanner scanner=new Scanner(System.in);
String str=scanner.next();
List<UserVo> list=QueryDesc.showUser(str);//调用查询方法
//如果list集合不为空,则循环遍历打印出所有的信息
if(list!=null){
System.out.print("dd");
System.out.println();
for (int j = 0; j < list.size(); j++) {
System.out.print(list.get(j).getDD()+"\t");
System.out.println();
}
}
}
}
为什么总是显示无效字符啊?
------解决方案--------------------
UserVo的代码 和完整的报错信息
------解决方案--------------------
rs = stmt.executeQuery("select %s from MONTHAVERAGE" );
改成
rs = stmt.executeQuery("select dd from MONTHAVERAGE" );
------解决方案--------------------
rs = stmt.executeQuery("select dd from MONTHAVERAGE dd" ); 或者rs = stmt.executeQuery("select * from MONTHAVERAGE " );