java jdbc创建数据库数据表

     public boolean createDB() {
        // 1.数据库名
        String DBName = env;
        // 数据库驱动
        String mysqlDriver = "com.mysql.jdbc.Driver";
        // 数据库地址
        String url = "jdbc:mysql://localhost:3306/";

        String username = "root";
        String password = "123456";
        Connection conn = null;
        Connection newConn = null;

        try {
            // 将mysql驱动注册到DriverManager中去
            Class.forName(mysqlDriver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            // 建表语句
            String tableSql = "create table t_user (username varchar(50) not null primary key,"
                    + "password varchar(20) not null ); ";
            // 建库语句
            String databaseSql = "create database " + DBName;
            // 链接数据库
            conn = DriverManager.getConnection(url, username, password);
            // 用于执行静态SQL语句并返回其产生的结果的对象
            Statement smt = conn.createStatement();
            if (conn != null) {
                System.out.println("数据库连接成功!");
                // 执行建库语句
                smt.executeUpdate(databaseSql);
                // 链接新建的数据库
                newConn = DriverManager.getConnection(url + DBName, username, password);
                if (newConn != null) {
                    System.out.println("已经连接到新创建的数据库:" + DBName);
                    Statement newSmt = newConn.createStatement();
                    // 执行建表语句
                    int i = newSmt.executeUpdate(tableSql);
                    // DDL语句返回值为0       
                    if (i == 0) {
                        System.out.println(tableSql + "表已经创建成功!");
                    }
                }
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        return true;
    }

也可以采用读取.sql文件的形式创建数据库表,把上面建表语句替换如下:

 if (newConn != null) {
                    System.out.println("已经连接到新创建的数据库:" + DBName);
                    // 找到SQL脚本(.sql文件)
                    ClassPathResource rc = new ClassPathResource("sqlfile/" + env + ".sql");
                    EncodedResource er = new EncodedResource(rc, "utf-8");
                    boolean exists = rc.exists();
                    // 当.sql文件存在
                    if (exists == true) {
                        try {
                            // 执行该文件
                            ScriptUtils.executeSqlScript(newConn, er);
                        } catch (Exception e) {
                            logger.error("创建数据表失败!", e);
                            flag = false;
                        }
                    } else {
                        flag = false;
                        logger.error("该产品的SQL脚本(.sql文件)不存在");
                    }
                }

sqlfile的位置如下:
在这里插入图片描述

  • 6
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值