* 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 com.stu.util;import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
//import java.sql.Connection;
/**
*
* @author Home-land
*/
public class DBUtil {
//常量字符串
private static final String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL ="jdbc:sqlserver://localhost:1433;DataBaseName=db_student";
private static final String USER ="sa";
private static final String PWD ="123456";
//给库添加jar文件
private static Connection connection = null;
static{
try {
//加载驱动
Class.forName(Driver);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
//获得链接
public static Connection getConnection(){
try {
connection =DriverManager.getConnection(URL,USER,PWD);
} catch (SQLException ex) {
ex.printStackTrace();
}
return connection;
}
//关闭连接
public static void close(Connection connection,Statement st,ResultSet rst){
if(rst != null){
try {
rst.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if(st != null){
try {
st.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}