try{
            rstb=stmt.executeQuery("SELECT '"+JTF_SKU.getText()+"' as SKU,'"+JTF_GN.getText()+"' as GoodsName,'"+Float.parseFloat(JTF_GBP.getText())+"'" +
                    " as GoodsBuyPrice,'"+Float.parseFloat(JTF_GSP.getText())+"' as GoodsSellPrice," +
                    "'"+Integer.parseInt(JTF_GW.getText())+"' as GoodsWeight,'"+JTF_GP.getText()+"' as GoodsProvider," +
                     "'"+Integer.parseInt(JTF_GT.getText())+"' as GoodsTotal FROM Goods_DB");
            rstb.next();//我在这里使用是否有rstb.next();来判断是否有此记录看来是不行的了……
            if(rstb.next()){
                if(JTF_SKU.getText().length()>20||JTF_GN.getText().length()>10||JTF_GP.getText().length()>10){
                    javax.swing.JOptionPane.showMessageDialog(null, "SKU字符要小于20个GoodsName字符要少于10个GoodsProvider要少于10个" +
                            "","温馨提示",JOptionPane.PLAIN_MESSAGE);
                }else{
                    str=JTF_GBP.getText();
                    fl=Float.parseFloat(str);
                    fl1=Float.parseFloat(JTF_GSP.getText());//直接用了,懒得去调用了
                    in=Integer.parseInt(JTF_GW.getText());
                    in1=Integer.parseInt(JTF_GT.getText());
                    String sql="INSERT INTO Goods_DB(SKU,GoodsName,GoodsBuyPrice,GoodsSellPrice,GoodsWeight,GoodsTotal,GoodsProvider) VALUES('"+JTF_SKU.getText()+"','" +
                            ""+JTF_GN.getText()+"','"+fl+"','"+fl1+"','"+in+"','"+in1+"','"+JTF_GP.getText()+"')";
                    stmt.executeUpdate(sql);
                    showtable();
                }
            }else{
                javax.swing.JOptionPane.showMessageDialog(null, "","温馨提示",JOptionPane.PLAIN_MESSAGE);
            }
        }catch(Exception e){
            javax.swing.JOptionPane.showMessageDialog(null,e);
            javax.swing.JOptionPane.showMessageDialog(null,e.getStackTrace(),"出错",javax.swing.JOptionPane.ERROR_MESSAGE);
        }
以上……有谁能告诉我,我应该怎么来判断数据库中已经有这个记录了么?难道是rstb为空?rstb==null也判断不了……哎~没辙了……还是说我查询的就是错误的?如果全部数据一样就判定为一样的数据,只要有一样不一样就判断为不同的数据

解决方案 »

  1.   

    对,你的查询就是错误的,你的语句相当于
    select 'a' as a, 'b' as b,'c' as c from tbl; 你的表里有多少条记录,你这个查询语句就会返回多少条记录。一般对重复数据的处理,都是从数据库里面来考虑的,比如把不能重复的某一个列或几个列(一般都不超过3个)设成唯一索引,这样你insert或update的时候,如果想弄重复值,数据库会给你报错,你只要去捕捉这个错就好了。
    用select的方式来看看数据库里面是否有了对应的值,不大靠谱,在多线程环境下,不锁表根本做不到的。
      

  2.   

    汗,
                rstb.next();//我在这里使用是否有rstb.next();来判断是否有此记录看来是不行的了……
                if(rstb.next()){//这里总行了吧
      

  3.   


    select count(1) from tbl where a='a' and b='b';程序rs.getInt(1) 看结果是不是0
      

  4.   


    用select count可以完成相关的功能 但是如果数据库中字段很多的话 不知道在性能上是否满足要求
    摸索中......
      

  5.   


    没办法,要求每个字段都相等的话,似乎只能这样,最多再加一个只要一条符合的就返回避免全表扫完。
    比如oracle:
      select  * from tbl where a='a' and b='b' and rownum=1;
    或者其他的
      select  top 1 之类的
      

  6.   

    看你什么数据库
    mysql的话
    select id from tbl where a='a' and b='b' limit 1oracle的话
    select rowid from tbl where a='a' and b='b' and rownum=1