Home >Database >Mysql Tutorial >封装-jdbc查询时间优化,判断主键是否重复的方法体简化怎么写

封装-jdbc查询时间优化,判断主键是否重复的方法体简化怎么写

WBOY
WBOYOriginal
2016-06-06 09:41:281336browse

封装mysqljdbcjava

菜鸟写了个对主键是否重复进行一个判断的方法。
mysql现在存有45万行数据,,就调用这方法判断就花了三四秒,但是直接mysql里直接查询cid主键列只需要0.00几秒不知道这里为什么这么慢,求解
后面大约还有一百多万行数据要录入。查询速度不久几何翻倍咯。求代码优化给点建议或者重写,,

<code> public  static boolean getcid(int cid){        Conn conn=null;    //Conn是封装好了对mysql数据库进行数据增删查改的方法的封装类        boolean gg=true;  //存取主键是否重复的变量。默认true        PreparedStatement st = null;        String sqll="SELECT cid FROM b";        ResultSet rs;        try {            conn=new Conn();                st=conn.prepareStatement(sqll);                rs=st.executeQuery();                while(rs.next())          //遍历结果集,,如果查询到cid已存在,方法返回false                    if(rs.getInt("cid")==cid)                    {                    gg=false;                    break;                    }        } catch (SQLException e) {            e.printStackTrace();        } catch (Exception e) {            e.printStackTrace();        }         return gg;    }</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn