你能帮忙解决这个问题吗?
我正在尝试创建然后使用名为TIGER的数据库.
如果我在MySQL中创建数据库并且运行完美,我没有问题.
我想做的是从Java创建它.因此,当第一次运行代码时,它会在初始启动时创建数据库.如果可能的话,我想用干净的方法把它装箱.
有人可以告诉我你实际定位代码的位置
这是代码
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,因为数据库不存在.此时我想捕获异常并在此时创建数据库.但是当我尝试这个时,它不会创建数据库.