java org.h2.jdbc.JdbcSQLException:未找到列“Salman”;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35646432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.h2.jdbc.JdbcSQLException: Column "Salman" not found;
提问by Salman Lashkarara
I have tried to run the following test in my spring application.
我试图在我的 spring 应用程序中运行以下测试。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=App1Application.class)
@Sql(scripts="customerTest.sql")
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
public class customerTest {
@Autowired
customerRepository customerDB;
@Test
public void countRecords(){
assertThat(customerDB.count(),is(2l));
}
}
and in the customerTest.sqlfile i have:
在customerTest.sql文件中,我有:
insert into customer(id,name,lastname) values(1,"name","lastname");
here is my customerclass
这是我的客户课
@Entity
@Data
public class customer {
@Id
@GeneratedValue
int id;
String name;
String lastname;
}
and i use jpatoo:
我也使用jpa:
public interface customerRepository extends JpaRepository<customer,Long>{
}
The problem is, when i run the test i face with the error:
问题是,当我运行测试时,我遇到了错误:
org.h2.jdbc.JdbcSQLException: Column "Salman" not found; SQL statement:
insert into customer(id,name,lastname) values(1,"name","lastname")
Meanwhile "Salman" is a value and not a column?
同时“Salman”是一个值而不是一个列?
Please pay attention, i am using spring-mvc so there is no DatabaseI have only my models (customer
) made by code.
请注意,我使用的是 spring-mvc,所以没有数据库我只有我的模型(customer
)由代码制作。
回答by Salman Lashkarara
The behavior of compiler to make such an error is still a question for me,
but I managed to handle this error using this single quote ''
rather than double quote ""
编译器产生这种错误的行为对我来说仍然是一个问题,但我设法使用这个单引号''
而不是双引号来处理这个错误""
I use this
我用这个
insert into customer(id,name,lastname) values(1,'name','Lastname')
rather than
而不是
insert into customer(id,name,lastname) values(1,"name","Lastname")
回答by Nimesha Kalinga
Try to use '' (single quotes) instead of "" (double quotes). I think it's a problem in H2 library
尝试使用 ''(单引号)而不是“”(双引号)。我认为这是 H2 库中的问题
回答by Lokesh Pandey
I know this is bit late for this answer but it still needs an improvement.
Do not pass or insert values like this in database, there is always a chances of SQL injection. What you can do is you can use PreparedStatement
and then pass a value to your query by using setString()
or setInteger()
or what ever value you want to pass. So your query will have a better performance and also the security threats are minimum. For e.g.,
我知道这个答案有点晚了,但它仍然需要改进。不要在数据库中传递或插入这样的值,总是有 SQL 注入的机会。您可以做的是您可以使用PreparedStatement
,然后通过使用setString()
或setInteger()
或您想要传递的任何值将值传递给您的查询。因此,您的查询将具有更好的性能,并且安全威胁最小。例如,
String insert = "INSERT into table_name(id, name,lastname) values (?, ?, ?)"
PreparedStatement insertStatement = conn.prepareStatement(insert);
insertStatement.setInteger(1, id);
insertStatement.setString(2, name);
insertStatement.setString(3, lastname);
insertStatement.executeUpdate();
insertStatement.close();
where conn
is the connection made to your database. And do not forget to close the PreparedStatement
and connection.
conn
与数据库的连接在哪里。并且不要忘记关闭PreparedStatement
和连接。
回答by Ivan R
Try to use FULL PATH to your database in project. This problem was solved after relative path was excluded. There is some bug with "~/RELATIVE_PATH" using in last H2 database versions! Bad example: "jdbc:h2:~\com\project\db\h2\h2testdb" GOOD EXAMPLE: "jdbc:h2:C:\Users\UserName\IdeaProjects\projectname\com.project\src\main\java\com\test\db\h2\h2testdb"
尝试在项目中使用 FULL PATH 到您的数据库。排除相对路径后,这个问题就解决了。在最新的 H2 数据库版本中使用“~/RELATIVE_PATH”存在一些错误!不好的例子:“jdbc:h2:~\com\project\db\h2\h2testdb” 好的例子:“jdbc:h2:C:\Users\UserName\IdeaProjects\projectname\com.project\src\main\java\com \test\db\h2\h2testdb"