0
votes

I am trying to develop a java web app. It is connected to a postgresql database. In this database, I have a table called emp99.In it there is a column called company. when I try to add values to the table I get an error which is:

org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [insert into emp99(name,salary,designation,age,surname,department,company,address,working) values('s',1.0,'d',4,'ff',gg,'dd',true )]; nested exception is org.postgresql.util.PSQLException: ERROR: column "gg" does not exist

But I have this column

CREATE TABLE emp99 (
    id int PRIMARY KEY,
    name VARCHAR ( 50 ) ,
    salary float ,
    designation varchar(50),
    age int,
    surname varchar(50),
    department varchar(50),
    company varchar(50),
    address varchar(50),
    working Boolean
);

and this is my adding code:

public int save(Emp p){    
            String sql="insert into emp99(name,salary,designation,age,surname,department,company,address,working) values('"+p.getName()+"',"+p.getSalary()+",'"+p.getDesignation()+"',"+p.getAge()+",'"+p.getSurname()+"',"+p.getCompany()+",'"+p.getAddress()+"',"+p.getWorking()+" )";    
            return template.update(sql);    
        }  
1
Quotes are missing. But you should use parameterized queries anyway. - sticky bit
I am new sorry. Can you give a small example about parameterized queries. - alp
No. But you can use your favorite search engine, there's plenty on that. - sticky bit
Do not concatenate values like that into a SQL query. Use a PreparedStatement mkyong.com/tutorials/jdbc-tutorials - a_horse_with_no_name
Thanks ı am looking now. - alp

1 Answers

0
votes

@stiky bit you were right i was missing Quotes. The correct one:

public int save(Employee p){
String sql="insert into emp89(name,salary,surname,departmentname,company,address,working,age,saat) values('"+p.getName()+"','"+p.getSalary()+"','"+p.getSurname()+"','"+p.getDepartmentname()+"','"+p.getCompany()+"','"+p.getAddress()+"','"+p.getWorking()+"','"+p.getAge()+"','"+saat+"')";
return template.update(sql);