首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Eclipse露娜:创建新的MySQL数据库的工具

问Eclipse露娜:创建新的MySQL数据库的工具
EN

Stack Overflow用户
提问于 2014-12-29 18:18:31
回答 1查看 2.1K关注 0票数 1

我已经知道如何使用MySQL命令行创建新的数据库。

Is there a tool provided by Eclipse Luna for creating MySQL databases ?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-29 19:57:40

既然用Java应用程序创建数据库和表非常简单,那么为什么需要一个工具呢?

代码语言:javascript
复制
package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateDatabase implements Runnable {

    private Connection connection;

    private Statement statement;

    private String databaseAddress;
    private String logon;
    private String password;

    public CreateDatabase(String logon, String password) {
        this.logon = logon;
        this.password = password;
        this.databaseAddress = "localhost:3306";
    }

    @Override
    public void run() {
        try {
            connect();
            createDatabase();
//          dropTable("user_account");
            createUserAccountTable();
            commit();
            disconnect();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private void connect() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://"
                + databaseAddress + "/?" + "user=" + logon + "&password="
                + password);
        statement = connection.createStatement();
    }

    private void createDatabase() throws SQLException {
        String sql = "create database if not exists accounts";
        statement.execute(sql);
        displayUpdateCount(sql);
        sql = "use accounts";
        statement.execute(sql);
        displayUpdateCount(sql);
        sql = "set names 'utf8'";
        statement.execute(sql);
        displayUpdateCount(sql);
    }

    private void createUserAccountTable() throws SQLException {
        String sql = "create table if not exists user_account ( " +
                "user_account_number integer not null auto_increment, " +
                "user_account_name varchar(40) not null, " +
                "created_timestamp timestamp not null default current_timestamp, " +
                "last_used_timestamp timestamp not null, " +
                "primary key (user_account_number)) " +
                "engine=InnoDB, " +
                "character set=utf8, " +
                "auto_increment=10000000 ";

        statement.execute(sql);
        displayUpdateCount(sql);
    }

    void dropTable(String tableName) throws SQLException {
        String sql = "drop table if exists " + tableName;
        statement.execute(sql);
        displayUpdateCount(sql);
    }

    private void commit() throws SQLException {
        String sql = "commit";
        statement.execute(sql);
        displayUpdateCount(sql);
    }

    private void displayUpdateCount(String sql) throws SQLException {
        int count = statement.getUpdateCount();
        StringBuilder builder = new StringBuilder();
        builder.append("Executing SQL \"");

        if (sql.length() > 40) builder.append(sql.substring(0, 40));
        else builder.append(sql);

        builder.append("\" resulted in ");
        builder.append(count);
        builder.append(" row");
        if (count != 1) builder.append("s");
        builder.append(" changed");

        System.out.println(builder.toString());
    }

    private void disconnect() throws SQLException {
        statement.close();
        connection.close();
    }

    public static void main(String[] args) {
        if (args.length != 2) {
            String s = "The logon and password for the database " +
                    "must be provided.";
            System.err.println(s);
        } else {
            new CreateDatabase(args[0], args[1]).run();
        }
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27693952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档