您的位置:首页 > 数据库

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

2015-09-28 12:17 483 查看
/*

 * 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();

            }

        }

    }

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: