I am trying to insert some data into a sql server i have but it seems like I'm formating the information wrong. This is what i have so far.

product id = int;
type = string;
watt = float;
volts = float;

String query = "INSERT INTO GoodCustomer(Product ID, Type, Watt, Volts) VALUES(+obj.getProductId()+,'"+obj.getType()+"','"+obj.getWatt()+"','"+obj.getVolts()+"')");
3 Years
Discussion Span
Last Post by bguild

It looks like obj.getProductId() should not be inside the quotes - it's not part of a string literal, it's an expression you want evaluated.

Edited by JamesCherrill


hai slygoth,

try to change the statement formating from this

String query = "INSERT INTO GoodCustomer(Product ID, Type, Watt, Volts) VALUES(+obj.getProductId()+,'"+obj.getType()+"','"+obj.ge####t()+"','"+obj.getVolts()+"')");

to this

String query = "INSERT INTO GoodCustomer(Product ID, Type, Watt, Volts) VALUES("+obj.getProductId()+",'"+obj.getType()+"','"+obj.ge####t()+"','"+obj.getVolts()+"')");

i think you missed to give " " quotation marks at the start and end of +obj.getProductId()+

let me know the status

happy coding

Edited by radhakrishna.p: giving clarity on the solution


hai stultuske

when we print the sql statement at console before executing

i thought it prints like as follows (what he has given in his first)

output might be:

INSERT INTO GoodCustomer(Product ID, Type, Watt, Volts) VALUES(+obj.getProductId()+,'type_value_here','ge####t_value_here','volts_value_here')

but +obj.getProductId()+ remains the same

thats the reason i left that value as a expression evaluated value as JamesCherrill said

please have a clean look once in changed sql statement in my post

i haven't placed any single quotes which declares obj.getProductId() as a string literal in my post

thats it

my apologies to all of you if i mislead the response

any comments are appreciated

Edited by radhakrishna.p: responding for question regarding solution


no, it wasn't misleading, but you basically just repeated what JamesCherrill said. so, my question:

how exactly is this answer different then the one given yesterday?

was more asking: why did you repeat James' answer? it doesn't really add new info to the mix.


Thanks guys for the response. Both radhak and james gave good answers. Im still getting an error tho let me send the function i have.

    public int  insertlighting(lighting obj)
        int n;
            stat = con.createStatement();
            String query = "INSERT INTO GoodCustomer(Product ID, Type, Watt, Volts) VALUES("+obj.getProductId()+",'"+obj.getType()+"','"+obj.getWatt()+"','"+obj.getVolts()+"')");
            n = stat.executeUpdate(query);
        catch(SQLException ex)
        return n;

im getting " ; expected.
Want to write this information in the database.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.