如果不存在则创建一个新数据库
Making a new database if not existing
我正在制作一个数据库检查器(如果它存在的话)。如果不是,它应该创建一个新数据库和 tables.
我的问题是我无法创建 table,只能创建数据库。
private void Connect() throws Exception
{
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:///",
"root", "");
st = con.createStatement();
String sql = "CREATE DATABASE dbproject2";
//sql = "DROP DATABASE DBNAME";
st.executeUpdate(sql);
//con = DriverManager.getConnection("jdbc:mysql:///dbproject2","root", "");
st.executeUpdate("CREATE TABLE tbl_admin(username varchar(200),password varchar(200),name varchar(200), PRIMARY KEY(username))");
int i = st.executeUpdate("insert into tbl_admin values('admin','123456','Chris')");
st.executeUpdate("CREATE TABLE tbl_emp(name varchar(200),department varchar(200),startTime time, PRIMARY KEY(name))");
st.executeUpdate("CREATE TABLE tbl_records(cardNo varchar(100),holderNo varchar(100),holderName varchar(100),IODate date,IOTime time,IOGateNo varchar(100),IOGateName varchar(100),IOStatus varchar(100),departmentNo int(10), PRIMARY KEY(name))");
} catch (SQLException sqlException) {
if (sqlException.getErrorCode() == 1007) {
// Database already exists error
Class.forName(Driver);
con = DriverManager.getConnection(url,uname,pword);
st = con.createStatement();
st1 = con.createStatement();
} else {
// Some other problems, e.g. Server down, no permission, etc
sqlException.printStackTrace();
}
} catch (ClassNotFoundException e) {
// No driver class found!
}
您需要重新连接新数据库并使用这些创建新语句。
例如
con = DriverManager.getConnection("jdbc:mysql:///",
"root", "");
st = con.createStatement();
String sql = "CREATE DATABASE dbproject2";
//sql = "DROP DATABASE DBNAME";
st.executeUpdate(sql);
con = DriverManager.getConnection("jdbc:mysql:///dbproject2","root", "");
st = con.createStatement();
st.executeUpdate("CREATE TABLE tbl_admin(username varchar(200),password varchar(200),name varchar(200), PRIMARY KEY(username))");
int i = st.executeUpdate("insert into tbl_admin values('admin','123456','Chris')");
st.executeUpdate("CREATE TABLE tbl_emp(name varchar(200),department varchar(200),startTime time, PRIMARY KEY(name))");
st.executeUpdate("CREATE TABLE tbl_records(cardNo varchar(100),holderNo varchar(100),holderName varchar(100),IODate date,IOTime time,IOGateNo varchar(100),IOGateName varchar(100),IOStatus varchar(100),departmentNo int(10), PRIMARY KEY(name))");
我正在制作一个数据库检查器(如果它存在的话)。如果不是,它应该创建一个新数据库和 tables.
我的问题是我无法创建 table,只能创建数据库。
private void Connect() throws Exception
{
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:///",
"root", "");
st = con.createStatement();
String sql = "CREATE DATABASE dbproject2";
//sql = "DROP DATABASE DBNAME";
st.executeUpdate(sql);
//con = DriverManager.getConnection("jdbc:mysql:///dbproject2","root", "");
st.executeUpdate("CREATE TABLE tbl_admin(username varchar(200),password varchar(200),name varchar(200), PRIMARY KEY(username))");
int i = st.executeUpdate("insert into tbl_admin values('admin','123456','Chris')");
st.executeUpdate("CREATE TABLE tbl_emp(name varchar(200),department varchar(200),startTime time, PRIMARY KEY(name))");
st.executeUpdate("CREATE TABLE tbl_records(cardNo varchar(100),holderNo varchar(100),holderName varchar(100),IODate date,IOTime time,IOGateNo varchar(100),IOGateName varchar(100),IOStatus varchar(100),departmentNo int(10), PRIMARY KEY(name))");
} catch (SQLException sqlException) {
if (sqlException.getErrorCode() == 1007) {
// Database already exists error
Class.forName(Driver);
con = DriverManager.getConnection(url,uname,pword);
st = con.createStatement();
st1 = con.createStatement();
} else {
// Some other problems, e.g. Server down, no permission, etc
sqlException.printStackTrace();
}
} catch (ClassNotFoundException e) {
// No driver class found!
}
您需要重新连接新数据库并使用这些创建新语句。
例如
con = DriverManager.getConnection("jdbc:mysql:///",
"root", "");
st = con.createStatement();
String sql = "CREATE DATABASE dbproject2";
//sql = "DROP DATABASE DBNAME";
st.executeUpdate(sql);
con = DriverManager.getConnection("jdbc:mysql:///dbproject2","root", "");
st = con.createStatement();
st.executeUpdate("CREATE TABLE tbl_admin(username varchar(200),password varchar(200),name varchar(200), PRIMARY KEY(username))");
int i = st.executeUpdate("insert into tbl_admin values('admin','123456','Chris')");
st.executeUpdate("CREATE TABLE tbl_emp(name varchar(200),department varchar(200),startTime time, PRIMARY KEY(name))");
st.executeUpdate("CREATE TABLE tbl_records(cardNo varchar(100),holderNo varchar(100),holderName varchar(100),IODate date,IOTime time,IOGateNo varchar(100),IOGateName varchar(100),IOStatus varchar(100),departmentNo int(10), PRIMARY KEY(name))");