| int affectableRow = 0; //执行SQL后影响的行数 if (conditions == null) { ss = "INSERT INTO " + name + "("; for (int i = 0; i < row.length(); ++i) { String k = row.getKey(i); ss += k; if (i != row.length() - 1) { ss += ", "; } } ss += ") VALUES ("; for (int j = 0; j < row.length(); ++j) { ss += (row.get(j) == null) ? "null" : "?"; //如果row中有空值则设置为null,否则设置为查询参数 if (j != row.length() - 1) { ss += ", "; } } ss += ")"; } else { ss = "UPDATE " + name + " SET "; for (int i = 0; i < row.length(); ++i) { String k = row.getKey(i); ss += k + "=" + ( (row.get(i) == null) ? "null" : "?"); //设置查询参数 if (i != row.length() - 1) { ss += ", "; } } ss += " WHERE "; ss += conditions; } Connection conn = null; try { conn = database.getConnection(); PreparedStatement st = conn.prepareStatement(ss); int j = 0; //查询参数计数器 for (int i = 0; i < row.length(); i++) { if (row.get(i) != null) { //如果不是空则解析查询参数 st.setObject(++j, row.get(i)); //解析查询参数 } } if (args != null) { for (int i = 0; i < args.length; i++) { st.setObject(++j, args[i]);//预定的规则,null不能放到查询参数中要以name=null的静态形式存放 } } affectableRow = st.executeUpdate(); st.close(); } |