gpt4 book ai didi

java - INSERT 中的 Mysql 错误 'field list' 中的未知列

转载 作者:行者123 更新时间:2023-11-30 00:59:29 26 4
gpt4 key购买 nike

您好,我在 Java 中使用 MySQL 时遇到错误。每次我尝试插入到表中时(当我运行下面的“testOne”时),我都会收到此未知列错误。

public static void sendMessage(String username, String recipientName, String message, String title) {                
DateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
Date date = new Date();
String stringDate = dateFormat.format(date);
String query = "INSERT INTO messages" + " (fromUser, toUser, message, title, dateCreated) VALUES ('" +
username + "', '" + recipientName + "', '" + message + "', '" + title + "', '" + stringDate + "')";
DBConnection.getInstance().executeQuery(query);
}

public void testOne(){
Message.sendMessage("user1", "user2", "hows it going", "hi");
}

CREATE TABLE messages (
id int AUTO_INCREMENT,
fromUser varchar(255),
toUser varchar(255),
mType int,
message TEXT,
title TEXT,
dateCreated varchar(255),
seen TINYINT(1),
quizID int,
PRIMARY KEY(pID)
);

public ResultSet executeQuery(String query){
ResultSet rs= null;
try {
java.sql.Statement stmt = connection.createStatement();
// this is the trick -- you need to pass different SQL to different methods
if (query.startsWith("SELECT")) {
rs = stmt.executeQuery(query);
} else if (query.startsWith("UPDATE") || query.startsWith("INSERT")
|| query.startsWith("DELETE")) {
stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
rs = stmt.getGeneratedKeys();
} else {
stmt.execute(query, Statement.RETURN_GENERATED_KEYS);
rs = stmt.getGeneratedKeys();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return rs;
}

public static DBConnection getInstance() {
if (instance == null)
instance = new DBConnection(DBConnection.rootDirectory);
return instance;
}

但是每次运行测试时都会出现此错误 - “字段列表”中的未知列“消息”请帮忙!!!

最佳答案

您在哪一列中收到错误?我面临着类似的问题。我在值字符串中的“,”之前和之后放置了空格,效果很好。根据您的情况,请尝试在逗号之前添加空格。

关于java - INSERT 中的 Mysql 错误 'field list' 中的未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20273316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com