日期:2014-05-16  浏览次数:20452 次

jdbc 简单封装
package com.fms.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 数据原始位置
* @author weir
*
*/
public class AttendanceRecordCardDbConn {
private Connection Conn = null;
    private Statement Stmt = null;
    private ResultSet Rs = null;
    public AttendanceRecordCardDbConn() {
    try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            Conn = java.sql.DriverManager.getConnection("jdbc:sqlserver://172.168.1.3:1433;DatabaseName=netsys1;SelectMethod=direct", "sa","sa");
        } catch (Exception e) {
            e.printStackTrace();
        }
}
   
    public ResultSet ExecuteQuery(String sql) {
        Rs = null;
        try {
            Stmt = Conn.createStatement();
            Rs = Stmt.executeQuery(sql);
        } catch (SQLException e) {
            System.err.println("执行查询语句出错:" + e.getMessage() + sql);
        }
        return Rs;
    }
   
    public void ExecuteUpdate(String sql) {
        PreparedStatement PreStmt = null;
        try {
            PreStmt = Conn.prepareStatement(sql);
            PreStmt.executeUpdate();
            PreStmt.close();
        } catch (SQLException e) {
            System.err.println("执行更新语句出错:" + e.getMessage() + sql);
        }
    }
    public void Close() {
        try {
            if (Rs != null) {
                Rs.close();
            }
            if (Stmt != null) {
                Stmt.close();
            }
            if (Conn != null) {
                Conn.close();
            }
        } catch (SQLException e) {
            System.err.println("数据库关闭失败,异常为:" + e.getMessage());
        }
    }
}