SQLServer异常:无效的列名

3
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'IDPaciente'

我收到了这个异常。以下是我的代码:
    String query = "INSERT INTO Paciente('IDPaciente', 'NomePaciente', 'IdadePaciente', 'LocalidadePaciente') VALUES('"+IDTextField.getText()+"', '"+NomeTextField.getText()+"', '"+IdadeTextField.getText()+"', '"+LocalidadeTextField.getText()+"')";
    try
    {
        st = con.DatabaseConnection().createStatement();
        rs = st.executeQuery(query);
    }

我猜测问题可能出在查询本身。
我已经搜索了很多,但找不到解决方法。我尝试过刷新缓存、更改模式内的权限、重启 SQL Server(我使用的是 SQL Server Management Studio 2012),我正确地连接到我的数据库,但似乎什么都不起作用。
我可能做错了什么? 谢谢!

2
删除所有单引号 ' ' - Ilyes
你尝试直接在SSMS中执行代码了吗?是否出现了相同的错误? - ahmed abdelqader
@RyanTuosto 是的,它有一个名为“IDPaciente”的列。 - Gazelle
@ahmedabdelqader 是的,我已经尝试直接执行它,但仍然出现相同的错误。 - Gazelle
1
单引号 ' 用于引用文本字面量,而不是列名。请删除所有单引号,并使用 PreparedStatement? 参数标记,而不是字符串连接。 - Andreas
@ Gazelle,所以按照我们说的去掉引号,有关详细信息,请参阅下面的答案。 - ahmed abdelqader
5个回答

4

去除引号,尝试使用:

String query = "INSERT INTO Paciente(IDPaciente, NomePaciente, IdadePaciente, LocalidadePaciente) VALUES('"+IDTextField.getText()+"', '"+NomeTextField.getText()+"', '"+IdadeTextField.getText()+"', '"+LocalidadeTextField.getText()+"')";
try
{
    st = con.DatabaseConnection().createStatement();
    rs = st.executeQuery(query);
}

移除 INT 值的引号。


已经取得了一些进展,但仍然无法正常工作。去掉引号后会出现一些不同的错误。如果我插入像“John”这样的值,它会说我在“John”附近有一个不正确的语法或“John”是无效的列名。哎呀,让我先检查一下,没看到你的编辑。 - Gazelle
保留 String 值的引号。 - Ilyes
2
@Gazelle 这是因为字符串字面值应该用单引号括起来(而不是双引号)。但真正的解决方案在 YCF_L 的答案中提供:不要使用字符串连接,而是使用带参数的预处理语句。 - Mark Rotteveel
@Sami 还有一件事,为什么这个语句在这个上下文中没有返回结果集?先谢谢了。 - Gazelle

4

您的代码不安全,很容易出现语法错误或SQL注入问题。建议使用PreparedStatement来代替。

您的查询存在问题,列名不应该放在''之间,建议改为:

String query = "INSERT INTO Paciente(IDPaciente, NomePaciente, IdadePaciente, "
        + "LocalidadePaciente) VALUES(?, ?, ?, ?)";

try (PreparedStatement insert = con.prepareStatement(query)) {
    insert.setString(1, IDTextField.getText());
    insert.setString(2, NomeTextField.getText());
    insert.setString(3, IdadeTextField.getText());
    insert.setString(4, LocalidadeTextField.getText());

    insert.executeUpdate();
}

如果您的列是整数类型,您必须使用 setInt ;如果是日期类型,则使用 setDate ,以此类推。

3

您有四个问题,但只有第一个问题导致当前的错误:

  1. 单引号 (') 用于引用文本字面值,而不是列名。在 MS SQL Server 中,您可以使用双引号 (") 或方括号 ([]) 引用列名,但您根本不需要引用它们。

  2. 为了防止 SQL 注入 攻击,黑客将窃取您的数据并删除您的表,并且为了防止潜在的语法错误,永远不要使用字符串连接构建带有用户输入的字符串的 SQL 语句,而应始终使用 PreparedStatement

  3. 始终清理您的资源,最好使用 try-with-resources

  4. 不要对 INSERT 语句使用 executeQuery()。使用 executeUpdate()。如 javadoc 所述:

    在此 PreparedStatement 对象中执行 SQL 语句,该对象必须是 SQL 数据操作语言 (DML) 语句,例如 INSERTUPDATEDELETE;或者返回空值的 SQL 语句,例如 DDL 语句。

所以,你的代码应该是:

String query = "INSERT INTO Paciente" +
              " (IDPaciente, NomePaciente, IdadePaciente, LocalidadePaciente)" +
              " VALUES (?, ?, ?, ?)";
try (PreparedStatement st = con.DatabaseConnection().prepareStatement(query)) {
    st.setString(1, IDTextField.getText());
    st.setString(2, NomeTextField.getText());
    st.setString(3, IdadeTextField.getText());
    st.setString(4, LocalidadeTextField.getText());
    st.executeUpdate();
}

0

请从您的列名中删除引号。

"INSERT INTO Paciente(IDPaciente, NomePaciente, IdadePaciente, LocalidadePaciente) VALUES('"+IDTextField.getText()+"', '"+NomeTextField.getText()+"', '"+IdadeTextField.getText()+"', '"+LocalidadeTextField.getText()+"')"

0

列名没有用引号括起来,去掉引号再试一次。

演示:

Create table MyTable (id int , name varchar (50))
go
insert into MyTable (id,name) values (1 , 'ahmed')

结果:

(1 row(s) affected)

尝试再次使用引号插入它们。
insert into MyTable ('id','name') values (1 , 'ahmed')

结果:

Msg 207, Level 16, State 1, Line 3
Invalid column name 'id'.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'name'.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接