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

📄 database.java

📁 一个用于监控WEB服务器和数据库服务器的客户端程序。
💻 JAVA
字号:
/*
 * 
 */

package com.jobcn.database;

import java.sql.*;

/**
 * remark: 用于连接数据源,并提供基本的数据读取与操作方法
 * @author: Stephen 
 * date: 2005-3-21
 * @version: v1.0 
 * 
 * upd_remark: 
 * upd_user: 
 * upd_date:
 */

public class DataBase {
    private String databaseName = "";
    private String user = "";
    private String password = "";
    private static String odbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
    
    static{
		try {
			Class.forName(odbcDriver);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
    //构造函数1
    public DataBase() {
        this("BOSSSITE", "up_bos$_top", "uptopbos$");
    }

    //构造函数2
    public DataBase(String databaseName, String user, String password) {
        this.databaseName = databaseName; 	//数据库
        this.user = user; 					//用户名
        this.password = password; 			//密码
    }

    //数据库连接:用jdbc直接连接或是由连接池进行管理
    public Connection connect(){
    	Connection conn = null;
		String odbcUrl = "jdbc:odbc:"+databaseName;
    	try{
			conn = DriverManager.getConnection(odbcUrl,user,password);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return conn;
		//return DBUtil.getConnection("jobcn_boss");
	}

    // 创建语句
    public Statement createStatement(Connection conn) {
        try {
            return conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //取得记录集
    public ResultSet getRS(Statement stmt, String SQL) throws SQLException {
        return stmt.executeQuery(SQL);
    }

    //执行SQL 语句
    public void execute(Statement stmt, String SQL) throws SQLException {
        stmt.execute(SQL);
    }

    //取得记录集的行数。
    public int getRowCount(ResultSet RS) {
        int rowCount = 0;
        try {
            String rowStatus = "";
            int preRow = 0;
            if(RS.isBeforeFirst()) {
                rowStatus = "isBeforeFirst";
            } else {
                if(RS.isAfterLast()) {
                    rowStatus = "isAfterLast";
                } else {
                    rowStatus = "normal";
                    preRow = RS.getRow();
                }
            }

            RS.last();
            rowCount = RS.getRow();
            if(rowStatus.equals("isBeforeFirst") || preRow == 0) {
                RS.beforeFirst();
            } else {
                if(rowStatus.equals("isAfterLast")) {
                    RS.afterLast();
                } else {
                    RS.absolute(preRow);
                }
            }
        } catch(SQLException e) {
            rowCount = 0;
        }
        return rowCount;
    }

    //取得记录集的列数。
    public int getColCount(ResultSet RS) {
        int colCount = 0;
        try {
            ResultSetMetaData RSMD = RS.getMetaData();
            colCount = RSMD.getColumnCount();
        } catch(SQLException e) {
            colCount = -1;
        }
        return colCount;
    }

    //分页
    public void absolutePage(ResultSet RS, int pageSize, int page) {
        try {
            RS.absolute(pageSize * (page - 1) + 1);
        } catch(Exception e) {
        }
    }

    //从存储过程中得到返回值。
    public int getReturnCode(CallableStatement callablestatement, boolean flag) {
        int i = -1;
        ResultSet resultset = null;
        if(flag) {
            try {
                resultset = callablestatement.getResultSet();
                resultset.next();
                i = resultset.getInt(1);
            } catch(SQLException _ex) {
                i = -1;
            } finally {
                try {
                    resultset.close();
                } catch(SQLException _ex) {
                }
            }
        }
        return i;
    }

    public static void main(String[] args) {
        DataBase db = new DataBase();
        Connection conn = null;
        Statement stmt = null;
        ResultSet RS = null;
        String SQL = "select '2005-01-01' as curDate";
        try {
            conn = db.connect();
            stmt = conn.createStatement();
            RS = stmt.executeQuery(SQL);
            if(RS.next()) {
                System.out.println(RS.getString("curDate"));
            }
            RS.close();
            RS=null;
            stmt.close();
            stmt=null;
            conn.close();
            conn=null;
        } catch(Exception e) {
            e.printStackTrace();
        } finally{
            try{
                if(RS!=null){
	                RS.close();
	            }
	            if(stmt!=null){
	                stmt.close();
	            }
	            if(conn!=null){
	                conn.close();
	            }
            } catch(SQLException sqle) {
                sqle.printStackTrace();             
            }
        }
    }

}

⌨️ 快捷键说明

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