2017-05-07 145 views
0
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'IDPaciente' 

我得到这个异常。这是我的代码:SQLServer例外:列名无效

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管理工作室2012),我正确连接到我的数据库,似乎没有任何工作。

我会做什么错? 谢谢!

+2

删除所有quetes' – Sami

+0

做ü试图执行对直接SSMS的代码,是养了同样的错误? –

+0

@RyanTuosto它有一个名为“IDPaciente”的列,是 – Gazelle

回答

3

删除引号,请尝试:

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值。

+0

取得了一些进展,但它仍然无法正常工作。拿出报价给出一些不同的错误。如果我插入一个像“John”这样的值,它说我在“John”或“John”附近有一个不正确的语法是一个无效的列名。哎呀让我先检查它,没有看到你的编辑。 – Gazelle

+0

保留“Strig”值的引号 – Sami

+2

@Gazelle这是因为字符串文字应该用单引号(而不是双引号)括起来。但真正的解决方案由YCF_L在答案中提供:不要使用字符串连接,请使用带有参数的预准备语句。 –

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'. 
3

您的代码是不安全的,你可以很容易地得到语法错误或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一个int,日期setDate , 等等。

3

你有四个方面的问题,但只有第一个是给你的当前错误:

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

  2. 为了防止SQL Injection攻击,即黑客会窃取你的数据和删除表,并防止潜在的语法错误,从未建立与用户输入的字符串,一个SQL语句,使用字符串连接。始终使用PreparedStatement

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

  4. 请勿将executeQuery()用于INSERT语句。使用executeUpdate()。正如Javadoc中说:

    Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE ; or an SQL statement that returns nothing, such as a DDL statement.

所以,你的代码应该是:

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(); 
}