⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbinfostation.java

📁 一套大型Office网络办公系统
💻 JAVA
字号:
/**
* Copyright (c) 2002 CODEFAD.com
* All right reserved.
*
* Description: ....
*
* Author: Chu Daping 2002-5-10 19:35
*
*/

package com.codefad.easyoffice.db;

import java.util.*;
import java.util.Date;
import java.sql.*;
import java.io.*;
import com.codefad.easyoffice.*;

public class DbInfoStation implements InfoStation {
	
	/** DATABASE QUERIES **/
	private static final String ALL_DEPTS =
        "SELECT deptID from dept WHERE stationID=?";
        
    private static final String ADD_DEPT =
        "UPDATE dept set stationID=? WHERE deptID=?";
        
    protected static final String DELETE_DEPTS =
        "DELETE FROM dept WHERE deptID=?";
        
	private static final String LOAD_STATION = 
		"select * from station where stationID=?";
		
	private static final String ADD_STATION =
		"insert into station (name,description,createDate) values (?,?,?)"; 
		
	protected static final String DELETE_CALLBOARD =
		"delete from station where stationID=?";
	
	private long id = -1;
	private String name;
	private String description;
	private java.util.Date createdate;
	

    
    //从id得到一个信息站
    public DbInfoStation(long id)
    {
        this.id = id;
        loadFromDb();
    }
    
    //来自接口InfoStation
    public long getID() {
        return id;
    }

    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
        saveToDb();
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
        saveToDb();
    }
    
    public java.util.Date getCreateDate() {
        return createdate;
    }
    
    public void setCreateDate(java.util.Date createdate) {
    	this.createdate = createdate;
    	saveToDb();
    }
    
    public void addDept(Department dept) {
        Connection con = new SqlCon().getConnection();
        PreparedStatement pstmt = null;
        try {
            try {
                pstmt = con.prepareStatement(ADD_DEPT);
                pstmt.setLong(1,id);
                pstmt.setLong(2,dept.getDeptID());
                pstmt.executeUpdate();
            }
            finally {
                try {  pstmt.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }
            //dept.insertIntoDb(this);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
        	try {con.close();} 
        	catch (SQLException e) {}
        }
    }
    
    public Department getDept(long deptID)
    {
    	Department dept = new DbDepartment(deptID);
    	return dept;
    }
    
    public void deleteDept(Department dept) {
        //
    }
    
    public ArrayList depts() {
        return null;
    }
    
    public Callboard getCallboard(long boardID) {
    	Callboard callboard = new DbCallboard(boardID);
    	return callboard;
    }
    
    public ArrayList callboards() {
        Connection con = new SqlCon().getConnection();
        PreparedStatement stmt = null;
        ArrayList callboards = new ArrayList();
        String sql = "select * from notice where stationID=? order by noticeID DESC";
        try {
        	stmt = con.prepareStatement(sql);
        	stmt.setLong(1,id);
        	ResultSet rs = stmt.executeQuery();
        	long boardID = -1;
        	while (rs.next())
        	{
        		boardID = rs.getLong("noticeID");
        		callboards.add(getCallboard(boardID));
        	}
	        rs.close();
        	stmt.close();
        	con.close();
        }
        catch (SQLException sqle) {
        	sqle.printStackTrace();
        }
        return callboards;
    }
    
    public News getNews(long boardID) {
    	News news = new DbNews(boardID);
    	return news;
    }
    
    public ArrayList news() {
        Connection con = new SqlCon().getConnection();
        PreparedStatement stmt = null;
        ArrayList news = new ArrayList();
        String sql = "select * from news where stationID=? order by noticeID DESC";
        try {
        	stmt = con.prepareStatement(sql);
        	stmt.setLong(1,id);
        	ResultSet rs = stmt.executeQuery();
        	long boardID = -1;
        	while (rs.next())
        	{
        		boardID = rs.getLong("noticeID");
        		news.add(getNews(boardID));
        	}
	        rs.close();
        	stmt.close();
        	con.close();
        }
        catch (SQLException sqle) {
        	sqle.printStackTrace();
        }
        return news;
    }
    
    //public Resource getResource(long sourceID) {
    //	Resource resource = new DbResource(sourceID);
    //	return resource;
    //}
    
    //public ArrayList resources() {
    //    Connection con = new SqlCon().getConnection();
    //    PreparedStatement stmt = null;
    //    ArrayList resources = new ArrayList();
    //    String sql = "select * from ziyuan where stationID=? order by resourceID ASC";
   //     try {
    //    	stmt = con.prepareStatement(sql);
    //    	stmt.setLong(1,id);
    //    	ResultSet rs = stmt.executeQuery();
    //    	long sourceID = -1;
    //    	while (rs.next())
    //    	{
    //    		sourceID = rs.getLong("resourceID");
    //    		resources.add(getResource(sourceID));
    //    	}
	//        rs.close();
    //    	stmt.close();
    //    	con.close();
    //    }
    //    catch (SQLException sqle) {
    //    	sqle.printStackTrace();
    //    }
    //    return resources;
    //}
   
    public static User getUser(long userID) {
        User user = new DbUser(userID);
        return user;
    }
    
    public ArrayList users() {
    	Connection con = new SqlCon().getConnection();
        PreparedStatement stmt = null;
        String sql = null;
        ArrayList users = new ArrayList();
        sql = "select * from userinf order by userID ASC";
        try {
        	stmt = con.prepareStatement(sql);
        	ResultSet rs = stmt.executeQuery();
        	long userID = -1; 
        	while (rs.next())
        	{
        		userID = rs.getLong(1);
        		users.add(getUser(userID));
        	}
        	rs.close();
        	stmt.close();
        	con.close();
        }
        catch (SQLException sqle) {
        	sqle.printStackTrace();
        }
        return users;
    }
    
    private void loadFromDb() {
        Connection con = new SqlCon().getConnection();
        PreparedStatement pstmt = null;
        try {
            pstmt = con.prepareStatement(LOAD_STATION);
            pstmt.setLong(1,id);
            ResultSet rs = pstmt.executeQuery();
            
            this.id = rs.getLong(1);
            this.name = rs.getString(2);
            this.description = rs.getString(3);
            this.createdate =
                new java.util.Date(Long.parseLong(rs.getString(4).trim()));
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }
    
    private void insertIntoDb() {
    	
        Connection con = new SqlCon().getConnection();
        PreparedStatement pstmt = null;
        try {
            pstmt = con.prepareStatement(ADD_STATION);
            pstmt.setString(1,name);
            pstmt.setString(2,description);
            pstmt.setString(3, new Timestamp(createdate.getTime()).toString());
            pstmt.executeUpdate();
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }
    
    private synchronized void saveToDb() {
    }
}

⌨️ 快捷键说明

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