StringUtils(字符串日期互转,字符串转Int)
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 字符串操作工具类
*
* @author chensw
*
*/
public final class StringUtil {
/**
* 字符串截取方法
*
* @param data
* @param len
* @return
*/
public static String subString(String data, int len) {
if (data == null) {
return null;
}
if (data.length() >= len) {
return data.substring(0, len - 1) + "...";
}
return data;
}
/**
* 字符串日期转换为Date类型日期
* @param str 需要转换的字符串对象
* @return 转换成功之后获得的Date类型日期
*/
public static Date strToDate(String str) {
Date date=null;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//日期格式
try {
date=sdf.parse(str);
} catch (Exception e) {
System.out.println("字符串日期转Date型日期转换失败,请注意格式重新输入");
}
return date;
}
/**
* Date类型日期转换成字符串日期
* @param date 需要转换的Date对象
* @return 转换成功之后获得的字符串日期
*/
public static String dateToString(Date date) {
String str=null;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//日期格式
try {
str=sdf.format(date);
} catch (Exception e) {
System.out.println("Date型日期转字符串日期转换失败,请注意格式重新输入");
}
return str;
}
public static int stringToInt(String data) {
if (data == null || data.trim().length() == 0) {
return 0;
}
try {
return Integer.valueOf(data.trim());
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}// end
DBUtils(连接创建、事务开启、提交、回滚)
import java.sql.*;
/**
* <pre>
* java数据库编程技术叫jdbc,jdbc编程需要用到4个基本参数和执行6个步骤
*
* 本例设计一个数据库工具类,用于保存四个参数和通用的步骤
*
*<pre>
*/
public final class DBUtil {//final类不会被继承,是一个终结类
//驱动:驱动类是由数据库厂商所提供的 ,不同数据库有不同的数据库驱动
private static final String driver="oracle.jdbc.driver.OracleDriver";
//路径:jdbc协议地址,
private static final String url="jdbc:oracle:thin:@localhost:1521:orcl";
//用户名
private static final String user="My12306";
//密码
private static final String passWord="tiger";
/**
* 打开数据库操作
* @return
*/
public static Connection openConnection(){
Connection conn=null;
//注册驱动
try {
Class.forName(driver);
//打开连接
try {
conn = DriverManager.getConnection(url,user,passWord);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("打开数据库失败,连接异常");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("打开数据库连接失败,找不到驱动类!");
}
return conn;
}
/**
* 关闭数据库连接
*/
public static void closeAll(Connection conn,Statement stmt,ResultSet rs){
//注意:关闭顺序从小到大,单独捕获异常,进行非空判断
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
}
}//关闭结果集
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
}
}//关闭状态连接
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
}
}//关闭数据库连接
}//关闭连接
/**
* 打开事务
* @param conn
*/
public static void openTransaction(Connection conn){
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
}
}
/**
* 提交事务
* @param conn
*/
public static void commitTransaction(Connection conn){
try {
conn.commit();
conn.setAutoCommit(true);
} catch (SQLException e) {
}
}
/**
* 回滚事务
* @param conn
*/
public static void rollbackTransaction(Connection conn){
try {
conn.rollback();
conn.setAutoCommit(true);
} catch (SQLException e) {
}
}
MD5Utils(MD5加密)
import java.security.MessageDigest;
public class Md5Utils {
public final static String md5(String s) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] btInput = s.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
常用工具类-StringUtils,DBUtils,MD5Utils
最新推荐文章于 2025-06-26 01:22:58 发布