从 Java 创建 MySQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/717436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create MySQL database from Java
提问by cb0
Is it possible to create a MySQL database from Java?
是否可以从 Java 创建 MySQL 数据库?
I have only seen connection URLs examples like this where the database name is specified in the URL:
我只见过这样的连接 URL 示例,其中在 URL 中指定了数据库名称:
String url="jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection( url, "cb0", "xxx" );
How can I create a MySQL database when I only have a login name and password?
当我只有登录名和密码时,如何创建 MySQL 数据库?
采纳答案by Jeremy Stanley
The database isn't required in the jdbc connection, so you can do something like recommended at http://forums.mysql.com/read.php?39,99321,102211#msg-102211and http://marc.info/?l=mysql-java&m=104508605511590&w=2:
jdbc 连接中不需要数据库,因此您可以执行类似于http://forums.mysql.com/read.php?39,99321,102211#msg-102211和http://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");
回答by durrellp
To make things even easier, you can use NetBeans 6.5 and it makes setting up SQL databases SO much easier. I'm using them right now and its a lifesaver on GUI layouts and database connections. Here's some code on how I connect to mysql database from NetBeans:
为了让事情变得更简单,您可以使用 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);
}
}
回答by pugmarx
An elegant approach to such issues is using DDL Utilsfrom Apache. Not only would it serve the basic purpose of allowing to execute your (externally configurable) DDLs, but also would make your application database independent.
解决此类问题的一种优雅方法是使用Apache 的DDL Utils。它不仅可以满足允许执行(外部可配置的)DDL 的基本目的,而且可以使您的应用程序数据库独立。
回答by PKhode
To create database through Java code, you must use executeUpdate(sql)
instead of executeQuery(sql);
and connect to the mysql
database as root:
要通过 Java 代码创建数据库,您必须使用executeUpdate(sql)
代替executeQuery(sql);
并以mysql
root 身份连接到数据库:
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull",
"root", "root"
);
Statement st = connection.createStatement();
st.executeUpdate(sql);
st.close();
回答by Richard Flores
You can use this lines:
您可以使用此行:
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();
}