java JDBC编程 实现 mysql create table建表 操作

public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        //String tabeleName = " test (id int ,name varchar(20))";
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java0531?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("*****");
        Connection connection = dataSource.getConnection();
        String sql =  "create table test (id int ,name varchar(20))" ;
        //删除
        //String sql =  "drop table test" ;
        //String sql = "create table" + tabeleName;
        PreparedStatement statement = connection.prepareStatement(sql);
        int ret = statement.executeUpdate();
        if(ret == 0) {
            System.out.println("创建成功");
        } else {
            System.out.println("创建失败");
        }
        statement.close();
        connection.close();
    }
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,可以使用第三方库或框架来实现实体类转 MySQL create table 语句。常用的第三方库有 Mybatis Generator 和 JPA-Auto。这些库可以根据实体类的注解和属性,自动生成对应的 MySQL create table 语句。 示例: 1.使用Mybatis Generator ```xml <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC" userId="root" password="root"> </jdbcConnection> <javaModelGenerator targetPackage="com.mybatis.model" targetProject="src/main/java"> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.dao" targetProject="src/main/java"> </javaClientGenerator> <table tableName="test_table" domainObjectName="TestTable"> <generatedKey column="id" sqlStatement="JDBC" identity="true"/> </table> </context> </generatorConfiguration> ``` 2.使用 JPA-Auto ```java @Entity @Table(name = "employee") public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name", nullable = false) private String name; @Column(name = "age", nullable = false) private Integer age; // getters and setters } ``` 使用jpa-auto 生成对应的 create table 语句 ``` mvn jpa-auto:generate ``` 注意: - 使用第三方库或框架时需要先安装相应的包。 - 上述两个示例只是简单的用法,实际使用时还需要根据需求进行配置。 希望这能帮到你。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值