DBUtil java 鏈接sqlserver 數據庫

package com.util.base;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

public class DBUtils {

    private static final String DBDRIVER = "net.sourceforge.jtds.jdbc.Driver";// 驅動類類名

    private static final String DBNAME = "database";// 數據庫名
    
    private static final String DBURL = "jdbc:jtds:sqlserver://localhost:1433/" + DBNAME;// 連接URL

    private static final String DBUSER = "sa";// 數據庫用戶名

    private static final String DBPASSWORD = "123456";// 數據庫密碼

    private static Connection conn = null;

    private static PreparedStatement ps = null;

    private static ResultSet rs = null;

    // 獲取數據庫連接

    public static Connection getConnection() {

        try {

            Class.forName(DBDRIVER);// 註冊驅動

            conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);// 獲得連接對象

        } catch (ClassNotFoundException e) {// 捕獲驅動類無法找到異常

            e.printStackTrace();

        } catch (SQLException e) {// 捕獲SQL異常

            e.printStackTrace();

        }

        return conn;

    }

    // 查詢數據

    public ResultSet select(String sql) throws Exception {

        try {

            conn = getConnection();

            ps = conn.prepareStatement(sql);

            rs = ps.executeQuery(sql);

            return rs;

        } catch (SQLException sqle) {

            throw new SQLException("select data Exception: " + sqle.getMessage());

        } catch (Exception e) {

            throw new Exception("System error: " + e.getMessage());

        }

    }

    // 插入數據

    public int insert(String sql) throws Exception {

        int num = 0;// 計數

        try {

            conn = getConnection();

            ps = conn.prepareStatement(sql);

            num = ps.executeUpdate();

        } catch (SQLException sqle) {

            throw new SQLException("insert data Exception: " + sqle.getMessage());

        } finally {

            try {

                if (ps != null) {

                    ps.close();

                }

            } catch (Exception e) {

                throw new Exception("ps close exception: " + e.getMessage());

            }

            try {

                if (conn != null) {

                    conn.close();

                }

            } catch (Exception e) {

                throw new Exception("conn close exception: " + e.getMessage());

            }

        }

        return num;

    }

    // 刪除數據

    public int delete(String sql) throws Exception {

        int num = 0;// 計數

        try {

            conn = getConnection();

            ps = conn.prepareStatement(sql);

            num = ps.executeUpdate();

        } catch (SQLException sqle) {

            throw new SQLException("delete data Exception: " + sqle.getMessage());

        } finally {

            try {

                if (ps != null) {

                    ps.close();

                }

            } catch (Exception e) {

                throw new Exception("ps close Exception: " + e.getMessage());

            }

            try {

                if (conn != null) {

                    conn.close();

                }

            } catch (Exception e) {

                throw new Exception("conn close Exception: " + e.getMessage());

            }

        }

        return num;

    }

    // 修改數據

    public int update(String sql) throws Exception {

        int num = 0;// 計數

        try {

            conn = getConnection();

            ps = conn.prepareStatement(sql);

            num = ps.executeUpdate();

        } catch (SQLException sqle) {

            throw new SQLException("update data Exception: " + sqle.getMessage());

        } finally {

            try {

                if (ps != null) {

                    ps.close();

                }

            } catch (Exception e) {

                throw new Exception("ps close Exception: " + e.getMessage());

            }

            try {

                if (conn != null) {

                    conn.close();

                }

            } catch (Exception e) {

                throw new Exception("conn close Excepiton: " + e.getMessage());

            }

        }

        return num;

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章