很简单,在插入数据库之前,先查询一下信息在数据库中是否存在。
如数据表(User)字段有:
id
name
age
即将插入一条信息【1,“名字”,22】
在插入数据之前,先
select count(*) from User where id=1 and name='名字' and age=22
假若查询出来不为0,那么数据就重复了。
根据这条线,很容易写出完整代码了。
给你要避免重复的字段建立唯一性约束,到时候java里捕获异常就可以了
你可以在insert into表的时候,先select count(0)判断这条数据是否存在
预防性建表create database if not testdb