得到某个表的某个字段的最大值(Oracle与Mysql的区别)

(1)Oracle中

public String getMaxIdInfo(String table, String columname) {
  String sql = "select max(to_number("+columname+")) from "+table;
  String num = "";
  con = dbc.getConnection();
  try {
   pst = con.prepareStatement(sql);
   //pst.setString(1, columname);
   //pst.setString(2, table);
   rs = pst.executeQuery();
   if (rs.next()) {
    num = (rs.getInt(1)+1)+"";
   }
  } catch (Exception e) {
   Menulogger.debug("获取最大ID信息失败!"+e);
   e.printStackTrace();
   // TODO: handle exception
  }finally{
   dbc.closeQuery(rs, pst, con);
  }
  return num;
 }

(2)MySql中

public String getMaxIdInfo(String table, String columname) {
  String sql = "select max("+columname+"+0) from "+table;
  String num = "";
  con = dbc.getConnection();
  try {
   pst = con.prepareStatement(sql);
   //pst.setString(1, columname);
   //pst.setString(2, table);
   rs = pst.executeQuery();
   if (rs.next()) {
    num = (rs.getInt(1)+1)+"";
   }
  } catch (Exception e) {
   Menulogger.debug("获取最大ID信息失败!"+e);
   e.printStackTrace();
   // TODO: handle exception
  }finally{
   dbc.closeQuery(rs, pst, con);
  }
  return num;
 }

你可能感兴趣的:(java)