Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java org.h2.jdbc.JdbcSQLException:列";“萨尔曼”;未找到;_Java_Spring Mvc - Fatal编程技术网

Java org.h2.jdbc.JdbcSQLException:列";“萨尔曼”;未找到;

Java org.h2.jdbc.JdbcSQLException:列";“萨尔曼”;未找到;,java,spring-mvc,Java,Spring Mvc,我已尝试在我的spring应用程序中运行以下测试 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes=App1Application.class) @Sql(scripts="customerTest.sql") @DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD) public class customerTest {

我已尝试在我的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));

    }   
}
customerTest.sql文件中,我有:

insert into customer(id,name,lastname) values(1,"name","lastname");
这是我的客户课程

@Entity
@Data

public class customer {

    @Id
    @GeneratedValue
    int id;

    String name;
    String lastname;
    }
我也使用jpa:

public interface customerRepository  extends JpaRepository<customer,Long>{

}
同时,“Salman”是一个值而不是一列

请注意,我使用的是spring mvc,因此没有数据库
我只有我的模型(
customer
)是由代码生成的。

编译器犯这种错误的行为对我来说仍然是个问题, 但是我用单引号
''
而不是双引号
''
处理了这个错误

我用这个

insert into customer(id,name,lastname) values(1,'name','Lastname')
而不是

 insert into customer(id,name,lastname) values(1,"name","Lastname")

尝试使用“”(单引号)而不是“”(双引号)。我认为这是H2库中的一个问题

我知道现在回答这个问题有点晚了,但它仍然需要改进。 不要像这样在数据库中传递或插入值,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();

其中
conn
是与数据库的连接。不要忘记关闭
PreparedStatement
和连接。

尝试在project中使用数据库的完整路径。排除相对路径后,此问题得到解决。在上一个H2数据库版本中使用“~/RELATIVE\u 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”

您必须在实体模块(在您的例子中是
customer.java
class)中设置数据库表字段的名称,如下所示:

@Column(name = "field_name_in_the_table")
String name;
INSERT INTO customer (id, field_name_in_the_table, lastname) values(1,"name","lastname");
然后在您的SQL脚本文件
customerTest.SQL
中,按如下方式写入数据库文件名:

@Column(name = "field_name_in_the_table")
String name;
INSERT INTO customer (id, field_name_in_the_table, lastname) values(1,"name","lastname");

那么文件“customerTest.sql”包含什么呢?它包含两个插入查询,正如我提到的,
是sql中的保留字符。因此,
”Salman“
不是您期望的字符串值,而是一个对象标识符,在
INSERT
语句的上下文中,该标识符被推断为列名。SQL中的字符串需要用单引号括起来<代码>“Salman”应该是
“Salman”
“Lashkarara”
应该是
“Lashkarara”