📄 topicdao.java
字号:
package db;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import javax.naming.*;
import domain.*;
import actions.*;
import listener.DataSourceListener;
public class TopicDao
{
private String selectBySortId="select topic_id,sort_id,topic_title,topic_content,topic_author,topic_updatetime from bbsTopics where sort_id=?";
private String selectById="select topic_id,sort_id,topic_title,topic_content,topic_author,topic_updatetime from bbsTopics where topic_id=?";
private DataSource ds=null;
public TopicDao()
{
try
{
{
Context ctx=new InitialContext();
ds=(DataSource)ctx.lookup(DataSourceListener.JNDI_NAME);
}
}
catch(Exception e)
{
}
}
public BBSTopic getTopic(String topicId)
{
Connection conn=null;
BBSTopic topic=null;
try
{
conn=ds.getConnection();
PreparedStatement psmt=conn.prepareStatement(selectById);
psmt.setInt(1,Integer.parseInt(topicId));
ResultSet rs=psmt.executeQuery();
rs.next();
topic=new BBSTopic(rs.getInt(1),
rs.getInt(2),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getDate(6));
}
catch(Exception e)
{
}
finally
{
try
{
if(conn!=null)
conn.close();
}
catch(Exception ignore)
{
}
}
return topic;
}
public void addTopic(String id,String title,String content,String userName)
{
Connection conn=null;
BBSTopic topic=null;
try
{
conn=ds.getConnection();
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery("select * from bbsTopics");
rs.moveToInsertRow();
rs.updateInt(2,Integer.parseInt(id));
rs.updateString(3,title);
rs.updateString(4,content);
rs.updateString(5,userName);
rs.insertRow();
}
catch(Exception e)
{
}
finally
{
try
{
if(conn!=null)
conn.close();
}
catch(Exception ignore)
{
}
}
}
public HashMap getTopics(int sortId)
{
Connection conn=null;
HashMap topics=null;
try
{
conn=ds.getConnection();
PreparedStatement psmt=conn.prepareStatement(selectBySortId);
psmt.setInt(1,sortId);
ResultSet rs=psmt.executeQuery();
topics=new HashMap();
while(rs.next())
{
BBSTopic topic=new BBSTopic();
topic.setId(rs.getInt(1));
topic.setSortId(rs.getInt(2));
topic.setTitle(rs.getString(3));
topic.setContent(rs.getString(4));
topic.setAuthor(rs.getString(5));
topic.setTime(rs.getDate(6));
topics.put(topic.getId(),topic);
}
}
catch(Exception e)
{
}
finally
{
try
{
if(conn!=null)
conn.close();
}
catch(Exception ignore)
{
}
}
return topics;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -