Results 1 to 8 of 8
- 10-09-2012, 02:04 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
Trouble changing a specific row using Preparedstatment
been trying to change a specific line using prepared statments
i did :
it works fine with no sql errors everything works fine exceptJava Code:public void loan(int cid , int bid) { PreparedStatement pst ; PreparedStatement pst2 ; int i; Date d=new Date(); String date= d.toString(); try{ Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","",""); //con = DriverManager.getConnection("jdbc:sqlserver://localhost;" + "user=sa;password=HerongYang"); Statement stmt= con.createStatement(); ResultSet rs=stmt.executeQuery("SELECT MAX(loan_id) FROM lib.books_out "); rs.next(); if(rs.getInt(1)>0) i=rs.getInt(1)+3; else i=1; stmt.executeUpdate("INSERT INTO lib.books_out ( loan_id , book_id , client_id , date1 ) VALUES ("+i+","+bid+","+cid+",'"+date+"')"); ResultSet rs2=stmt.executeQuery("SELECT ammount FROM lib.books WHERE id = "+bid+""); rs2.next(); pst=con.prepareStatement("UPDATE lib.books SET ammount = ? WHERE id = ?"); pst.setInt(1, rs2.getInt(1)); pst.setInt(2, bid); pst2=con.prepareStatement("UPDATE lib.clients SET allowed = ? WHERE id = ?"); pst2.setInt(1, 0); pst2.setInt(2, cid); pst.executeUpdate(); pst2.executeUpdate(); jLabel3.setText("** Book loan was successful"); } catch(Exception e){ System.out.println("Bad Connection"+e.getStackTrace()); } }
those
pst.executeUpdate();
pst2.executeUpdate();
something i'm missing ?
thanks to all the helpers !
- 10-09-2012, 03:04 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Trouble changing a specific row using Preparedstatment
How do you know the INSERT is working?
How do you know the SELECT is working?
I see no logging in there to show that.
If the 'SELECT ammount...' is working, then how can you tell that pst hasn't worked? You are, after all, setting the value to what it already is, so there's nothing there to show it's changed.
Ditto with the lib.clients change. Is it actually a change?Please do not ask for code as refusal often offends.
- 10-09-2012, 03:21 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
Re: Trouble changing a specific row using Preparedstatment
yea i look manualy in mysql to see changes , the INSERT makes a new row but the specific values that i'm tring to alter with the prepared statments won't change
- 10-09-2012, 03:34 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Trouble changing a specific row using Preparedstatment
So how do you know they haven't run?
If nothing has changed?Please do not ask for code as refusal often offends.
- 10-09-2012, 03:53 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
Re: Trouble changing a specific row using Preparedstatment
they ran ,
there was no exception error but they didn't change anything in the table like they should...
- 10-09-2012, 04:47 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Trouble changing a specific row using Preparedstatment
Did you commit?
But in any case:
SELECT ammount FROM lib.books WHERE id = <bid>
UPDATE lib.books SET ammount = <amount from above> WHERE id = <bid>;
The first one is your select.
The second your update.
The first selects the amount from the table.
The second sets that amount to the value it's just selected.
How can you tell whether it has "worked"?
No value changes.
Can't tell if the second update has the same problem.Please do not ask for code as refusal often offends.
- 10-09-2012, 05:16 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 39
- Rep Power
- 0
Re: Trouble changing a specific row using Preparedstatment
hehe yea i found it
it should have been :
pst.setInt(1, rs2.getInt(1)+1);
that's what it was missing
the second update worked just fine
tnx for all your help man
- 10-09-2012, 06:02 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
How to allow specific letters only?
By ctmarco3 in forum New To JavaReplies: 7Last Post: 04-01-2011, 04:41 AM -
How to print PreparedStatment object in console
By sasi.tati in forum AWT / SwingReplies: 1Last Post: 08-21-2010, 06:16 AM -
Changing binding when changing the underlying model object
By ChrisNY in forum NetBeansReplies: 0Last Post: 08-14-2010, 10:09 AM -
Changing text fonts and colours for specific files.
By breako in forum EclipseReplies: 0Last Post: 01-27-2010, 03:52 PM -
how do i print a specific txt file on a specific printer
By nikhilbhat in forum New To JavaReplies: 2Last Post: 11-08-2008, 10:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks