📄 files.java
字号:
/**
* Copyright (c) 2002 CODEFAD.com
* All right reserved.
*
* Description: ....
*
* Author: Chu Daping 2002-5-12 15:05
*
*/
package com.codefad.easyoffice;
import java.util.*;
import java.sql.*;
import com.codefad.easyoffice.*;
import com.codefad.easyoffice.db.*;
public class Files {
private static final String LOAD_FILE =
"SELECT * FROM files WHERE fileID=?";
private static final String INSERT_FILE =
"INSERT INTO files(deptID, stationID, description, memo," +
"createdate, createuserID, url, filetype) VALUES(?,?,?,?,?,?,?,?)";
private static final String SAVE_FILE =
"UPDATE files SET stationID, deptID=?, decription=?, memo=?," +
" createdate=?, createuserID=?, url=? WHERE fileID=?";
private static final String DELETE_FILE =
"DELETE FROM files WHERE fileID=?";
public static final int ZILIAO_FILE = 1;
public static final int WENJIAN_FILE = 2;
public static final int SOFTWARE_FILE = 3;
private long fileID = -1;
private java.util.Date createdate = null;
private String description = null;
private String memo = null;
private String url = null;
private long createuserID = -1;
protected long deptID = -1;
protected long stationID = -1;
private int filetype = -1;
private boolean isReadyToSave = false;
public Files() {};
public Files(long stationID, long deptID, long createuserID, String description,
String memo, String url, java.util.Date createdate, int filetype)
{
this.stationID = stationID;
this.deptID = deptID;
this.createuserID = createuserID;
this.description = description;
this.memo = memo;
this.url = url;
this.createdate = createdate;
this.filetype = filetype;
//if (isNew(stationID, deptID, url)) {
insertToDb();
//}
}
public Files(long id)
{
this.fileID = id;
loadFromDb();
isReadyToSave = true;
}
private void loadFromDb()
{
try {
Connection con = new SqlCon().getConnection();
PreparedStatement pstmt = con.prepareStatement(LOAD_FILE);
pstmt.setLong(1, fileID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
deptID = rs.getLong("deptID");
stationID = rs.getLong("stationID");
createuserID = rs.getLong("createuserID");
description = rs.getString("description");
memo = rs.getString("memo");
url = rs.getString("url");
createdate = rs.getDate("createdate");
filetype = rs.getInt("filetype");
}
rs.close();
pstmt.close();
con.close();
}
catch( SQLException sqle ) {
System.out.print(sqle.toString());
}
}
public int getFileType() {
return filetype;
}
public long getID() {
return fileID;
}
public long getStationID() {
return stationID;
}
public void setStationID(long id) {
this.stationID = id;
saveToDb();
}
public long getDeptID() {
return deptID;
}
public void setDeptID(long id) {
this.deptID = id;
saveToDb();
}
public long getCreateUserID() {
return createuserID;
}
public void setCreateUserID(long id) {
this.createuserID = id;
saveToDb();
}
public java.util.Date getCreateDate() {
return createdate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
saveToDb();
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
saveToDb();
}
public String getUrl() {
//String encodedUrl = null;
//try {
//encodedUrl = new String(url.getBytes("UTF-16"));
//} catch (Exception e) {}
return url;
}
public void setUrl(String url) {
this.url = url;
saveToDb();
}
public ArrayList getFileArray(int filetype) {
Connection con = new SqlCon().getConnection();
PreparedStatement stmt = null;
ArrayList fileArray = new ArrayList();
String sql = "select * from files where filetype=? order by fileID DESC";
try {
stmt = con.prepareStatement(sql);
stmt.setInt(1, filetype);
ResultSet rs = stmt.executeQuery();
long fileID = -1;
while (rs.next())
{
fileID = rs.getLong("fileID");
fileArray.add(String.valueOf(fileID));
}
rs.close();
stmt.close();
con.close();
}
catch (SQLException sqle) {
sqle.printStackTrace();
}
return fileArray;
}
private void saveToDb()
{
try {
Connection con = new SqlCon().getConnection();
PreparedStatement pstmt = con.prepareStatement(SAVE_FILE);
pstmt.setLong(8, fileID);
pstmt.setLong(2, deptID);
pstmt.setLong(1, stationID);
pstmt.setLong(6, createuserID);
pstmt.setString(3, description);
pstmt.setString(4, memo);
pstmt.setString(7, url);
pstmt.setString(5, new Timestamp(createdate.getTime()).toString());
pstmt.executeUpdate();
pstmt.close();
con.close();
}
catch( SQLException sqle ) {
System.out.print(sqle.toString());
}
}
public boolean isNew(long stationID, long deptID, String url)
{
try {
Connection con = new SqlCon().getConnection();
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM files WHERE stationID=? and deptID=? and url=?");
pstmt.setLong(1, stationID);
pstmt.setLong(2, deptID);
pstmt.setString(3, url);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return false;
}
rs.close();
pstmt.close();
con.close();
}
catch( SQLException sqle ) {
System.out.print(sqle.toString());
}
return true;
}
private void insertToDb()
{
try {
Connection con = new SqlCon().getConnection();
PreparedStatement pstmt = con.prepareStatement(INSERT_FILE);
pstmt.setLong(2, deptID);
pstmt.setLong(1, stationID);
pstmt.setLong(6, createuserID);
pstmt.setString(3, description);
pstmt.setString(4, memo);
pstmt.setString(7, url);
pstmt.setString(5, new Timestamp(createdate.getTime()).toString());
pstmt.setInt(8, filetype);
pstmt.executeUpdate();
pstmt.close();
con.close();
}
catch( SQLException sqle ) {
System.out.print(sqle.toString());
System.out.print("无法插入数据库!");
}
}
public void deleteFile() {
deleteFromDb();
}
private synchronized void deleteFromDb() {
try {
Connection con = new SqlCon().getConnection();
PreparedStatement pstmt = con.prepareStatement(DELETE_FILE);
pstmt.setLong(1, fileID);
pstmt.executeUpdate();
pstmt.close();
con.close();
} catch (Exception e) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -