Parse integer, insert record

G

Grzegorz Stasica

Hi,

During updating my database I keep going receiving the same error caused
by formating integer by Java. The code is as follows:
...
...
stmt.executeQuery(MessageFormat.format("insert into tab (num) values ({0})",
new Object[] {new Integer(txtNum.getText())}));

The problem is that when in field txtNum the number is >999 java formats
the value to for example 1 234(mark space between 1 and 2). This cause
the db to throw errors since it's not a number. Could anybody tell me
how to avoid this.

Regards
 
M

Michael Borgwardt

Grzegorz said:
stmt.executeQuery(MessageFormat.format("insert into tab (num) values
({0})",
new Object[] {new Integer(txtNum.getText())}));

The problem is that when in field txtNum the number is >999 java formats
the value to for example 1 234(mark space between 1 and 2). This cause
the db to throw errors since it's not a number. Could anybody tell me
how to avoid this.

Don't use MessageFormat to build the queries. It's meant to build *human readable*
messages, that's why it formats the number that way. For DB queries, it's totally
unfitting. PreparedStatement does the same thing in a way that's actually tailored
towards DB queries and yields additional benefits.
 
G

Grzegorz Stasica

Yes, I know about PreparedStatements benefits, but since I'm not going
to use the same statement twice the better and lighter solution is with
Statement.
Do you have any idea how to force format nnnn (n-stands for number)

Rgs
 
M

Michael Borgwardt

Grzegorz said:
Yes, I know about PreparedStatements benefits, but since I'm not going
to use the same statement twice the better and lighter solution is with
Statement.

No, it's not. The statements have to be compiled anyway, and the memory overhead
is negligible. There is practically no difference in "weight", and with
PreparedStatement your problem disappears.

Do you have any idea how to force format nnnn (n-stands for number)

java.text.DecimalFormat
 
J

John C. Bollinger

Grzegorz said:
Hi,

During updating my database I keep going receiving the same error caused
by formating integer by Java. The code is as follows:
..
..
stmt.executeQuery(MessageFormat.format("insert into tab (num) values
({0})",
new Object[] {new Integer(txtNum.getText())}));

The problem is that when in field txtNum the number is >999 java formats
the value to for example 1 234(mark space between 1 and 2). This cause
the db to throw errors since it's not a number. Could anybody tell me
how to avoid this.

Use a PreparedStatement to achieve this kind of functionality. Not only
will it reliably format your data according to the requirements of the
DB in use, it may also give you a performance improvement by allowing
the DB engine to precompile the query.


John Bollinger
(e-mail address removed)
 
A

Alan Meyer

John C. Bollinger said:
Grzegorz said:
Hi,

During updating my database I keep going receiving the same error caused
by formating integer by Java. The code is as follows:
..
..
stmt.executeQuery(MessageFormat.format("insert into tab (num) values
({0})",
new Object[] {new Integer(txtNum.getText())}));

The problem is that when in field txtNum the number is >999 java formats
the value to for example 1 234(mark space between 1 and 2). This cause
the db to throw errors since it's not a number. Could anybody tell me
how to avoid this.

Use a PreparedStatement to achieve this kind of functionality. Not only
will it reliably format your data according to the requirements of the
DB in use, it may also give you a performance improvement by allowing
the DB engine to precompile the query.


John Bollinger
(e-mail address removed)

Surprisingly perhaps, I think Grzegorz is right about performance. I have
also
read that Statement is much more efficient than PreparedStatement if the
Statement only executes once.

Being a skeptic, I've tested this on several platforms and, if I remember
correctly, an ordinary Statement may be 10 to as much as 50 times
faster than a PreparedStatement, IF the statement only executes once.
If the statement executes hundreds of times, the PreparedStatement will
be faster.

Of course that's not necessarily a reason not to use PreparedStatement. If
the statement only executes once, efficiency is probably of no significance,
and PreparedStatement is always safer because there are no format issues.

But even if you use Statement, I still don't see why MessageFormat needs
to be used. If you have an Integer object Foo, Foo.toString() may do
the right thing and be much simpler. Or, depending on the format of
txtNum, it might be directly usable.

For example:

"insert into tab (num) values (" + Foo.toString() + ")"
or
"insert into tab (num) values (" + txtNum + ")"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top