论坛首页 入门技术论坛

jdbc 简单封装

浏览 1474 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-03-11  
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());
        }
    }
}
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics