?? topiclistdao.java
字號:
package emptyprj;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.ArrayList;
import java.util.HashMap;
import javawebstudio.struts_db.ConnectionPool;
import javawebstudio.struts_db.NetForm;
public class topicListDAO {
private Connection con;
private ConnectionPool pool;
private int rowCount;
private int pageCount;
private int length;
private String pagestr;
public int getLength() { return (this.length); }
public void setLength(int length) { this.length=length; }
public String getPagestr(int ipage)
{
String strPage="";
if(getLength()>0)
{
strPage+="共";
strPage+=String.valueOf(rowCount);
strPage+="條記錄,共";
strPage+=String.valueOf(pageCount);
strPage+="頁,當前是第";
strPage+=String.valueOf(ipage);
strPage+="頁, ";
int istart,iend;
istart=ipage-5;
if(istart<0) {istart=0;}
iend=istart+10;
if(iend>pageCount) {iend=pageCount;}
istart=iend-10;
if(istart<0) {istart=0;}
for(int i=istart;i<iend;i++)
{
strPage+="<a href='toTopicListAction.do?action=find&search=search&page=";
strPage+=String.valueOf(i+1);
strPage+="'>";
strPage+=String.valueOf(i+1);
strPage+="</a>";
strPage+=" ";
}
}
this.pagestr=strPage;
return strPage;
}
public topicListDAO()
{
pool = ConnectionPool.getInstance();
try
{
con = pool.getConnection();
}
catch (SQLException e)
{
e.printStackTrace();
throw new RuntimeException("Unable to get connection.");
}
}
public void Close()
{
try
{
if (con != null) con.close();
}
catch (SQLException e)
{
throw new RuntimeException(e.getMessage());
}
}
public void create(NetForm m_topicList) throws SQLException {
//需要特別注意,如果表中包含有“自動生成序號”的字段,下面“?”變量中需要手工去掉“自動生成序號”字段對應的“?”和變量,并重新排列ps插入變量的序號!!
PreparedStatement ps = null;
//<createSQL>
String sql = "INSERT INTO bbs VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
//</createSQL>
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
//用SQL Server請加入下行:
con.setAutoCommit(true); //如果是MySQL,請注解掉這一句
//SQL Server
ps = con.prepareStatement(sql);
//<create>
ps.setInt(1,m_topicList.getInt("ID"));
ps.setInt(2,m_topicList.getInt("topicID"));
ps.setInt(3,m_topicList.getInt("parentID"));
ps.setString(4,m_topicList.getString("username"));
ps.setString(5,m_topicList.getString("topic"));
ps.setString(6,m_topicList.getString("body"));
ps.setString(7,m_topicList.getString("datetime"));
ps.setInt(8,m_topicList.getInt("reverts"));
ps.setInt(9,m_topicList.getInt("hits"));
ps.setInt(10,m_topicList.getInt("child"));
ps.setString(11,m_topicList.getString("IP"));
ps.setString(12,m_topicList.getString("image"));
//</create>
if (ps.executeUpdate() != 1) {
throw new SQLException ("error.create.topicList");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
try {
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
public void update(NetForm m_topicList,String keyID) {
//需要特別注意,如果表中包含有“自動生成序號”的字段,下面“?”變量中需要手工去掉“自動生成序號”字段對應的“?”和變量,并重新排列ps插入變量的序號!!
PreparedStatement ps = null;
//<updateSQL>
String sql = "UPDATE bbs SET ID = ?,topicID = ?,parentID = ?,username = ?,topic = ?,body = ?,datetime = ?,reverts = ?,hits = ?,child = ?,IP = ?,image = ? WHERE ID = ?";
//</updateSQL>
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
//用SQL Server請加入下行:
con.setAutoCommit(true); //如果是MySQL,請注解掉這一句
//SQL Server
ps = con.prepareStatement(sql);
//<update>
ps.setInt(1,m_topicList.getInt("ID"));
ps.setInt(2,m_topicList.getInt("topicID"));
ps.setInt(3,m_topicList.getInt("parentID"));
ps.setString(4,m_topicList.getString("username"));
ps.setString(5,m_topicList.getString("topic"));
ps.setString(6,m_topicList.getString("body"));
ps.setString(7,m_topicList.getString("datetime"));
ps.setInt(8,m_topicList.getInt("reverts"));
ps.setInt(9,m_topicList.getInt("hits"));
ps.setInt(10,m_topicList.getInt("child"));
ps.setString(11,m_topicList.getString("IP"));
ps.setString(12,m_topicList.getString("image"));
//</update>
///////////////
//<updatekeyID>
ps.setInt(13,java.lang.Integer.parseInt(keyID,10));
//</updatekeyID>
if (ps.executeUpdate() != 1) {
throw new SQLException (
"error.removed.topicList");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
try {
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
public void remove(String sql) {
PreparedStatement ps = null;
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
//用SQL Server請加入下行:
con.setAutoCommit(true); //如果是MySQL,請注解掉這一句
//SQL Server
ps = con.prepareStatement(sql);
if (ps.executeUpdate() != 1) {
throw new SQLException (
"error.removed.topicList");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
try {
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
public void removeID(String keyID) {
//<removeIDSQL>
String sql="DELETE FROM bbs WHERE ";
sql+="ID";
sql+=" = ?";
//</removeIDSQL>
PreparedStatement ps = null;
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
//用SQL Server請加入下行:
con.setAutoCommit(true); //如果是MySQL,請注解掉這一句
//SQL Server
ps = con.prepareStatement(sql);
//<setremovekeyIDdata>
ps.setInt(1,java.lang.Integer.parseInt(keyID,10));
//</setremovekeyIDdata>
if (ps.executeUpdate() != 1) {
throw new SQLException (
"error.removed.topicList");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
try {
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
public NetForm findByPrimaryKey(String keyID)
throws SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
NetForm m_topicList= new NetForm();
//<findByPrimaryKeySQL>
String sql = "SELECT * from bbs WHERE ID = ?";
//</findByPrimaryKeySQL>
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
ps = con.prepareStatement(sql);
//<setfindkeyIDdata>
ps.setInt(1,java.lang.Integer.parseInt(keyID,10));
//</setfindkeyIDdata>
rs = ps.executeQuery();
if (rs.next()) {
//<findByPrimaryKey>
m_topicList.setInt("ID",rs.getInt(1));
m_topicList.setInt("topicID",rs.getInt(2));
m_topicList.setInt("parentID",rs.getInt(3));
m_topicList.setString("username",rs.getString(4));
m_topicList.setString("topic",rs.getString(5));
m_topicList.setString("body",rs.getString(6));
m_topicList.setString("datetime",rs.getString(7));
m_topicList.setInt("reverts",rs.getInt(8));
m_topicList.setInt("hits",rs.getInt(9));
m_topicList.setInt("child",rs.getInt(10));
m_topicList.setString("IP",rs.getString(11));
m_topicList.setString("image",rs.getString(12));
//</findByPrimaryKey>
return m_topicList;
} else {
throw new SQLException (
"error.removed.topicList");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
} finally {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
public Collection findSQL(String sql,int ipage) {
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList list = new ArrayList();
try {
if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}
ps = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = ps.executeQuery();
rs.absolute(-1);
rowCount=rs.getRow();
int offset=1;
int pagesize=getLength();
if(getLength()<1)
{
pagesize=rowCount;
pageCount=1;
}
else
{
pageCount=rowCount/getLength()+((rowCount%getLength())>0?1:0);
offset=(ipage-1)*getLength()+1;
if(offset<1)offset=1;
if(offset>rowCount)offset=rowCount;
}
rs.absolute(offset);
for(int i=0;i<pagesize&&offset<rowCount+1;i++,offset++) {
NetForm m_topicList= new NetForm();
//<find>
m_topicList.setInt("ID",rs.getInt(1));
m_topicList.setInt("topicID",rs.getInt(2));
m_topicList.setInt("parentID",rs.getInt(3));
m_topicList.setString("username",rs.getString(4));
m_topicList.setString("topic",rs.getString(5));
m_topicList.setString("body",rs.getString(6));
m_topicList.setString("datetime",rs.getString(7));
m_topicList.setInt("reverts",rs.getInt(8));
m_topicList.setInt("hits",rs.getInt(9));
m_topicList.setInt("child",rs.getInt(10));
m_topicList.setString("IP",rs.getString(11));
m_topicList.setString("image",rs.getString(12));
//</find>
rs.next();
list.add(m_topicList);
}
return list;
} catch (SQLException e) {
//e.printStackTrace();
//throw new RuntimeException("error.unexpected");
return list;
} finally {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -