【发布时间】:2010-07-22 20:46:08
【问题描述】:
每当我执行 SQL UPDATE 和 INSERT 时,我想将登录用户(Web 应用程序)的用户 ID 和 IP 地址写入我的 Oracle DB。比如
public static int updateUser(STKUser user, STKUser loggedIn) throws DAOException {
Connection connection = null;
connection = DB.getConnFromCache();
PreparedStatement ps = null;
String query = "INSERT INTO xtblPersonnel (pID, pPssWrd, pAdminDate, pAdminIP, pAdminBy) VALUES (?,?,SYSDATE,?,?)";
try {
ps = connection.prepareStatement(query);
ps.setString(1, user.getBadge());
ps.setString(2, user.getPassword());
ps.setString(3, loggedIn.getIpAddress());
ps.setString(4, loggedIn.getBadge());
return ps.executeUpdate();
}
catch (Exception e) {
System.out.println("SQL Exception inserting new user with badge: " + user.getBadge() + ". Error Message: " + e.getMessage());
LOGGER.log(Level.INFO, "SQL Exception inserting new user with badge: " + user.getBadge() + ". Error Message: " + e.getMessage(), user);
throw new DAOException("SQL Exception inserting new user!");
// return 0;
}
finally {
DB.closePreparedStatement(ps);
DB.releaseConnToCache(connection);
}
}
STKuser 是一个 Javabean
我的应用程序使用通用的 Oracle 数据库用户名和密码,这就是为什么我要记录谁进行了更新或插入以及来自哪台机器的原因。
这是一种可接受的方法吗?我曾经参加过会议,但意识到这是不行的。
【问题讨论】:
-
很难从给定的信息中分辨出来。为什么是 2 个 STKUser 对象?我猜他们似乎代表同一个用户。你在哪里初始化 PreparedStatement 和 Connection?捕获块在哪里?为什么这个方法声明为静态的?
-
一个 STKUser 对象表示要添加到数据库的员工数据,第二个 STKUser 对象表示登录执行 SQL INSERT 的管理员。我已经编辑了答案以包含更多代码