Trouble changing a specific row using Preparedstatment
been trying to change a specific line using prepared statments
i did :
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());
}
}
it works fine with no sql errors everything works fine except
those
pst.executeUpdate();
pst2.executeUpdate();
something i'm missing ?
thanks to all the helpers !
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?
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
Re: Trouble changing a specific row using Preparedstatment
So how do you know they haven't run?
If nothing has changed?
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...
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.
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
Re: Trouble changing a specific row using Preparedstatment
No problem.
We all get code blindness at times.