可以从Java创建一个MySQL数据库吗?
我只见过这样的连接URL示例,其中数据库名称在URL中指定:
String url="jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection( url, "cb0", "xxx" );只有登录名和密码时,如何创建MySQL数据库?
发布于 2009-04-04 17:27:39
数据库在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");发布于 2012-11-30 23:19:09
要通过Java代码创建数据库,必须使用executeUpdate(sql)而不是executeQuery(sql);,并以根用户身份连接到mysql数据库:
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull",
"root", "root"
);
Statement st = connection.createStatement();
st.executeUpdate(sql);
st.close();发布于 2009-05-17 12:24:48
解决这类问题的一种优雅方法是使用Apache中的DDL Utils。它不仅服务于允许执行(外部可配置的)DDL的基本目的,还将使您的应用程序数据库独立。
https://stackoverflow.com/questions/717436
复制相似问题