📄 essayop.java
字号:
package essay;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import org.springframework.jdbc.core.JdbcTemplate;
import com.mysql.jdbc.Blob;
import common.DS;
public class EssayOp
{
static String BASEPATH = "C:\\Program Files\\Tomcat\\webapps\\China\\images";
public static void tInsert(Text t) throws Exception
{
JdbcTemplate jt = new JdbcTemplate(DS.getDs());
String content = t.getContent().replaceAll("'", "&sq&");
jt.execute("insert into essay(id, titleid, flag, content) values("
+ t.getId()+ ", " + t.getTitleId() + ", 't', '" + content + "')");
}
public static void paInsert(Binary b) throws Exception
{
String insert = "insert into essay(titleid," +
" flag, name, content) values(?, ?, ?, ?)";
PreparedStatement ps = DS.getDs().getConnection().prepareStatement(insert);
ps.setInt(1, b.getTitleId());
ps.setString(2, b.getFlag());
ps.setString(3, b.getName());
ps.setBinaryStream(4, b.getIs(), b.getIs().available());
ps.execute();
}
public static Vector selectAll(int titleId, String un)
throws IOException, SQLException
{
String search = "select flag, name, content from essay " +
"where titleid = ? order by id";
ResultSet rs = null;
PreparedStatement PS = null;
Vector v = null;
Text ts = null;
Blob ctBlob;
byte[] ctBytes;
String ctStr, flag, name;
Binary bs = null;
File dir = null, file;
InputStream is;
FileOutputStream fos;
int size = 0;
byte[] buffer = new byte[4096];
PS = DS.getDs().getConnection().prepareStatement(search);
PS.setInt(1, titleId);
rs = PS.executeQuery();
dir = new File(BASEPATH);
if(rs.isBeforeFirst())
{
if (!dir.exists())
dir.mkdir();
dir = new File(BASEPATH + "\\" + un);
if (!dir.exists())
dir.mkdir();
v = new Vector();
}
while(rs.next())
{
flag = rs.getString(1);
ctBlob = (Blob)rs.getBlob(3);
if("p".equals(flag) || "a".equals(flag))
{
name = rs.getString(2);
file = new File(dir.getPath(), name);
fos = new FileOutputStream(file);
is = rs.getBinaryStream(3);
while ((size = is.read(buffer)) != -1)
fos.write(buffer, 0, size);
fos.close();
bs = new Binary(name, flag);
v.add(bs);
}
else
{
ctBytes = ctBlob.getBytes(1, (int)ctBlob.length());
ctStr = new String(ctBytes);
ctStr = ctStr.replaceAll(" ", " ").replaceAll("&sq&", "'");
ts = new Text(ctStr, "t");
v.add(ts);
}
}
rs.close();
return v;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -