📄 accessforumdao.java
字号:
package cn.ialvin.bbs.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import cn.ialvin.bbs.bean.Forum;
import cn.ialvin.sql.DBConnection;
public class AccessForumDAO implements IForumDAO {
private DBConnection coxn;
AccessForumDAO(DBConnection coxn) { this.coxn = coxn; }
public boolean deleteForum(int id) {
boolean res = false;
String sql = "DELETE FROM [forum] WHERE [id]=?";
PreparedStatement stmt = null;
try {
stmt = this.coxn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.execute();
res = true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.coxn.closeStatement(stmt);
}
return res;
}
public Forum getForum(int id) {
Forum forum = null;
String sql = "SELECT * FROM [forum] WHERE [id]=" + id;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = this.coxn.prepareStatement(sql);
rs = stmt.executeQuery();
if (rs.next()) {
forum = new Forum();
forum.setId(rs.getInt("id"));
forum.setName(rs.getString("name"));
forum.setDescription(rs.getString("descripted"));
forum.setTopic(rs.getInt("topic"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.coxn.closeResultSet(rs);
this.coxn.closeStatement(stmt);
}
return forum;
}
public ArrayList<Forum> getForums() {
ArrayList<Forum> list = new ArrayList<Forum>();
String sql = "SELECT * FROM [forum]";
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = this.coxn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Forum forum = new Forum();
forum.setId(rs.getInt("id"));
forum.setName(rs.getString("name"));
forum.setDescription(rs.getString("descripted"));
forum.setTopic(rs.getInt("topic"));
list.add(forum);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.coxn.closeResultSet(rs);
this.coxn.closeStatement(stmt);
}
return list;
}
public int insertForum(Forum forum) {
int res = -1;
String sql = "INSERT INTO [forum] ([name], [descripted], [topic]) " +
"VALUES(?, ?, 0)";
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = this.coxn.prepareStatement(sql);
stmt.setString(1, forum.getName());
stmt.setString(2, forum.getDescription());
stmt.executeUpdate();
this.coxn.closeStatement(stmt);
sql = "SELECT @@IDENTITY";
stmt = this.coxn.prepareStatement(sql);
rs = stmt.executeQuery();
if (rs.next()) {
res = rs.getInt(1);
forum.setId(res);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.coxn.closeStatement(stmt);
}
return res;
}
public boolean updateForum(Forum forum) {
boolean res = false;
String sql = "UPDATE [forum] SET [name]=?, [descripted]=?, [topic]=? WHERE [id]=?";
PreparedStatement stmt = null;
try {
stmt = this.coxn.prepareStatement(sql);
stmt.setString(1, forum.getName());
stmt.setString(2, forum.getDescription());
stmt.setInt(3, forum.getTopic());
stmt.setInt(4, forum.getId());
stmt.executeUpdate();
res = true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.coxn.closeStatement(stmt);
}
return res;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -