public static boolean hasEmployee(String empName) :查询SQL语句后 如果有返回true,否则返回false。这是小弟写的代码:
public static boolean hasEmployee( String empName)
{
PreparedStatement pstmt=null;
try
{
String sql="select empno,ename,sal,deptno from emp where ename=?";
pstmt=ConnectionFctory.getConnection().prepareStatement(sql);
pstmt.setString(1, empName);
pstmt.executeUpdate();
pstmt.close();
return true;
}catch(SQLException se)
{
System.out.println("查询员工是否存在出错!"+se.getMessage());
return false;
}
}执行SQL语句就返回true,抛出异常就返回false;
但是为什么我每次执行都是true~~不返回false的~ 就算我输入的empName数据库里面没有~~ 还是显示true啊~~ 高手指教!!!!
public static boolean hasEmployee( String empName)
{
PreparedStatement pstmt=null;
try
{
String sql="select empno,ename,sal,deptno from emp where ename=?";
pstmt=ConnectionFctory.getConnection().prepareStatement(sql);
pstmt.setString(1, empName);
pstmt.executeUpdate();
pstmt.close();
return true;
}catch(SQLException se)
{
System.out.println("查询员工是否存在出错!"+se.getMessage());
return false;
}
}执行SQL语句就返回true,抛出异常就返回false;
但是为什么我每次执行都是true~~不返回false的~ 就算我输入的empName数据库里面没有~~ 还是显示true啊~~ 高手指教!!!!
楼主【goopoolzl】截止到2008-07-25 21:44:35的历史汇总数据(不包括此帖):
发帖的总数量:11 发帖的总分数:200 每贴平均分数:18
回帖的总数量:6 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:11 结贴的总分数:200
无满意结贴数:1 无满意结贴分:20
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:9.09 % 无满意结分率:10.00 %
敬礼!
在此 PreparedStatement 对象中执行 SQL 语句,该语句必须是一个 SQL 数据操作语言(Data Manipulation Language,DML)语句,比如 INSERT、UPDATE 或 DELETE 语句;或者是无返回内容的 SQL 语句,比如 DDL 语句。
executeQuery()才能返回ResultSet对象啊,看下API