简体   繁体   中英

org.h2.jdbc.JdbcSQLException: Column “Salman” not found;

I have tried to run the following test in my spring application.

@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.sql file i have:

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

here is my customer class

@Entity
@Data

public class customer {

    @Id
    @GeneratedValue
    int id;

    String name;
    String lastname;
    }

and i use jpa too:

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?

Please pay attention, i am using spring-mvc so there is no Database I have only my models ( customer ) made by code.

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")

Try to use '' (single quotes) instead of "" (double quotes). I think it's a problem in H2 library

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 eg,

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.

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"

You must set the name of the database table field in the entity module (in your case customer.java class) as follows:

@Column(name = "field_name_in_the_table")
String name;

Then in your SQL script file customerTest.sql write the database filed name as follows:

INSERT INTO customer (id, field_name_in_the_table, lastname) values(1,"name","lastname");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM