【问题标题】:Create MySQL database from Java从 Java 创建 MySQL 数据库
【发布时间】:2010-10-17 13:48:51
【问题描述】:

是否可以从 Java 创建 MySQL 数据库?

我只见过这样的连接 URL 示例,其中在 URL 中指定了数据库名称:

String url="jdbc:mysql://localhost:3306/test";

Connection con = DriverManager.getConnection( url, "cb0", "xxx" );

如果我只有登录名和密码,如何创建 MySQL 数据库?

【问题讨论】:

    标签: java mysql database


    【解决方案1】:

    您可以使用以下几行:

        try {
            String databaseName = "dbName";
            String userName = "root";
            String password = "yourPassword";
    
            String url = "jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull";
            Connection connection = DriverManager.getConnection(url,username, password);
    
            String sql = "CREATE DATABASE " + databaseName;
    
            Statement statement = connection.createStatement();
            statement.executeUpdate(sql);
            statement.close();
            JOptionPane.showMessageDialog(null, databaseName + " Database has been created successfully", "System Message", JOptionPane.INFORMATION_MESSAGE);
    
        } catch (Exception e) {
            e.printStackTrace();
    }
    

    【讨论】:

    • Pm我你的电子邮件地址,以便我可以发送 JAR 文件。
    • 谢谢。但请注意这个问题是 6 年前的问题 :)
    【解决方案2】:

    为了让事情变得更简单,您可以使用 NetBeans 6.5,它让设置 SQL 数据库变得如此简单。我现在正在使用它们,它是 GUI 布局和数据库连接的救星。下面是一些关于如何从 NetBeans 连接到 mysql 数据库的代码:

        //these are variables i declare in the beginning of my code
        public static final String DRIVER = "com.mysql.jdbc.Driver";
        public static final String DATABASE_URL = "jdbc:mysql://localhost:3306/jtschema";
        private Connection connection = null;
        public static Statement statement = null;
    
        public void initSQLServer() {
            try {
                Class.forName(DRIVER).newInstance();
                try {
                    connection = DriverManager.getConnection(DATABASE_URL, "root", "Dropatrain!248");
                    statement = connection.createStatement();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    

    【讨论】:

    • NetBeans wiki 拥有您在项目中设置 sql 表所需的所有信息!
    【解决方案3】:

    要通过Java代码创建数据库,你必须使用executeUpdate(sql)而不是executeQuery(sql);并以root身份连接到mysql数据库:

    connection =  DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull",
        "root", "root"
    );
    Statement st = connection.createStatement();
    st.executeUpdate(sql);
    st.close();
    

    【讨论】:

      【解决方案4】:

      解决此类问题的一种优雅方法是使用来自 Apache 的 DDL Utils。它不仅可以实现允许执行(外部可配置的)DDL 的基本目的,还可以使您的应用程序数据库独立。

      【讨论】:

        【解决方案5】:

        jdbc 连接中不需要数据库,因此您可以执行http://forums.mysql.com/read.php?39,99321,102211#msg-102211http://marc.info/?l=mysql-java&m=104508605511590&w=2 推荐的操作:

        Conn = DriverManager.getConnection
        ("jdbc:mysql://localhost/?user=root&password=rootpassword"); 
        s=Conn.createStatement();
        int Result=s.executeUpdate("CREATE DATABASE databasename");
        

        【讨论】:

          猜你喜欢
          • 2013-10-01
          • 2012-05-15
          • 1970-01-01
          • 1970-01-01
          • 2013-10-01
          • 2018-03-02
          • 2017-05-21
          相关资源
          最近更新 更多