Thursday, 15 May 2014

Inserting numeric data type using SQL statement in Java -



Inserting numeric data type using SQL statement in Java -

i writing method allows users store info within database.

the problem have storing numeric datatype in database, user's id.

firstly, how can tell if id number auto-incrementing? these it's properties:

type: numeric column size: 4 decimal digits: 0 part of primary key: true part of index: true position: 1

i'm sure read somewhere that, setting part of index (true) allows auto-incrementation. can confirm?

more importantly, when inserting info table receive error stating:

columns of type 'numeric' cannot hold values of type 'char'.

this snippet of insert code (i can reveal more if need be):

statement pstatement = conn.createstatement(); string selectsql = ("insert app.person values ('" + '3' + "','" + user + "','" + pass + "','" + fname + "','" + sname + "','" + squestion + "','" + sanswer + "') "); pstatement.executeupdate(selectsql);

as can see setting id (the first value), 3 manually. believe throwing error , know how insert numeric value, using insert command.

i coding in java, using netbeans 7.3 beta 2, on mac os x mount lion. using derby database.

hey bro suggest u utilize preparestatement

string template = "insert app.person values (your,table,collumn,name) values (?,?,?,?)"; preparedstatement stmt = yourconnection.preparestatement(template);

so if want set id integer, allow java with

stmt.setint(1, id); stmt.setstring(2, user); stmt.setstring(3, fname); stmt.executeupdate();

i dont know how table construction illustration if want utilize int utilize stmt.setint(1, 3);

1--> position u set in string template

3--> maybe u want hard coded id

here illustration pattern utilize preparestatement

string name = "shadrach" double cost = "100.00" int qty = 3; string template = "insert product (name,price,qty) values (?,?,?)"; preparedstatement stmt = connection.preparestatement(template); stmt.setstring(1, name); stmt.setdouble(2, price); stmt.setint(3, qty); stmt.executeupdate();

java sql numeric javadb

No comments:

Post a Comment