📄 topicserviceimpl.java
字号:
package com.ql.bbs.service.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.ql.bbs.model.Topic;
import com.ql.bbs.service.TopicService;
import com.ql.bbs.util.JdbcUtils;
public class TopicServiceImpl implements TopicService {
public boolean addTopic(Topic topic) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "insert into t_topic(title, context,userId,pid,date,categoryId) "+
"values(?,?,?,?,?)";
try {
con = JdbcUtils.getInstance().getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1,topic.getTitle());
pstmt.setString(2, topic.getContext());
pstmt.setInt(3, topic.getUserId());
pstmt.setInt(4, topic.getPid());
//因为setDate必须set java.sql包中的 Date,java.sql.Date是java.util.Date的子类
pstmt.setDate(5, new java.sql.Date(new java.util.Date().getTime()));
pstmt.setInt(6, topic.getCategoryId());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
JdbcUtils.getInstance().close(con, pstmt, rs);
return false;
} finally {
JdbcUtils.getInstance().close(con, pstmt, rs);
}
return true;
}
public boolean delTopic(int id) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "delete from t_topic where id = ?";
try {
con = JdbcUtils.getInstance().getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
JdbcUtils.getInstance().close(con, pstmt, rs);
return false;
} finally {
JdbcUtils.getInstance().close(con, pstmt, rs);
}
return true;
}
public Topic findTopicById(int id) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Topic topic = null;
String sql = "select title, context,userId,pid,date,categoryId " +
"from t_topic where id=?";
try {
con = JdbcUtils.getInstance().getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
topic = new Topic();
topic.setId(id);
topic.setTitle(rs.getString("title"));
topic.setContext(rs.getString("context"));
topic.setUserId(rs.getInt("userId"));
topic.setPid(rs.getInt("pid"));
topic.setDate(rs.getDate("date"));
topic.setCategoryId(rs.getInt("categoryId"));
}
} catch (Exception e) {
e.printStackTrace();
JdbcUtils.getInstance().close(con, pstmt, rs);
return null;
} finally {
JdbcUtils.getInstance().close(con, pstmt, rs);
}
return topic;
}
public List searchTopics() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List topicList = new ArrayList();
String sql = "select id,title, context,userId,pid,date,categoryId " +
"from t_topic";
try {
con = JdbcUtils.getInstance().getConnection();
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Topic topic = new Topic();
topic.setId(rs.getInt("id"));
topic.setTitle(rs.getString("title"));
topic.setContext(rs.getString("context"));
topic.setUserId(rs.getInt("userId"));
topic.setPid(rs.getInt("pid"));
topic.setDate(rs.getDate("date"));
topic.setCategoryId(rs.getInt("categoryId"));
topicList.add(topic);
}
} catch (Exception e) {
e.printStackTrace();
JdbcUtils.getInstance().close(con, pstmt, rs);
return null;
} finally {
JdbcUtils.getInstance().close(con, pstmt, rs);
}
return topicList;
}
public boolean updateTopic(Topic topic) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "update t_topic set title=?,context=?,date=?,categoryId=?";
try {
con = JdbcUtils.getInstance().getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1,topic.getTitle());
pstmt.setString(2, topic.getContext());
pstmt.setInt(3, topic.getUserId());
pstmt.setInt(4, topic.getPid());
pstmt.setDate(5, new java.sql.Date(topic.getDate().getTime()));
pstmt.setInt(6, topic.getCategoryId());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
JdbcUtils.getInstance().close(con, pstmt, rs);
return false;
} finally {
JdbcUtils.getInstance().close(con, pstmt, rs);
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -