日期:2014-05-20  浏览次数:21045 次

jdbc如何取得select count(*)的总和
SQL语句是 select count(*) from people
然后我怎么才能得到这个结果,用JDBC的哪个对象取得啊。
另外我的这个代码怎么回事?
public void inputInfo() throws SQLException { // 录取人事
System.out.print("\n 请您输入姓名:\n");
name = s.nextLine();
System.out.print("\n 请您输入性别(m代表男,W代表女:\n");
sex = s.nextLine();
System.out.print("\n 请您输入城市:\n");
city = s.nextLine();
System.out.print("\n请您输入街道:\n");
address = s.nextLine();
System.out.print("\n请您输入电话\n");
tel = s.nextLine();
System.out.print("\n请您输入年龄\n");
age = s.nextInt();
System.out.print("\n请您输入入职年份\n");
date = s.nextLine();
DBConn d = new DBConn();
con = d.getCon(); //
try {
ps = con.prepareStatement(sqlInsert);
ps.setString(1, name);
ps.setString(2, sex);
ps.setString(3, city);
ps.setString(4, address);
ps.setString(5, tel);
ps.setInt(6, age);
ps.setString(7, date);
ps.executeUpdate();
} catch (Exception e) {
System.out.println("插入数据库失败");

con.close();
}


public static void main(String args[]) throws SQLException {
PManagent p = new PManagent();
// p.menu();
Scanner s = new Scanner(System.in);
int choice;
boolean flag = true;
while (flag) {
p.menu();
try {
choice = s.nextInt();
switch (choice) {
case 1:
p.inputInfo();
break;
case 2:
p.showInfo();
break;
case 3:
p.delInfo();
break;
case 4:
p.showPerson();
break;
case 5:
p.total();
break;
case 6:
p.version();
break;
case 0:
p.exit();
break;
default:
System.out.println("请输入正确的选项数字");
}
} catch (Exception e) {
System.out.println("输入的不是正确的数字,系统将退出");
break;
}
}
}

还没有等输入年份就进入下次循环了,能不能像C语言那样有个getchar();可以缓冲一下的。


------解决方案--------------------
是这样子的
select count(*) from [table]

rs.next();
int count =rs.getInt(1);
就可以了