A database operation problems

Category: Web Develop
 
lnfi_lf
2009-08-14 02:48:49

Sponsored Links
I wrote a Bean method to update the user's login times, the user login is successful call it, the idea is to start with the user table read logcount, +1 after writing back the following code:
- ------------------------------
public void writeLogCount (String username)
{
int newcount = 0;
int oldcount = 0;
try
{
Statement stmt = con.createStatement ();
ResultSet rs = stmt.executeQuery ("select * from users where username = '" + username + "'");
while (rs.next ())
{
oldcount = rs.getInt ("userlogcount");
String sql = "update users set logcount = '" + newcount + "' where username = '" + username + "'";
stmt.executeUpdate (sql);
}
}
catch (Exception e) {};
}
--------------------------------
debugging for a long time found
String sql = "update users set logcount = '" + newcount + "' where username = '" + username + "'";
stmt.executeUpdate (sql);
This two fundamental lack of implementation, fault lies in what it does? (Data in the database is no problem, my other login date update method can be used)

Sponsored Links

lixing19900815
2009-08-14 02:57:12
if your SQL string that was wrong, oh.
String sql = "update users set logcount = '" + newcount + "' where username = '" + username + "'";

In the front plus a space where to try,
String sql = "update users set logcount = '" + newcount + "' where username = '" + username + "'";

try, wondered not?
kuaileshenghuo4
2009-08-14 03:12:46
1. in the catch statement must be printed in the wrong, and do not do nothing.
2. stmt you can perform executeQuery () after the switch off and then re-open it before performing executeUpdate () try.
3. use "upadte users set logcount = logcount +1" Would not an easier way?
g952141042
2009-08-14 03:31:59
The user has no rights
yhjktyuj
2009-08-14 03:48:28
Single-stepping a look ah, yes rs Lane did not get to the data, or executing an exception
tian417126109
2009-08-14 03:54:52
preferably in the catch (Exception e) {e.printStackTrace ();} to know where the wrong
shijietingyu
2009-08-14 04:04:56
have a try

Statement stmt = con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
migoal
2009-08-14 04:21:07
rs should not get to the data in it
L04520
2009-08-14 04:36:50
should be a very simple question, it seems that as long as the update is performed before the ResultSet close look on it.
qqqqqqiu
2009-08-14 04:53:15
Halo, not ah, now I do not give permission to the user points. . .
jianhe2009
2009-08-14 04:58:30
I tried it, rs did not get to the data. If you change this:
public ResultSet writeLogCount (String username)
{
try
{
Statement stmt = con.createStatement ();
String sql = "select * from users where username = '" + username + "'";
ResultSet rs = stmt.executeQuery (sql);
return rs;
}
catch (Exception e) {}
return null;
}
---------- -----------------------------------
then in JSP
ResultSet rs = userlogin.writeLogCount (name);
try
{
while (rs.next ())
{
out.println ("your login number is:" + rs.getInt ("logcount"));
}
}
catch (Exception e) {}
can be read out, but this is only read out it, is it still possible to set up in JSP variables to assign values it, write a method +1 after write back? I hope to complete this work in the Bean. The question now is, if the while (rs.next ()) and rs.getInt ("userlogcount") written in the Bean, there is no way performed, why?
qaz1397
2009-08-14 05:10:59
TomyGuan (Tomy): I tried or not, did not get to the data. That does not seem to have the same space, and I in another way where you can. The question now is, if the while (rs.next ()) and rs.getInt ("userlogcount") written in the Bean, there is no way to perform, and write in the JSP where you can, which in the end why?
zhiwenhuang
2009-08-14 05:23:12
this problem so difficult to do? No way, continue UP
xiaozhhg
2009-08-14 05:40:45
wanchao2001 (): But it did not even catch the exception, ah, I'm in the JSP where such tests:
int a = userlogin.writeLogCount (name );
out.println ("a =" + a);
results show a = 0, so it seems oldcount = rs.getInt ("userlogcount") This is a did not even get executed (table value is 300); I changed oldcount = 100, then the correct display a = 100, visible while (rs.next ()) was performed. I look at a building prawn trouble code, thank you! ! !
longer841202
2009-08-14 05:46:59
influential
a select * from tablewhere id = 10
and a select * from table where id = 10

you debug it. Run to see if there sql, sql value is how much?
jxrykkxxjxrykkxx
2009-08-14 06:05:39
TomyGuan (Tomy): I added a space, or not, because even oldcount did not even take out.

while (rs.next ())
{
oldcount = rs.getInt ("userlogcount") ;/ / not implemented from the beginning of the sentence, changed oldcount = 100 you can perform
String sql = "update users set logcount = '" + newcount + "' where username = '" + username + "'" ;
stmt.executeUpdate (sql);
}
----------------------------- ---
qybao (Bao): tried, the question remains, what does it mean that the two parameters, with and without what difference does it?

kekeiloveyou
2009-08-14 06:16:18
rs.getInt ("userlogcount")
to rs.getString ("userlogcount"); sub-over try.

huh.

weird thing. I do not know how to solve it.
xiaoqzhang
2009-08-14 06:27:58
108041217 (gold): Yes, rs certainly did not get to the data, but this is why, I changed the ResultSet function return values, and then in the JSP, you can take rs the right to take, like the following.
public ResultSet writeLogCount (String username)
{
try
{
Statement stmt = con.createStatement ();
String sql = "select * from users where username = '" + username + "'";
ResultSet rs = stmt.executeQuery (sql);
return rs ;
}
catch (Exception e) {}
return null;
}
--------------- ------------------------------
then in JSP
ResultSet rs = userlogin.writeLogCount ( name);
try
{
while (rs.next ())
{
out.println ("Your login is the number:" + rs.getInt ("logcount"));
}
}
catch (Exception e) {}
-------------------------------- ------------------
but this did not reach my goal, ah, I also need to logcount +1, write back, write the JSP is inconvenient ah !

zcd711122
2009-08-14 06:47:21
Weighs, get, could not believe the mentally commit such mistakes, do not say, too embarrassed, some people may have seen, hey cold himself a. . . Thank you for your suggestion or above, very helpful to me, thank you! ! Results posted ing ....
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.