ResultSet rs=statement.executeQuery("select * from test where ID='"+name+"'");
if(rs==null)
{System.out.println("记录不存在");}
else
{System.out.println("记录存在");}这里我给的name的ID无论在数据库中是否存在,都是输出"记录存在"
如何修改?求教

解决方案 »

  1.   

    rs不管它查询记录是否存在,它返回值都不会是null的,要实现你要的效果可以这样
    if(rs.next())
    {System.out.println("记录不存在");}
    else
    {System.out.println("记录存在");}
      

  2.   

    你name是参数把,如果是这样写String sql="select * from test where ID="+name;
    ResultSet rs=statement.executeQuery(sql);
    if(rs==null)
    {System.out.println("记录不存在");}
    else
    {System.out.println("记录存在");}
      

  3.   

    name是参数
    改了之后还总是输出" 记录存在"
      

  4.   

    to zhangj0571:
    if(rs.next())之前要改什么吗?
    我改了之后还是老样子
      

  5.   

    String sql="select * from test where ID="+name;
    ResultSet rs=statement.executeQuery(sql);
    if(rs.next())
    {System.out.println("记录不存在");}
    else
    {System.out.println("记录存在");}
      

  6.   

    executeQuery
    ResultSet executeQuery(String sql)
                           throws SQLException
    Executes the given SQL statement, which returns a single ResultSet object. Parameters:
    sql - an SQL statement to be sent to the database, typically a static SQL SELECT statement 
    Returns:
    a ResultSet object that contains the data produced by the given query; never null 
    Throws: 
    SQLException - if a database access error occurs or the given SQL statement produces anything other than a single ResultSet object
    查查jdk带的帮助就知道了,写着never null,永不为空