package org.hdht.util;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 简单工具类对象
* 简易工具就用这个
*
*/
public class EasyT {
private final String dbDriver = "oracle.jdbc.driver.OracleDriver";
public EasyT() {
}
public static void out(String str) {
System.out.println(str + "");
}
public static void out(Object o) {
System.out.println(o.toString() + "");
}
public int execBat(String batPath) {
try {
Process process = Runtime.getRuntime().exec(batPath);
StreamDrainer t_a = new StreamDrainer(process.getInputStream());
new Thread(t_a).start();
StreamDrainer t_b = new StreamDrainer(process.getErrorStream());
new Thread(t_b).start();
process.getOutputStream().close();
int exitValue = process.waitFor(); //
int a = process.exitValue();
process.destroy();
out(t_a.inptMess + "\n" + t_b.inptMess);
return a;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
public int execCmd(String cmd) {
try {
String[] cmds = new String[] { "cmd.exe", "/C", cmd };
Process process = Runtime.getRuntime().exec(cmds);
StreamDrainer t_a = new StreamDrainer(process.getInputStream());
new Thread(t_a).start();
StreamDrainer t_b = new StreamDrainer(process.getErrorStream());
new Thread(t_b).start();
process.getOutputStream().close();
int exitValue = process.waitFor();
int a = process.exitValue();
process.destroy();
out(t_a.inptMess + "\n" + t_b.inptMess);
return a;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
class StreamDrainer implements Runnable {
private InputStream ins;
private String inptMess = "";
public StreamDrainer(InputStream ins) {
this.ins = ins;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
// System.out.println(line);
inptMess += (line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public List<String> fileReader(String filePath) {
BufferedReader br = null;
String lineContent = "";
try {
FileReader fr = new FileReader(filePath);
br = new BufferedReader(fr);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
List<String> lines = new ArrayList<String>();
lineContent = br.readLine();
for (int i = 0; lineContent != null; i++) {
lines.add(lineContent);
lineContent = br.readLine();
}
return lines;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Connection getOracleConn(String ip, String userName, String password) {
try {
String url = "";
if ("".equals(ip)) {
url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
} else {
url = "jdbc:oracle:thin:@" + ip + ":1521:orcl";
}
Class.forName(dbDriver).newInstance();
Connection con = DriverManager.getConnection(url, userName,
password);
con.setAutoCommit(true);
return con;
} catch (Exception ex) {
System.out.println("couldn't find driver");
return null;
}
}
public Connection getLocalConn(String userName,String password) {
try {
String url= "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
Class.forName(dbDriver).newInstance();
Connection con = DriverManager.getConnection(url,userName,password);
con.setAutoCommit(true);
return con;
} catch (Exception ex) {
System.out.println("couldn't find driver");
return null;
}
}
public Connection getOracleConnByURL(String url,String userName,String password) {
try {
if("".equals(url)){
url= "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
}
Class.forName(dbDriver).newInstance();
Connection con = DriverManager.getConnection(url,userName,password);
con.setAutoCommit(true);
return con;
} catch (Exception ex) {
System.out.println("couldn't find driver");
return null;
}
}
public ResultSet QueryDB(String sql,Connection conn){
Statement st;
try {
st = conn.createStatement();
ResultSet res = st.executeQuery(sql);
return res;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public int UpdateDB(String sql,Connection conn){
Statement st;
try {
st = conn.createStatement();
int res = st.executeUpdate(sql);
return res;
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public static void main(String[] args) {
//EasyT.out(new java.util.Date());
//EasyT.out(EasyT.getNow("-"));
//EasyT.out(EasyT.format(new Date(),""));
//int a = obj.execCmd("md d:\\mdbycmd");
//int b = obj.execBat("/back.bat");
//out(obj.fileReader("d:\\sq.js").size());
//out(obj.getOracleConn("localhost", "db_qb", "pw123456"));
//out(obj.getLocalConn("db_qb","pw123456"));
/*try {
ResultSet res= obj.QueryDB("select * from tb_agent", obj.getLocalConn("db_qb", "pw123456"));
if(res.next()){
out(res.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}*/
/*int res = obj.UpdateDB("delete from tb_agent t where id=1222", obj.getLocalConn("db_qb", "pw123456"));
out(res);*/
//out(obj.getOracleConnByURL("jdbc:oracle:thin:@127.0.0.1:152j1:orcl", "db_qb", "pw123456"));
EasyT.encodeConvert("我爱你", "UTF-8");
}
public String getInputMessage() throws IOException{
byte buffer[]=new byte[1024];
int count=System.in.read(buffer);
byte[] buf = new byte[count-2];
for(int i=0;i<count-2;i++){
buf[i]=buffer[i];
}
String str=new String(buf,"gb2312");
return str;
}
/**
* 获取当前年月日
* @return
*/
public static String getYNR(){
Calendar cal =Calendar.getInstance() ;
String s="";
String y =cal.get(Calendar.YEAR)+"";
String m =(cal.get(Calendar.MONTH)+1)+"";
String r =cal.get(Calendar.DATE)+"";
s=y+"-"+m+'-'+r;
return s;
}
/**
* 判断一个字符串是不是空或者""
* @param str
* @return
*/
public static boolean isNull(Object obj){
if(obj==null){
return true;
}
if(obj.toString().trim().length()==0){
return true;
}
return false;
}
/**
* 判断一个字符串是不是空或者""
* @param str
* @return
*/
public static boolean isNotNull(Object obj){
if(obj==null){
return false;
}
if(obj.toString().trim().length()==0){
return false;
}
return true;
}
/**
* 去除空格方法
* @param str
* @return null返回""
*/
public static String toString(Object obj){
if(EasyT.isNull(obj)){
return "";
}else{
return obj.toString().trim();
}
}
/**
* 判断一个集合是否是空的list
* @param list 集合对象
* @return true|false
*/
public static boolean isNullList(List list){
if(list==null || list.size()==0 || list.get(0)==null){
return true;
}
return false;
}
/**
* 判断一个集合是否是空的list
* @param list 集合对象
* @return true|false
*/
public static boolean isNotNullList(List list){
return !EasyT.isNullList(list);
}
/**
* 判断一个集合是否是空的数组
* @param array 数组对象
* @return true|false
*/
public static boolean isNullArray(Object[] array){
if(array==null || array.length==0){
return true;
}
return false;
}
/**
* 将字符串转换成精简日期类型(yyyy-MM-dd)
* @param dateStr 时间字符串
* @return 制定的日期或者当前日期。
*/
public static Date convertToDate(String dateStr){
if(EasyT.isNull(dateStr)){
return new Date();
}
dateStr = dateStr.replace(":", ":").replace(":", ":");
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
try {
return formater.parse(dateStr);
} catch (ParseException e) {
return new Date();
}
}
/**
* 将字符串转换成全日期类型(yyyy-MM-dd HH:mm:ss)
* @param dateStr 时间字符串
* @return 制定的日期或者当前日期。
*/
public static Date convertToFullDate(String dateStr){
if(EasyT.isNull(dateStr)){
return new Date();
}
dateStr = dateStr.replace(":", ":").replace(":", ":");
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return formater.parse(dateStr);
} catch (ParseException e) {
return new Date();
}
}
/**
* 将字符串转换成全日期类型(yyyy-MM-dd HH:mm)
* @param dateStr 时间字符串
* @return 制定的日期或者当前日期。
*/
public static Date convertToLongDate(String dateStr){
if(EasyT.isNull(dateStr)){
return new Date();
}
dateStr = dateStr.replace(":", ":").replace(":", ":");
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
return formater.parse(dateStr);
} catch (ParseException e) {
return new Date();
}
}
/**
* 比较两个字符串数组相同的比率
* @param arr1 字符串数组
* @param arr2 字符串数组
* @return 两个数组相同的比率或者-1(表示不符合比较的条件,有null值)
*/
public static double comparArr(String[] arr1,String[] arr2){
if(EasyT.isNullArray(arr1) || EasyT.isNullArray(arr2)){
return -1;//空数组不具备比较条件
}
if(arr1.length!=arr2.length){
return -1;//两个数组长度不相等,不具备比较条件
}
double success=0;//
double total = arr1.length;
for(int i=0;i<arr1.length;i++){
String src=arr1[i];
for(int j=0;j<arr2.length;j++){
String s = EasyT.toString(arr2[j]);
if(EasyT.toString(src).trim().equals(s)){
success++;
break;
}
}
}
double d =success/total;
return d;
}
/**
* 获取当前年月日
* @param str 分割标识符
* @return 用分割标识符分开年月日的日期字符创
*/
public static String getNYR(String str){
if(EasyT.isNull(str)){
str="-";//TODO 设置默认时间分隔符
}
Calendar cal =Calendar.getInstance() ;
String s="";
String y =cal.get(Calendar.YEAR)+"";
String m =(cal.get(Calendar.MONTH)+1)+"";
String r =cal.get(Calendar.DATE)+"";
s=y+str+m+str+r;
return s;
}
/**
* 获取当前月日
* @param str 分割标识符
* @return 用分割标识符分开月日的日期字符创
*/
public static String getYR(String str){
if(EasyT.isNull(str)){
str="-";//TODO 设置默认时间分隔符
}
Calendar cal =Calendar.getInstance() ;
String s="";
int month =cal.get(Calendar.MONTH)+1;
String m ="";
// if(month<10){
// m="0"+month;//TODO 月份小于10,用0补位
// }else{
// m=""+month;
// }
m=""+month;
String r =cal.get(Calendar.DATE)+"";
s=m+str+r;
return s;
}
/**
* 转码方法(用于url传值)(就是两次encode转码)
* @param content 要转吗的方法
* @param encode 转码类型
* @return
*/
public static String encodeConvert(String content,String encode){
try {
//GBK
//EasyT.Log("原始数据="+content);
content =java.net.URLEncoder.encode(content,encode);
//EasyT.Log("第一次encode进行GBK转码后="+content+"");
//String king =new String(content.getBytes("UTF-8"), "UTF-8");
content =java.net.URLEncoder.encode(content,"GBK");
//EasyT.Log("第二次encode进行GBK转码后="+content+"");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
EasyT.LogError("encode进行GBK转码失败");
}
return content;
}
/**
* 记录系统消息日志(在tomcat的root.log中查看)
* @param msg 输入的日志内容
*/
public static void Log(String msg){
Log log = LogFactory.getLog(EasyT.class);
//log.debug("debug;"+msg);
//log.error("deror:"+msg);
log.info(msg);
}
/**
* 记录错误消息日志(在tomcat的root.log中查看)
* @param msg 输入的日志内容
*/
public static void LogError(String msg){
Log log = LogFactory.getLog(EasyT.class);
//log.debug("debug;"+msg);
//log.error("deror:"+msg);
log.error(msg);
}
}