Exemplo n.º 1
0
 public boolean update(T entity) {
   Connection connection = null;
   try {
     Integer result = 0;
     List<Object> sList =
         DBUtils.getInstance().createUpdateParam(getTableName(), entity, getTablePK());
     String sql = sList.get(sList.size() - 1).toString();
     System.out.println(sql);
     connection = DBManager.getConnection();
     final PreparedStatement pst = connection.prepareStatement(sql);
     for (int i = 0; i < sList.size() - 1; i++) {
       setParam(pst, sList.get(i), i);
     }
     result = pst.executeUpdate();
     pst.close();
     if (result > 0) {
       return true;
     }
   } catch (Exception e) {
     // TODO: handle exception
   } finally {
     DBManager.freeConnection(connection);
   }
   return false;
 }
Exemplo n.º 2
0
 /**
  * 插入实体类返回自增长id
  *
  * @param entity
  * @return
  */
 public Integer insertBackID(T entity) {
   Integer auto_id = -1;
   ResultSet rs = null;
   PreparedStatement pst = null;
   Connection connection = null;
   try {
     List<Object> sList =
         DBUtils.getInstance().createInsertParms(getTableName(), entity, getTablePK());
     String sql = sList.get(sList.size() - 1).toString();
     System.out.println(sql);
     connection = DBManager.getConnection();
     pst = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
     for (int i = 0; i < sList.size() - 1; i++) {
       setParam(pst, sList.get(i), i);
     }
     pst.executeUpdate();
     rs = pst.getGeneratedKeys();
     if (rs.next()) {
       auto_id = rs.getInt(1);
     }
     rs.close();
     pst.close();
   } catch (Exception e) {
     e.printStackTrace();
     // TODO: handle exception
   } finally {
     DBManager.freeConnection(connection);
   }
   return auto_id;
 }
Exemplo n.º 3
0
 public boolean delete(Integer id) {
   Connection connection = null;
   try {
     Integer result = 0;
     String sql = DBUtils.getInstance().createDelete(getTableName(), id, getTablePK());
     connection = DBManager.getConnection();
     final PreparedStatement pst = connection.prepareStatement(sql);
     result = pst.executeUpdate();
     pst.close();
     if (result > 0) {
       return true;
     }
   } catch (Exception e) {
     // TODO: handle exception
   } finally {
     DBManager.freeConnection(connection);
   }
   return false;
 }
Exemplo n.º 4
0
 /**
  * 插入数据
  *
  * @param entity 实体类
  * @return
  */
 public boolean insert(T entity) {
   Connection connection = null;
   try {
     Integer result = 0;
     String sql = DBUtils.getInstance().createInsert(getTableName(), entity, getTablePK());
     System.out.println("sql:" + sql);
     connection = DBManager.getConnection();
     final PreparedStatement pst = connection.prepareStatement(sql);
     result = pst.executeUpdate();
     if (result > 0) {
       return true;
     }
     pst.close();
   } catch (Exception e) {
     // TODO: handle exception
     e.printStackTrace();
   } finally {
     DBManager.freeConnection(connection);
   }
   return false;
 }
Exemplo n.º 5
0
 /**
  * 插入实体类返回自增长id
  *
  * @param entity
  * @return
  */
 public Integer insertBackIDParms(T entity) {
   Integer auto_id = -1;
   ResultSet rs = null;
   PreparedStatement pst = null;
   Connection connection = null;
   try {
     String sql = DBUtils.getInstance().createInsert(getTableName(), entity, getTablePK());
     System.out.println(sql);
     connection = DBManager.getConnection();
     pst = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
     pst.executeUpdate();
     rs = pst.getGeneratedKeys();
     if (rs.next()) {
       auto_id = rs.getInt(1);
     }
     rs.close();
     pst.close();
   } catch (Exception e) {
     // TODO: handle exception
   } finally {
     DBManager.freeConnection(connection);
   }
   return auto_id;
 }
Exemplo n.º 6
0
 public void updateTran(T entity, Connection connection, Statement statement) throws SQLException {
   String sql = DBUtils.getInstance().createUpdate(getTableName(), entity, getTablePK());
   statement.addBatch(sql);
 }