cmtop.java

来自「一个JAVA做的博客系统。系统采用JSP + JS + TOMCAT + MYS」· Java 代码 · 共 55 行

JAVA
55
字号
package cmt;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;

import org.springframework.jdbc.core.JdbcTemplate;

import com.mysql.jdbc.Blob;
import common.DS;

public class CmtOp
{
	public static void Insert(Comment c) throws Exception
	{
		JdbcTemplate jt = new JdbcTemplate(DS.getDs());
		String comment = c.getComment().replaceAll("'", "&sq&");
		jt.execute("insert into comment(titleid, username, comment, date) values("
			+ c.getTitleId() + ", '" + c.getUsername() + "', '" + comment
			+ "', '" + new Date().toLocaleString() + "')");
	}
	public static Vector selectAll(int titleId)
	    throws IOException, SQLException
	{
		String search = "select username, comment, date " +
			"from comment where titleid = ? order by id";
		ResultSet rs = null;
		PreparedStatement PS = null;
		Vector v = null;
		Comment cmts = null;
		Blob cmtBlob;
		byte[] cmtBytes;
		String cmtStr;
		PS = DS.getDs().getConnection().prepareStatement(search);
		PS.setInt(1, titleId);
		rs = PS.executeQuery();
		if(rs.isBeforeFirst())
			v = new Vector();
		while(rs.next())
		{
			cmtBlob = (Blob)rs.getBlob(2);
			cmtBytes = cmtBlob.getBytes(1, (int)cmtBlob.length());
			cmtStr = new String(cmtBytes);
			cmtStr = cmtStr.replaceAll(" ", " ").replaceAll("&sq&", "'");
			cmts = new Comment(rs.getString(1), cmtStr, 
				rs.getTimestamp(3).toLocaleString());
			v.add(cmts);
		}
		rs.close();
		return v;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?