
alter table test2 add column id int auto_increment not null, add primary key(id);
注意: 一个表中,最多只能有一个自动字段,并且这个自动字段还必须被定义为key。
用JDBC连接数据库,然后用sql语句。要导入mysql的驱动包。
importjavasql;
publicclassTestMySql{
staticConnectioncon=null;//声明Connection对象
staticStatementsql=null;
staticResultSetres=null;
publicstaticvoidmain(String[]args){
TestMySqlc=newTestMySql();
con=c();
try{
sql=con();
res=sql("selectfromdept");
//sql语句,我数据库里有张dept表
while(resnext()){//输出结果
Systemoutprint(resgetString(1)"
Systemoutprint(resgetString(2)"
Systemoutprint(resgetString(3));
Systemoutprintln();
}
}catch(e){
e();
}finally{
try{
if(res!=null){
resclose();
res=null;
}
if(sql!=null){
sqlclose();
sql=null;
}
if(con!=null){
conclose();
con=null;
}
}catch(e){
e();
}
}
}
publicConnection(){
try{
ClassforName("commysqljdbcDriver");
//加载oracleJDBC驱动
Systemoutprintln("数据库驱动加载成功");
}catch(e){
e();
}
try{//通过访问数据库的URL获取数据库连接对象
con=(
"jdbc:mysql://localhost:3306/mydata","root","qwer1234");
//mydata为mysql名字
Systemoutprintln("数据库连接成功");
}catch(e){
e();
}
returncon;//按方法要求返回一个Connection对象
}
}
把数组转换成字符串后再存入数据库
参考函数:
implode() ---把数组转换成字符串
explode() ---把字符串转换成数组
手册里有详细说明
序列化以后存储
对于一维数组可以简单的用
implode
explode
函数做数组——串的变换,但需注意分隔符要使用数组中不存在的字符
对于多维数组可以用序列化函数处理
serialize
unserialize
函数的用法手册中都有
mysql是没有序列的,我最近刚做完一个项目也是从oralce移植到mysql数据库上,oracle中 HIbernate配置都是这样
<generator class="sequence">
<param name="sequence">SEQUENCE_CHILDREM_ARCHIVE_ID</param>
</generator>
,到移植到mysql数据库中之后
<generator class="identity"></generator>
identity或者是increment都是可以滴,前提是你mysql表中的主键是auto_increatement的int类型的。
以上就是关于mysql 主键自增 年份加自增序列号怎么做全部的内容,包括:mysql 主键自增 年份加自增序列号怎么做、java中怎么获取mysql数据库的数据(java获取mysql数据库列表)、mysql数据库 怎么得到序列化数组的长度等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)