在 MySQL 中使用 Java 创建 Table 时未选择数据库错误
No database selected Error While Creating Table in MySQL with Java
几天前我用这段代码创建了一个数据库
sql = "Create Database if not exists My_Test_Project";
stmnt.executeUpdate(sql);
并且当时创造了一些 table。现在我正在其中创建两个新的 tables,其中一个使用此查询
sql = "CREATE TABLE if not exists My_Test_Project.Sales_Invoice_Help "
+ "(inv_help_id INTEGER,"
+ "item VARCHAR(255),"
+ "qty INTEGER,"
+ "rate DECIMAL (7, 2),"
+ "total DECIMAL (7, 2),"
+ "sale_inv_id INTEGER,"
+ " PRIMARY KEY (inv_help_id),FOREIGN KEY (sale_inv_id) REFERENCES Sales_Invoice (sale_inv_id))";
stmnt.executeUpdate(sql);
当我执行我的程序时它抛出异常
SEVERE: null
java.sql.SQLException: No database selected
但同时此查询执行成功
sql = "CREATE TABLE if not exists My_Test_Project.Sales_Invoice "
+ "(sale_inv_id INTEGER not NULL, "
+ "date VARCHAR(255), "
+ "acc_name VARCHAR(255),"
+ "due_date VARCHAR(255),"
+ "customer_name VARCHAR(255),"
+ "receipt_no VARCHAR(255),"
+ "freight_charges INTEGER,"
+ "deliver_to VARCHAR(255),"
+ "deliver_date VARCHAR(255),"
+ "total INTEGER,"
+ "discount INTEGER,"
+ "g_total INTEGER ,"
+ " PRIMARY KEY (sale_inv_id))";
stmnt.executeUpdate(sql);
注意: Sales_Invoice table 在顺序和代码中也是第一个。
我不知道为什么会抛出异常。你能指导我吗?
我相信在这一行中您需要添加数据库名称:
REFERENCES Sales_Invoice (sale_inv_id)
现在应该是:
REFERENCES My_Test_Project.Sales_Invoice (sale_inv_id)
几天前我用这段代码创建了一个数据库
sql = "Create Database if not exists My_Test_Project";
stmnt.executeUpdate(sql);
并且当时创造了一些 table。现在我正在其中创建两个新的 tables,其中一个使用此查询
sql = "CREATE TABLE if not exists My_Test_Project.Sales_Invoice_Help "
+ "(inv_help_id INTEGER,"
+ "item VARCHAR(255),"
+ "qty INTEGER,"
+ "rate DECIMAL (7, 2),"
+ "total DECIMAL (7, 2),"
+ "sale_inv_id INTEGER,"
+ " PRIMARY KEY (inv_help_id),FOREIGN KEY (sale_inv_id) REFERENCES Sales_Invoice (sale_inv_id))";
stmnt.executeUpdate(sql);
当我执行我的程序时它抛出异常
SEVERE: null
java.sql.SQLException: No database selected
但同时此查询执行成功
sql = "CREATE TABLE if not exists My_Test_Project.Sales_Invoice "
+ "(sale_inv_id INTEGER not NULL, "
+ "date VARCHAR(255), "
+ "acc_name VARCHAR(255),"
+ "due_date VARCHAR(255),"
+ "customer_name VARCHAR(255),"
+ "receipt_no VARCHAR(255),"
+ "freight_charges INTEGER,"
+ "deliver_to VARCHAR(255),"
+ "deliver_date VARCHAR(255),"
+ "total INTEGER,"
+ "discount INTEGER,"
+ "g_total INTEGER ,"
+ " PRIMARY KEY (sale_inv_id))";
stmnt.executeUpdate(sql);
注意: Sales_Invoice table 在顺序和代码中也是第一个。 我不知道为什么会抛出异常。你能指导我吗?
我相信在这一行中您需要添加数据库名称:
REFERENCES Sales_Invoice (sale_inv_id)
现在应该是:
REFERENCES My_Test_Project.Sales_Invoice (sale_inv_id)