倒数据库连接类jar包以SQL Server 2005 为例:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionUtil {
private static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String URL = "jdbc:sqlserver://127.0.0.1:1433;databaseName=数据库名";// 127.0.0.1
private static String USERNAME = "登录名";
private static String PASSWORD = "登录密码";
private static Connection CON = null;
public static Connection openConnection() {
try {
Class.forName(DRIVER);
CON = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return CON;
}
public static void closeConnection() {
try {
CON.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}