java
  • sql
  • while-loop
  • 2014-12-13 95 views 3 likes 
    3

    基本上我将一条记录添加到数据库:标题和说明。 我想让它如此,如果标题的值已经存在 - 将(1)或(2)添加到最后以使其可区分。我写了这个函数:Java:虽然记录存在=保持将记录添加到记录的末尾

    public boolean existCheck(int userId, String title){ 
        String Validate = "SELECT COUNT(*) FROM memories WHERE userid='"+userId+"' AND title='"+title+"'"; 
        int cnt = 0; 
        boolean result = false; 
        try{ 
         rs = st.executeQuery(Validate); 
         if(rs.next()){ 
          cnt = rs.getInt(1); 
          System.out.println(cnt); 
          if(cnt != 0){ 
    
           result = true; 
          } 
         } 
        } 
        catch(Exception e){e.printStackTrace();} 
        return result; 
    } 
    

    它计算该userID和Title有多少条目。如果不是0,则它存在并返回它存在的真。

    此代码将记录添加到数据库。

    if(existCheck(userId, title) == true){ 
        st.executeUpdate("INSERT INTO memories (userid, title, description) VALUES ('"+userId+"', '"+title+"(1)', '"+desc+"')"); 
    } 
    else{ 
        st.executeUpdate("INSERT INTO memories (userid, title, description) VALUES ('"+userId+"', '"+title+"', '"+desc+"')");  
    } 
    

    但这个代码的缺点是,如果title(1)已经存在,它会再次添加另一个标题(1)。 当然,我可以做很多if语句,但我确信我可以使用while循环以较少的线条做到这一点。

    我不知道如何在这种情况下使用它,但我不希望服务器陷入无限循环。

    我该如何申请while()循环呢?

    回答

    2

    您的existCheck应返回计数而不是布尔值。

    然后,你可以这样做:

    int index = existCheck(userId, title); 
    if(index > 0){ 
         st.executeUpdate("INSERT INTO memories (userid, title, description) VALUES ('"+userId+"', '"+title+index+"', '"+desc+"')"); 
    } else{ 
         st.executeUpdate("INSERT INTO memories (userid, title, description) VALUES ('"+userId+"', '"+title+"', '"+desc+"')");  
    } 
    

    的第一条记录中输入将有title,第二title+"1",第三title+"2",等等...

    当然,你会也必须更改existCheck中的WHERE子句以解释不同的标题:

    SELECT COUNT(*) FROM memories WHERE userid='"+userId+"' AND title LIKE '"+title+"'%" 
    

    而且虽然与您询问的内容不完全相关,但我强烈建议您使用预准备语句而不是静态语句(其中参数值是SQL字符串的一部分)。这会更安全。

    +0

    这个解决方案的工作,以什么方式更安全? – arleitiss 2014-12-13 18:51:19

    +1

    @arleitiss阅读关于SQL注入 – Eran 2014-12-13 18:52:57

    1

    你应该改变existCheck寻找标题模糊,而不是:

    "SELECT COUNT(*) FROM memories WHERE userid='"+userId+"' AND title='"+title+"'" 
    

    其寻找一个确切的冠军争夺战中,使用

    "SELECT COUNT(*) FROM memories WHERE userid='"+userId+"' AND title LIKE '"+title+"%'" 
    

    这将寻找开头标题title

    或者,使得像开始前一个标题不匹配,你也可以使用:

    "SELECT COUNT(*) FROM memories WHERE userid='"+userId+"' AND (title='"+title+"' OR title LIKE '"+title+"(%)')" 
    

    注意%将匹配在括号任何东西,所以title(soemthing)也将匹配。
    如果您使用MySQL you can use regular expressions,我不确定其他数据库服务器。


    至于避免题(1)的重复插入,你需要知道符合条件的标题的量,所以只返回COUNT

    public int existCount(int userId, String title){ 
        String Validate = "SELECT COUNT(*) FROM memories WHERE userid='"+userId+"' AND (title='"+title+"' OR title LIKE '"+title+"(%)')"; 
        int cnt = 0; 
        try{ 
         rs = st.executeQuery(Validate); 
         if(rs.next()){ 
          cnt = rs.getInt(1); 
          System.out.println(cnt); 
         } 
        } 
        catch(Exception e){ 
         e.printStackTrace(); 
        } 
        return cnt; 
    } 
    // ... 
    final int count = existCount(userId, title); 
    if(count > 0){ 
        final String sql = String.format("INSERT INTO memories (userid, title, description) VALUES ('%d', '%s(%d)', '%s')", userId, title, count, desc); 
        st.executeUpdate(sql); 
    } 
    else{ 
        st.executeUpdate("INSERT INTO memories (userid, title, description) VALUES ('"+userId+"', '"+title+"', '"+desc+"')");  
    } 
    
    0
    In the exisTCheck() function, I will advice you to return the number instead of boolean. The return number will be number appended with the title. Say if you find only title in DB, then return 0. If you find title(1) then with string split get the number and return it. 
    
    While you are calling existCheck() function to check whether it is present or not, rather then true or false you will get counter. You just need to increment that counter with new value according to your requirement and modify your query. 
    
    
    The abstract idea is like this.... 
    
    Function int existCheck()enter code here 
    { 
    // Get the data from the db 
    // Split the string with title 
    //if after title no string then return 0 
    //else return your number 
    
    } 
    // call to existCheck() 
    int count = existCheck(); 
    //Modify your query with 
        st.executeUpdate("INSERT INTO memories (userid, title, description) VALUES ('"+userId+"', '"+title+"count', '"+desc+"')"); 
    
    相关问题