闂備浇銆€閸嬫捇鏌熼婊冾暭妞ゃ儻鎷� (0) +1 闂佽崵濮嶅鍡楊伓 (0) +1 闂傚倸饪存禍鐑藉箯閿燂拷 (0) +1
闂備浇銆€閸嬫捇鏌熼婊冾暭妞ゃ儲鍨块弻鐔衡偓娑櫭慨鍥р攽椤曞棙瀚�闂備礁鎼悮顐﹀磿閸愯鑰块柛娑欐綑缂佲晠鏌熼婊冾暭妞ゃ儻鎷�>>

正在阅读:一个简单实用的数据库操作框架一个简单实用的数据库操作框架

2004-08-16 10:34 出处:CSDN 作者:chensheng913 责任编辑:linjixiong


  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();
  }





察看评论详细内容 我要发表评论
作者笔名简短内容 发表时间
:

键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品
闂備礁缍婂ḿ褔顢栭崱妞绘敠闁逞屽墴閹鎷呴崨濠備淮濠碘槅鍋撻幏锟�闂備焦鎮堕崕鎶藉磻濞戙垹鏄ラ悘鐐板嫎娴滄粓鏌涢锝嗙闁告棑鎷�