我花了 4 天时间在 java 中使用 SQL 搜索新的数据库创建。我发现我可以通过使用以下代码来做到这一点,但我没有得到我想要的。
据我所知,没有创建数据库。如何确定数据库是否已创建?
我的代码是
public class DataManaging {
private Connection Connecter;
private Statement Stat;
private ResultSet Result;
private String pass,user;
public DataManaging(){
user = "root";
pass = "root";
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
try {
Connecter = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306", user, pass);
Stat = Connecter.createStatement();
Stat.executeUpdate("CREATE DATABASE IF NOT EXISTS FATEH;");
Stat.executeUpdate("USE FATEH");
Stat.executeUpdate("CREATE TABLE IF NOT EXISTS ROOMS(N INT auto_increment PRIMARY key,Floor INT NOT NULL,Class VARCHAR(1) NOT NULL,Situation VARCHAR(10),Reservation_Date DATE,Duration INT,Reserver_Name VARCHAR(40) NOT NULL)");
} 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);
}
}
}
}
运行代码后我得到
BUILD SUCCESSFUL (total time: 0 seconds)