从java在mysql中创建数据库
你能帮我解决这个问题吗 我正在尝试创建并使用一个名为TIGER的数据库 如果我在MySQL中创建数据库并且它运行得很好,我就没有问题了 我想做的是从Java创建它。因此,当代码第一次运行时,它将创建数据库作为初始启动的一部分。如果可能的话,我想用一种干净的方法来包装它 有人能告诉我你到底把代码放在哪里吗 这是密码从java在mysql中创建数据库,java,mysql,database,jdbc,Java,Mysql,Database,Jdbc,你能帮我解决这个问题吗 我正在尝试创建并使用一个名为TIGER的数据库 如果我在MySQL中创建数据库并且它运行得很好,我就没有问题了 我想做的是从Java创建它。因此,当代码第一次运行时,它将创建数据库作为初始启动的一部分。如果可能的话,我想用一种干净的方法来包装它 有人能告诉我你到底把代码放在哪里吗 这是密码 private String jdbcDriver = "com.mysql.jdbc.Driver"; private String dbAddress = "jdb
private String jdbcDriver = "com.mysql.jdbc.Driver";
private String dbAddress = "jdbc:mysql://localhost:3306/";
private String dbName = "TIGER";
private String userName = "root";
private String password = "";
private PreparedStatement statement;
private ResultSet result;
private Connection con;
public DbStuff() {
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(dbAddress + dbName, userName, password);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
下面是创建数据库的代码
con = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword");
statement = Conn.createStatement();
int myResult = statement.executeUpdate("CREATE DATABASE TIGER");
提前感谢,感谢您的帮助
很抱歉我犯了这些错误,因为我是一个长期的读者,但又是一个新的作家
当我尝试运行代码的主要部分时,它会生成一个SQLException,因为数据库不存在。此时,我希望捕获异常并在此时创建数据库。但是当我尝试这个方法时,它不会创建数据库 尝试使用此代码
public class DbStuff {
private static String jdbcDriver = "com.mysql.jdbc.Driver";
private static String dbName = "TIGER";
public static void main(String[] args) throws Exception {
Class.forName(jdbcDriver);
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=");
Statement s = conn.createStatement();
int Result = s.executeUpdate("CREATE DATABASE "+dbName);
}
}
你可以一直跑
int myResult = statement.executeUpdate("CREATE DATABASE IF NOT EXISTS TIGER;")
如果在项目启动时运行,它只需检查数据库是否存在,如果不存在,它将创建新数据库
参考:关于一个不相关的(问题)注释:
我认为以编程方式生成数据库从来都不是一个好主意。您最好使用数据库附带的实用工具。使用MySQL的每个人几乎肯定都熟悉该实用工具,而不必熟悉Java代码、更改代码、编译代码和运行代码来进行更改。此外,该实用程序还具有Java代码可能没有的丰富功能集,例如分配权限、索引、设置外键等。首先
我要感谢所有回答这一问题的人,得到不同的观点和意见确实很有帮助
这是我共同提出的解决方案
public class DbStuff {
private String jdbcDriver = "com.mysql.jdbc.Driver";
private String dbAddress = "jdbc:mysql://localhost:3306/";
private String userPass = "?user=root&password=";
private String dbName = "TIGER";
private String userName = "root";
private String password = "";
private PreparedStatement statement;
private ResultSet result;
private Connection con;
public DbStuff() {
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(dbAddress + dbName, userName, password);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
createDatabase();
}
}
private void createDatabase() {
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(dbAddress + userPass);
Statement s = con.createStatement();
int myResult = s.executeUpdate("CREATE DATABASE " + dbName);
}
catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
请随意评论任何代码。我知道使用e.printStackTrace()不是最好的方法,不要担心它以后会被修改。需要注意和纠正的几点-
private Connection con代码>但您将其用作语句=Conn.createStatement()代码>
private PreparedStatement的引用代码>但您正在创建语句的实例Statement=Conn.createStatement()代码>。
你的密码应该是-
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword");
Statement statement = con.createStatement();
int myResult = statement.executeUpdate("CREATE DATABASE IF NOT EXISTS TIGER"); //should get 0
}
catch (SQLException e) {
System.out.println("Database creation failed");
e.printStackTrace();
}
- 执行的语句是影响零行的update语句
- 执行的语句是DDL语句
解决问题的简单方法是:
package Database;
import java.sql.*;
public class Database {
public static void main(String[] args) {
final String URl = "JDBC:mysql:/localhost:3306/info";
final String id = "root";
final String password = "";
try
{
Connection con1 = DriverManager.getConnection(URl,id,password);
Statement s1 = con1.createStatement();
String s = "CREATE DATABASE CLASSIFIED";
s1.executeUpdate(s);
}
catch(SQLException err)
{
System.out.println(err.getMessage());
}
}
}
我建议使用这段代码
private String jdbcDriver = "com.mysql.jdbc.Driver";
private String dbAddress = "jdbc:mysql://localhost:3306/TIGER?createDatabaseIfNotExist=true";
private String userName = "root";
private String password = "";
private PreparedStatement statement;
private ResultSet result;
private Connection con;
public DbStuff() {
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(dbAddress, userName, password);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
可能重复,不确定问题是什么。您在执行上面的代码时是否出错,或者您不知道如何执行它?我认为这是创建数据库的最佳方法。我们不需要使用这个createDatabaseIfNotExist=true来执行命令