Java之JDBC创建表名不同,表头相同的表

一、先创建对应类,具体代码如下

public class UserTable {
    private String tableName;

    public UserTable(String str){
        tableName = str;
    }

    @Override
    public String toString() {
        return "create table u" + tableName  + "(" +  "time varchar(50) not null, content varchar(100) not null );";
    }
}

二、JDBC操作,先检查表是否存在,若不存在则创建

public void createRecordsTable() throws SQLException {
    //1.创建数据库连接池
    Properties prop = new Properties();
    prop.load(new FileInputStream("src/druid.properties"));
    //2. 获取连接池对象
    DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);    
    //3. 获取数据库连接 Connection
    Connection conn = dataSource.getConnection();    
    //4. 定义SQL语句
    sql = "select user_id from `user`";
    //5. 根据sql语句获取PreparedStatement对象
    PreparedStatement pstmt = conn.prepareStatement(sql);
    //6. 先查询,获取查询结果
    ResultSet rs = pstmt.executeQuery();
    //7. 创建集合
    HashMap<Integer,Boolean> users = new HashMap();     //用来存储每个表是否创建
    //8. 集合赋初值
    while (rs.next()) {
        int userId = rs.getInt("user_id");
        users.put(userId,true);         //先全是true
    }
    //9. 检查表是否存在,更新集合中的表是否存在的状态
    for (Integer key : users.keySet()) {
        users.put(key,validateTableExist("u"+key));
    }
    //10. 遍历集合,如果不存在则创建表
    for (Integer key : users.keySet()) {
        if (users.get(key) == false) {       //表不存在
            UserTable ut = new UserTable(key.toString());       //创建对应的表实例
            int cnt = pstmt.executeUpdate(ut.toString());
            System.out.println(cnt);        //打印受影响行数
        }
    }
    rs.close();
}

三、检查表是否存在的方法

public boolean validateTableExist(String tableName){
    boolean flag = false;
    try {
        DatabaseMetaData meta = conn.getMetaData();
        String type [] = {"TABLE"};
        rs = meta.getTables(null, null, tableName, type);
        flag = rs.next();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return flag;
}

这部分也可以参考我自己的宁外一篇博客,详细介绍了检查表的方法,链接如下。

aJJava之JDBC创建表之前判断表是否存在_@陆先生的博客-CSDN博客Java之JDBC的创建表之前检查表是否存在https://blog.csdn.net/qq_44587785/article/details/125764643?spm=1001.2014.3001.5502Java之JDBC的创建表之前检查表是否存在https://blog.csdn.net/qq_44587785/article/details/125764643?spm=1001.2014.3001.5502Java之JDBC的创建表之前检查表是否存在https://blog.csdn.net/qq_44587785/article/details/125764643?spm=1001.2014.3001.5502Java之JDBC的创建表之前检查表是否存在https://blog.csdn.net/qq_44587785/article/details/125764643?spm=1001.2014.3001.5502Java之JDBC的创建表之前检查表是否存在https://blog.csdn.net/qq_44587785/article/details/125764643?spm=1001.2014.3001.5502Java之JDBC的创建表之前检查表是否存在https://blog.csdn.net/qq_44587785/article/details/125764643?spm=1001.2014.3001.5502

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@陆先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值