博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库的自增字段代码生成器——解决不同数据库自增字段的差异机制
阅读量:4323 次
发布时间:2019-06-06

本文共 2692 字,大约阅读时间需要 8 分钟。

数据库的ID自增字段代码生成器——解决不同数据库自增字段的差异机制

问题:

在设计数据库字段时,有时需要一个int型的id主键,让它能自动递增,每次插入一条数据,它都能够自动增1或者规定的自增数n。对于特定的数据库,要实现这个很简单,比如mysql是用auto_incrementSql Server是用identity。但是如果在代码中使用和特定数据库有关的特性,那么代码就不能移植,比如把数据库从mysql换成sql server,那么代码就要更改,就不能实现移植扩展。

解决思路:

既然要实现代码的移植扩展,就不能使用和数据库相关的特性,那该怎么办呢?这就是本篇要介绍的ID生成器。

思路是这样的:在数据库中另外建立一张表ID_Main,其包含两个字段:一个字段是要实现ID自增的所属表名(table_name),另一个字段是该表中自增字段对应的现在最大的记录值(id_max)。假设现在要在数据表(user_info)中插入一条数据时,先使表ID_Main中对应字段table_name为的user_infoid_max的值加1,然后再取出该值,就是自动增1后的值。以后每次要插入一条记录,都会先使ID_Main表中对应的id值加1,再返回,其也就是保持了id的自增。

ID_Main的设计如下:

/*==============================================================*/

/* Table: ID_Main                                           */

/*==============================================================*/

create table ID_Main  (

   TABLE_NAME           VARCHAR2(20)                    not null,

   ID_MAX               NUMBER(10)                      not null,

   constraint PK_ ID_Main primary key (TABLE_NAME)

);

comment on table ID_Main is '用于维护各个表中的记录条数';

 

 

以下就是代码层,以Java分析为例:

import java.sql.ResultSet; import java.sql.SQLException; import kane.DbUtility; //ID生成器实体类 public class IDGenerator { // 使用单例模式 private static IDGenerator instance = null; private IDGenerator() { } public static synchronized IDGenerator getInstance() { if (instance == null) { instance = new IDGenerator(); } return instance; } // 通过主键维护表ID_Main来获取表中的记录数 public int getTotal(Connection conn, String tableName) throws SQLException { int i = 0; PreparedStatement pstmt = null; ResultSet rs = null; try { String sql = "select ID_MAX from ID_Main where table_name = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, tableName); rs = pstmt.executeQuery(); if (rs.next()) { i = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); throw e; } finally { DbUtility.closeResultSet(rs); DbUtility.closePreparedStatement(pstmt); } return i; } // 将主键维护表ID_Main中的记录数加1,然后再返回该值 public int getNextID(String tableName) { int total = 0; Connection conn = null; PreparedStatement pstmt = null; try { conn = DbUtility.getConnection(); String sql = "update ID_Main set ID_MAX = ID_MAX +1 where table_name=?"; // 开启事务 conn.setAutoCommit(false); pstmt = conn.prepareStatement(sql); pstmt.setString(1, tableName); pstmt.executeUpdate(); total = getTotal(conn, tableName); // 执行事务 conn.commit(); } catch (SQLException e) { e.printStackTrace(); // 事务回滚 try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } finally { DbUtility.closePreparedStatement(pstmt); DbUtility.closeConnection(conn); } return total; } }

其中的DbUtility类是连接数据库操作的封装类,其代码详细参见》。代码使用了connection的事务操作,保证事务的一致性,也就是当ID_MAX的值自增1时,应该在相应的表中插入一条记录,以保证自增值一致。如果出错,这应该回滚事务。

通过以上分析与代码示例,就简单的实现了一个IDGeneratorID生成器,它与数据库的特性隔离,实现了代码层的移植性。

 

 

 

 

转载于:https://www.cnblogs.com/java-source/archive/2010/11/22/2604341.html

你可能感兴趣的文章
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
64位MATLAB和C混合编程以及联合调试
查看>>
原生js大总结二
查看>>
PHP基础
查看>>
UVa 11488 超级前缀集合(Trie的应用)
查看>>
Django 翻译与 LANGUAGE_CODE
查看>>
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>
数据挖掘算法比赛 - 简单经验总结
查看>>
win7(64位)php5.5-Apache2.4-mysql5.6环境安装
查看>>
生成商户订单号/退款单号
查看>>
使用Android OpenGL ES 2.0绘图之六:响应触摸事件
查看>>
我们过去几年做对了哪些事
查看>>