连接数据库工具类调用程序

来源:互联网 发布:网络口碑双刃剑图片 编辑:程序博客网 时间:2024/06/01 10:39
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Test.util;


import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


/**
 *
 * @author Administrator
 */
public class JDBCUtil {
    //driverclass=com.mysql.jdbc.Driver
    public static final String DRIVER="com.microsoftsqlserverjdbc.SQlServerDriver";
    //jdbcurl=JDBC:mysql://127.0.0.1:3306/selectivesys?characterEncoding=UTF-8
    public static final String URL="jdbc:sqlserver://localhost1433;databasename=selectivedb";
    public static final String USER="root";
    public static final String PWD="123456";
    //构造方法调用驱动
    public  JDBCUtil(){
    try {
            Class.forName(DRIVER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //创建链接
    public static Connection getConnection() throws SQLException{
        new JDBCUtil();
        Connection conn=null;
        conn=DriverManager.getConnection(URL, USER,PWD);
        return conn;    
    }
    //关闭连接
    public static void closeConn(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
    }
        //关闭执行对象
    public static void closeStatement(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
    }


    //关闭结果集
    public static void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
}
0 0
原创粉丝点击