Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

catching SQLException or just Exception?

 
rick sowfilk
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,

calling SQLException.getMessage() on a caught SQLException returns

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at org.sqlite.PrepStmt.batch(PrepStmt.java:173)
at org.sqlite.PrepStmt.setDouble(PrepStmt.java:189)
at double_entry.account_manager.add_new_account(account_manager.java:155)
at double_entry.main_code.main(main_code.java:49)



however catching the super class Exception
and calling getMessage returns only an int 3



why getMessage() return different result
 
Rob Spoor
Sheriff
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it's not an SQLException that's occurring but a java.lang.ArrayIndexOutOfBoundsException. That last one has a very short message, "3".

You should ask yourself why you're getting an ArrayIndexOutOfBoundsException. Did you forget to set a parameter value?
 
rick sowfilk
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to insert values as a string (warp them in quotation marks), using PreparedStatement



however jdbc PreparedStatement completely ignore the question marks in quotation marks
in the previous code i can only replace 3 values not 5 values
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the quote marks are part of the data (that's really very weird, though), then you add them to the data, not the SQL statement.
 
rick sowfilk
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the quote marks are part of the data (that's really very weird, though)


why its called string literal, and i use it to insert string values in db

ex:

update accounts_data set notes=sdfsd where id=1;
will get you error
Error: no such column: sdfsd

while if use string literal
update accounts_data set notes='sdfsd' where id=1;
then its ok,


although
update accounts_data set notes='sdf()sd' where id=1;
is ok,

however
update accounts_data set notes=sdf()sd where id=1;
Error: near "sd": syntax error
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are not examples of using a prepared statement.

You do not use literals with a PreparedStatement; at least not in any part where you are using a ? placeholder.
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't need to quote the string literal when passing into the prepared statement. Once you set up the query, you should have block of code that sets these literals eg ps.setString("value"); or ps.setInt(123);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic