我正在尝试插入postgresql db date,但是当我插入它时,我收到了这个错误:
INSERTING操作时发生错误:org.postgresql.util.PSQLException:错误:列“我写的文字”不存在“
我不明白我的编码错误是什么,程序试图在值中找到列?
但是当我尝试输出这个表时,它输出没有错误
WORKERDAO课程
public static void insertWrk(String name, String post) throws SQLException, ClassNotFoundException {
//Declare a INSTERT statement
String updateStmt ="INSERT INTO worker(full_name,post)" +
"VALUES" +
"("+name+","+post+");";
//Execute INSTERT operation
try {
DBUtil.dbExecuteUpdate(updateStmt);
} catch (SQLException e) {
System.out.print("Error occurred while INSTERING Operation: " + e);
throw e;
}
}
DBUTIL类
public static void dbExecuteUpdate(String sqlStmt) throws SQLException, ClassNotFoundException {
//Declare statement as null
Statement stmt = null;
try {
//Connect to DB
dbConnect();
//Create Statement
stmt = conn.createStatement();
//Run executeUpdate operation with given sql statement
stmt.executeUpdate(sqlStmt);
} catch (SQLException e) {
System.out.println("Проблема в выполнение executeUpdate операции : " + e);
throw e;
} finally {
if (stmt != null) {
//Close statement
stmt.close();
}
//Close connection
dbDisconnect();
}
}
WorkerController类
@FXML
private void insertWorker (ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
try {
WorkerDAO.insertWrk(nameText.getText(),postCombo.getSelectionModel().getSelectedItem());
} catch (SQLException e) {
System.out.println("Problem occurred while inserting worker " + e);
throw e;
}
}
答案 0 :(得分:2)
您需要在您要插入的数据周围加上引号:
String updateStmt ="INSERT INTO worker(full_name,post)" +
"VALUES" +
"("+name+","+post+");";
应该是:
String updateStmt ="INSERT INTO worker(full_name,post)" +
"VALUES" +
"('"+name+"',"'+post+'");";
请注意,您使用此方法冒SQL Injection attacks冒险,请参阅使用parameterised insert。 This site列出了更多细节。