aspose file tools*
The moose likes JDBC and the fly likes prepareStatement Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "prepareStatement Help" Watch "prepareStatement Help" New topic
Author

prepareStatement Help

Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
Hi,

Updating Prepared statement takes two criteria's. Both column names of the JTable. The problem i am having is as follows:


The problem i am having is inserting the columnName within the preparedstatement. I am trying get value from a cell and just insert that updated value into the database using these statement (i have everything but this working). Please advise.

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

You'll need some space between the word "set" and the column name.
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54


doing the above (adding space) still throws a syntax error..i just want to store the update data in 1 cell..any ideas?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You have a column called "CV no"? Normally spaces are not allowed in column names.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
yes i have a column by that name..Ah right ok i will give that a try..thanks
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
The column name in the WHERE clause is most likely the cause for the syntax error: "where CV no = ?".
Even if your DBMS allows column names to contain spaces, these column names must be escaped in the SQL queries.
The escape char/sequence depends on the DBMS that you're using; you'll need to type something like <pre>`CV no`</pre> or <pre>[CV no]</pre> or whatever.
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
ok i have changed the column name to CV wasnt an issue.


Still i get a syntax error. I googled this and somewhere it said you can't pass variable into prepare statement so i am not sure. If thats case is there an alternative?
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
1) What's your DBMS ?
2) What is the value of columnName when you get to the prepareStatement() call.
3) What's the stack-trace ?
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
Ok my fault i have fixed this..it was referring to an incorrect table that i was testing, which has exactly the same info, sincere apologies.

I am using access, so if i wanted to include spaces in my column name how would i do that? I mean which 1 would i use? `Cv no` or [Cv no] ?
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
I'm pretty sure it's the [...] in Microsoft products.

However, I'm almost positive you already found it yourself by now -- even if you had to iterate through all ASCII codes and stop when no exception is thrown.
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
you work wonders mate..its wrks gd..
A slight prblem though, wen i update the 1st value in table its throw a invaliid cursor state error (0)..i can update any cell except cells in 1st row..any ideas?

Ps its backtips in ms prducts for space in column name..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

At a guess, your code is written as if the first row is row zero? The first row is row 1.
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
Paul Clapham wrote:At a guess, your code is written as if the first row is row zero? The first row is row 1.


I havent changed any of the numbering..All i am doign is getting the selected row and column value and then storing that in database. This works fine apart from the 1st one because 1st row is row 0. As getselectedRow returns 0, which is causing this problem i think..anyway to overcome this? I can post the code if you wish!
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
i have fixed it thanks..you guys are great..

resultSet cannot be on position 0..that was the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: prepareStatement Help