BBS_笔记
日期格式化:
<%=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getPdate()) %>
id传值必须的判断:
String strid = request.getParameter("id");
if(strid==null||strid.trim().equals("")){
out.print("id error!");
return;
}
int id = 0;
try{
id = Integer.parseInt(strid);
}catch(NumberFormatException e){
out.print("id error again!");
return;
}
关于中文乱码需要转码两种方式:
request.setCharacterEncoding("utf8");
String title = new String(request.getParameter("title").getBytes("8859_1"),"utf-8");
String cont = new String(request.getParameter("cont").getBytes("8859_1"),"utf-8");
对于自动生成key获取办法:
PreparedStatement st = DB.createPstmt(conn,sql,Statement.RETURN_GENERATED_KEYS); ResultSet rs = st.getGeneratedKeys(); rs.next(); rootId = rs.getInt(1);
事物处理:
conn.setAutoCommit(false); 。。。。 conn.commit(); conn.setAutoCommit(true);
读秒返回到首页:
<script type="text/javascript">
function toUrl(url){
window.location.href=url;
}
</script>
<script type="text/javascript">
function delayTime(){
var time = document.getElementById("time").innerHTML;
if(time>0){
time--;
document.getElementById("time").innerHTML=time;
setTimeout("delayTime()",1000);
}else{
var url ="article.jsp";
toUrl(url);
}
}
</script>
分页处理:
final int PAGE_SIZE = 4; //每页显示数量
int totalPages = 0;//总页数
int totalRecords = 0;//总记录数
int pageNo =1; //页码(当前第pageNo页)
String pno = request.getParameter("pageNo");
if(pno!=null&&!pno.trim().equals("")){
try{
pageNo = Integer.parseInt(pno);
}catch(NumberFormatException e){
pageNo = 1;
}
}
if(pageNo<=0)
pageNo=1;
List<Article> articles = new ArrayList<Article>();
Connection conn = DB.getConn();
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeSql(stmt, "select count(*) from article where pid=0");
if(rs.next()){
totalRecords = rs.getInt(1);
}
totalPages = (totalRecords+PAGE_SIZE-1)/PAGE_SIZE;//常用算法,算出总页数
if(pageNo>totalPages)
pageNo = totalPages;
int startPos = (pageNo-1)*PAGE_SIZE;
Statement st = DB.createStmt(conn);
ResultSet r = DB.executeSql(st, "select * from article where pid=0 limit "+startPos+","+PAGE_SIZE);
while(r.next()){
Article a = new Article();
a.artFormrs(r);
articles.add(a);
}
DB.close(rs);
DB.close(stmt);
DB.close(conn);

浙公网安备 33010602011771号