我已经知道如何使用MySQL命令行创建新数据库。
Is there a tool provided by Eclipse Luna for creating MySQL databases ?
提前致谢。
答案 0 :(得分:1)
为什么在使用Java应用程序创建数据库和表这么简单时需要一个工具?
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();
}
}
}