我有一个MySQL表EMPLOYEES
,其格式如下:
CREATE TABLE EMPLOYEES
(
FNAME VARCHAR(15) NOT NULL,
LNAME VARCHAR(15) NOT NULL,
PHONE CHAR(10),
HOURS INT NOT NULL,
EMPLOYEE_NUM CHAR(5) NOT NULL,
PRIMARY KEY(EMPLOYEE_NUM)
);
我正在尝试编写一个允许我操作表的程序。我有以下方法尝试这样做。
void addEmployee(Connection conn) throws SQLException, IOException {
Statement stmt = conn.createStatement();
String fName = readEntry("First name: ");
String lName = readEntry("Last name: ");
String phoneNumber = readEntry("Phone number: ");
String hours = readEntry("Number of hours worked: ");
String employeeNum = readEntry("Employee Number: ");
String query = "INSERT INTO EMPLOYEES VALUES ('" +
fName + "','" + lName + "','" + phoneNumber + "'," + hours + ",'" + employeeNum + "')";
try {
int nrows = stmt.executeUpdate(query);
} catch (SQLException e) {
System.out.println("Error Adding Catalog Entry");
while (e != null) {
System.out.println("Message : " + e.getMessage());
e = e.getNextException();
}
return;
}
stmt.close();
System.out.println("Added Catalog Entry");
}
当我尝试执行此调用时:
First name: fname
Last name: lname
Phone number:
Number of hours worked: 34
Employee Number: 01923
Error Adding Catalog Entry
Message : ORA-00947: not enough values
在这次尝试中,我将“phoneNumber”留空了NULL。在之前的尝试中,我给了它一个电话号码并得到了相同的信息。
有谁知道为什么会这样?
如果缺少重要信息,请告知我,我可以添加。
谢谢, erip
答案 0 :(得分:0)
当您执行insert
时,您应始终列出列。
这有用吗?
INSERT INTO EMPLOYEES(fname, lname, phoneNumber, hours, employee_Num)
VALUES ('" +fName + "','" + lName + "','" + phoneNumber + "'," + hours + ",'" + employeeNum + "')