Java Reference
In-Depth Information
/*CANTHAPPEN */
throw
throw new
new
SQLException
(
"ERROR: deleted "
+
n
+
" rows!!"
);
}
// IFF we deleted it from the DB, also remove from the in-memory list
users
.
remove
(
u
);
}
public
public synchronized
synchronized
void
void
setPassword
(
String nick
,
String newPass
)
throws
throws
SQLException
{
// Find the user object
User u
=
getUser
(
nick
);
// Change it in DB first; if this fails, the info in
// the in-memory copy won't be changed either.
setPasswordStatement
.
setString
(
1
,
newPass
);
setPasswordStatement
.
setString
(
2
,
nick
);
setPasswordStatement
.
executeUpdate
();
// Change it in-memory
u
.
setPassword
(
newPass
);
}
/** Update the Last Login Date field. */
public
public synchronized
synchronized
void
void
setLoginDate
(
String nick
,
java
.
util
.
Date
date
)
throws
throws
SQLException
{
// Find the user object
User u
=
getUser
(
nick
);
// Change it in DB first; if this fails, the date in
// the in-memory copy won't be changed either.
// Have to convert from java.util.Date to java.sql.Date here.
// Would be more efficient to use java.sql.Date everywhere.
setLastLoginStmt
.
setDate
(
1
,
new
new
java
.
sql
.
Date
(
date
.
getTime
()));
setLastLoginStmt
.
setString
(
2
,
nick
);
setLastLoginStmt
.
executeUpdate
();
// Change it in-memory
u
.
setLastLoginDate
(
date
);
}
}
Another example of prepared statements is given in
Changing Data Using SQL
.