日期:2014-05-20 浏览次数:20698 次
import java.sql.*; /** * 获得连接 */ public class DBHelper { public DBHelper() { } public static Connection getConn() { Connection conn = null; try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); conn = DriverManager.getConnection( "jdbc:sqlserver://localhost:1433;databaseName=pubs", "sa", ""); } catch (SQLException ex) { System.out.println(ex.getMessage()); } catch (ClassNotFoundException ex) { System.out.println(ex.getMessage()); } return conn; } /** * 关闭连接 * @param conn Connection */ public static void clostConn(Connection conn) { if (conn != null) { try { conn.close(); conn = null; } catch (SQLException ex) { ex.printStackTrace(); } } } }