0

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);    
        }  
5
  • 3
    Quotes are missing. But you should use parameterized queries anyway. Commented Mar 18, 2021 at 15:02
  • I am new sorry. Can you give a small example about parameterized queries. Commented Mar 18, 2021 at 15:13
  • 1
    No. But you can use your favorite search engine, there's plenty on that. Commented Mar 18, 2021 at 15:14
  • 1
    Do not concatenate values like that into a SQL query. Use a PreparedStatement mkyong.com/tutorials/jdbc-tutorials Commented Mar 18, 2021 at 15:19
  • Thanks ı am looking now. Commented Mar 18, 2021 at 16:03

1 Answer 1

0

@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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.