Mysql varchar自增ID
来源:互联网 发布:数据分析就业行业 编辑:程序博客网 时间:2024/06/07 21:32
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import utils.DBHelper;
public class CreateID {
static DBHelper dbHelper = new DBHelper();
Connection conn = dbHelper.getConn();// 获取连接
Statement stmt = null;
/**
*
*
* @param head
* 头部编号
* @param col
* 字段名称
* @param tableName
* 数据表名称
* @return
* @throws Exception
*/
public String createID(String head, String col, String tableName) throws Exception {
stmt = conn.createStatement();
// 获取编号最大值
String sql = "select " + col + " from " + tableName + " order by " + col + " desc limit 0,1";
ResultSet rs = stmt.executeQuery(sql);
String value = null;// 课程编号值
ResultSetMetaData metaData = rs.getMetaData();
if (rs.next()) {
// 此方法返回列的别名。如果列别名不可用,此方法将返回列名称。
String columnName = metaData.getColumnLabel(1);
// 获取到最大的编号值
value = rs.getString(columnName);
}
if (value == null) {
// 如果编号是空,则新增为"KC0001"
value = head + "0001";
} else {
// 取出后4位流水号
value = value.substring(2, 6);
int num = Integer.parseInt(value) + 1;// 实现编号自增
int length = Integer.toString(num).length();
String zero = "";
// 将剩余位数补全为0
for (int i = 0; i < 4 - length; i++) {
zero += "0";
}
value = head + zero + num;// 返回流水号的数值
}
dbHelper.closeConn();
return value;
}
/**
*
* @param head
* @param col
* @param tableName
* @return
* @throws Exception
*/
public String createOrderID(String head, String col, String tableName) throws Exception {
stmt = conn.createStatement();
String sql = "select " + col + " from " + tableName + " order by " + col + " desc limit 0,1";
ResultSet rs = stmt.executeQuery(sql);
String value = null;
String dateNow = new SimpleDateFormat("yyyyMM").format(new Date());
ResultSetMetaData metaData = rs.getMetaData();
if (rs.next()) {
metaData.getColumnCount();
String columnName = metaData.getColumnLabel(1);
value = rs.getString(columnName);
}
if (value == null) {
value = head + dateNow + "0001";
} else {
String date=value.substring(3,9);
if (date.equals(dateNow)) {
value = value.substring(9, 13);
int num = Integer.parseInt(value) + 1;
int length = Integer.toString(num).length();
String zero = "";
for (int i = 0; i < 4 - length; i++) {
zero += "0";
}
value = head + date + zero + num;
} else {
value = head + dateNow + "0001";
}
}
dbHelper.closeConn();
return value;
}
public String createEmpId() throws Exception {
return createID("XD", "employeeid", "employeebasicinfo");
}
public String createPetId() throws Exception {
return createOrderID("Pet", "petid", "petinfo");
}
public static void main(String[] args) {
try {
System.out.println(new CreateID().createPetId());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import utils.DBHelper;
public class CreateID {
static DBHelper dbHelper = new DBHelper();
Connection conn = dbHelper.getConn();// 获取连接
Statement stmt = null;
/**
*
*
* @param head
* 头部编号
* @param col
* 字段名称
* @param tableName
* 数据表名称
* @return
* @throws Exception
*/
public String createID(String head, String col, String tableName) throws Exception {
stmt = conn.createStatement();
// 获取编号最大值
String sql = "select " + col + " from " + tableName + " order by " + col + " desc limit 0,1";
ResultSet rs = stmt.executeQuery(sql);
String value = null;// 课程编号值
ResultSetMetaData metaData = rs.getMetaData();
if (rs.next()) {
// 此方法返回列的别名。如果列别名不可用,此方法将返回列名称。
String columnName = metaData.getColumnLabel(1);
// 获取到最大的编号值
value = rs.getString(columnName);
}
if (value == null) {
// 如果编号是空,则新增为"KC0001"
value = head + "0001";
} else {
// 取出后4位流水号
value = value.substring(2, 6);
int num = Integer.parseInt(value) + 1;// 实现编号自增
int length = Integer.toString(num).length();
String zero = "";
// 将剩余位数补全为0
for (int i = 0; i < 4 - length; i++) {
zero += "0";
}
value = head + zero + num;// 返回流水号的数值
}
dbHelper.closeConn();
return value;
}
/**
*
* @param head
* @param col
* @param tableName
* @return
* @throws Exception
*/
public String createOrderID(String head, String col, String tableName) throws Exception {
stmt = conn.createStatement();
String sql = "select " + col + " from " + tableName + " order by " + col + " desc limit 0,1";
ResultSet rs = stmt.executeQuery(sql);
String value = null;
String dateNow = new SimpleDateFormat("yyyyMM").format(new Date());
ResultSetMetaData metaData = rs.getMetaData();
if (rs.next()) {
metaData.getColumnCount();
String columnName = metaData.getColumnLabel(1);
value = rs.getString(columnName);
}
if (value == null) {
value = head + dateNow + "0001";
} else {
String date=value.substring(3,9);
if (date.equals(dateNow)) {
value = value.substring(9, 13);
int num = Integer.parseInt(value) + 1;
int length = Integer.toString(num).length();
String zero = "";
for (int i = 0; i < 4 - length; i++) {
zero += "0";
}
value = head + date + zero + num;
} else {
value = head + dateNow + "0001";
}
}
dbHelper.closeConn();
return value;
}
public String createEmpId() throws Exception {
return createID("XD", "employeeid", "employeebasicinfo");
}
public String createPetId() throws Exception {
return createOrderID("Pet", "petid", "petinfo");
}
public static void main(String[] args) {
try {
System.out.println(new CreateID().createPetId());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
阅读全文
0 0
- Mysql varchar自增ID
- mysql id 自增
- MySql id自增
- MySQL自增Id
- Mysql varchar型自增方法
- Mysql 获取自增Id
- MySQL 自增ID生成器
- mysql返回自增id
- MYSQL的自增ID
- mysql 虚拟自增ID
- 【mysql】 varchar类型id,取最大值
- mysql 数据库自增id 的总结
- mysql自定义表自增id步长
- mysql中设置id号自增
- mysql自增ID(auto_increment)讨论
- J2EE MySQL获取自增id
- mysql 数据库自增id 的总结
- MySQL获取自增ID的方法
- OLAP和OLTP的区别
- 剑指offer 编程题(6):旋转数组
- Nginx部署
- 112. Path Sum
- mybatis不同数据库共用同个事物
- Mysql varchar自增ID
- 为input标签绑定事件的几种方式
- 哈夫曼树与哈夫曼编码
- 在centos7下使用systemctl加入自建服务
- Django死活不跳转的问题
- 字符串模板(Python)
- TCP UDP
- CSAPP Note chap7
- 浅谈Spring的事务配置注意点