jar连接数据库出现断开的解决方案
作者:51itcn 日期:2010-05-10
1. 采用JDBC连接数据库,如:DB2
/**
* 建立联接
*
* @return
*/
public Connection getConnection() {
try {
conn = null;
Class.forName(driverClassName).newInstance();
log.debug("创建Mon连接……");
conn = DriverManager.getConnection(app_url, app_user, app_password);
log.debug("Mon连接创建成功!");
} catch (Exception e) {
log.debug("Mon连接创建失败!");
log.error(e);
}
return conn;
}
2. 实际应用
java读取资源文件信息
作者:51itcn 日期:2010-05-05
private static final String CONFIG = "conf";
private static ResourceBundle CONFIG_S = ResourceBundle.getBundle(CONFIG);
private Conf() {
}
public static String getString(String key) {
try {
return CONFIG_S.getString(key);
} catch (MissingResourceException e) {
e.printStackTrace();
}
return "";
}
java提前N天的日期
作者:51itcn 日期:2010-04-04
/**
* 提前N天的日期
*
* @param date
* @param days
* @return
*/
public static Date beforeDate(Date date, int days) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
c.add(java.util.Calendar.DAY_OF_YEAR, -days);
return c.getTime();
}
比如取当天的前一天日期时间:类文件.beforeDate(new Date(),1)
java需要格式化的日期对象
作者:51itcn 日期:2010-04-04
写一个方法,比如公共文件里:
public static String formatDateToStr(Date date, String formatter) {
if (date == null)
return "";
try {
return new java.text.SimpleDateFormat(formatter).format(date);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
取当天的日期:
public static String today() {
return formatDateToStr(new Date(),“YYYY-MM-DD”);
}
Java计算指定年月的日期数
作者:51itcn 日期:2010-03-17
*
* @param year
* @param month
* @return
*/
public static int maxDayOfMonth(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
boolean isRunYear = (year % 400 == 0)
|| (year % 4 == 0 && year % 100 != 0);
return isRunYear ? 29 : 28;
default:
return 31;
}
}
