📄 dbnews.java
字号:
/**
* Copyright (c) 2002 CODEFAD.com
* All right reserved.
*
* Description: ....
*
* Author: Chu Daping 2002-5-12 15:05
*
*/
package com.codefad.easyoffice.db;
import java.sql.*;
import java.util.*;
import java.io.*;
import com.codefad.easyoffice.*;
public final class DbNews implements News {
private static final String LOAD_BOARD =
"SELECT * FROM news WHERE noticeID=?";
private static final String INSERT_BOARD =
"INSERT INTO news(deptID, stationID, userID, subject, content ," +
"createdate) VALUES(?,?,?,?,?,?)";
private static final String SAVE_BOARD =
"UPDATE news SET userID=?, subject=?, body=?, createdate=? WHERE noticeID=?";
private long boardID = -1;
private java.util.Date createdate = null;
private String subject = null;
private String body = null;
private long userID = -1;
protected long deptID = -1;
protected long stationID = -1;
private boolean isReadyToSave = false;
public DbNews(long stationID, long deptID, long userID, String subject, String body)
{
this.stationID = stationID;
this.deptID = deptID;
this.userID= userID;
this.subject = subject;
this.body = body;
this.createdate = new java.util.Date();
insertToDb();
}
public DbNews(long id)
{
this.boardID = id;
loadFromDb();
isReadyToSave = true;
}
private void loadFromDb()
{
try {
Connection con = new SqlCon().getConnection();
PreparedStatement pstmt = con.prepareStatement(LOAD_BOARD);
pstmt.setLong(1, boardID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
deptID = rs.getLong("deptID");
stationID = rs.getLong("stationID");
userID = rs.getLong("userID");
subject = rs.getString("subject");
body = rs.getString("content");
createdate = rs.getDate("createdate");
}
rs.close();
pstmt.close();
con.close();
}
catch( SQLException sqle ) {
System.out.print(sqle.toString());
}
}
public long getID() {
return boardID;
}
public java.util.Date getCreateDate() {
return createdate;
}
public void setCreateDate(java.util.Date createdate)
{
this.createdate = createdate;
saveToDb();
}
public String getSubject()
{
return subject;
}
public void setSubject(String subject)
{
this.subject = subject;
saveToDb();
}
public String getBody() {
return body;
}
public long getDeptID() {
return deptID;
}
public long getUserID() {
return userID;
}
public long getStationID() {
return stationID;
}
public Department getDept() {
Department dept = new DbDepartment(deptID);
return dept;
}
public void setBody(String body)
{
this.body = body;
saveToDb();
}
public User getUser(long userID) {
User user = new DbUser(userID);
return user;
}
public void insertToDb()
{
try {
Connection con = new SqlCon().getConnection();
PreparedStatement pstmt = con.prepareStatement(INSERT_BOARD);
pstmt.setLong(1, deptID);
pstmt.setLong(2, stationID);
pstmt.setLong(3, userID);
pstmt.setString(4, subject);
pstmt.setString(5, body);
pstmt.setString(6, new Timestamp(createdate.getTime()).toString());
pstmt.executeUpdate();
pstmt.close();
con.close();
} catch (Exception e) {
}
}
private synchronized void saveToDb(Connection con) throws SQLException
{
}
private synchronized void saveToDb() {
boolean abortTransaction = false;
Connection con = new SqlCon().getConnection();
try {
saveToDb(con);
}
catch( Exception e ) {
e.printStackTrace();
abortTransaction = true;
}
finally {
try { con.close(); }
catch (Exception e) { }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -