分类: 数据库开发技术
2007-07-20 18:10:30
package com.Embed.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Common {
// 判断用户输入的时间格式是否正确
public static boolean checkDateTime(String inputDate) {
String DATE_TIME_FORMAT = "yyyy-MM-dd";
java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat(
DATE_TIME_FORMAT);
simpleDateFormat.setLenient(false);
Date d = new Date();
boolean check = false;
try{
d = simpleDateFormat.parse(inputDate);
check = true;
}catch(Exception ex){
check = false;
ex.printStackTrace();
}
return check;
}
// 判断用户输入的是否是数字
public static boolean checkNum(String inputValue) {
return false;
}
public static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0)
k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
public static Connection getConnection() {
Connection conn = null;
String Driver = "org.gjt.mm.mysql.Driver";
// String Url = "jdbc:mysql://888888888:888/8888?useUnicode=true&characterEncoding=GBK";
// String user = "8888";
// String pwd = "8888";
String Url = "jdbc:mysql://localhost:3306/8888?useUnicode=true&characterEncoding=GBK";
String user = "root";
String pwd = "000";
try {
Class.forName(Driver);
conn = DriverManager.getConnection(Url, user, pwd);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
// 返回系统时间..其他类可以直接调用此方法。
public static String getDataTime() {
String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat(
DATE_TIME_FORMAT);
String time = simpleDateFormat.format(new java.util.Date(System
.currentTimeMillis()));
return time;
}
/**
* 此方法判断输入字符是否为数字0-9 是返回true不是返回false
*
* @param c
* char
* @return boolean
*/
public static boolean isDigit(char c) {
return (('0' <= c) && (c <= '9'));
}
/**
* 此方法判断输入字符是否为数字a-z或A-Z 是返回true不是返回false
*
* @param c
* char
* @return boolean
*/
public static boolean isAlpha(char c) {
return ((('a' <= c) && (c <= 'z')) || (('A' <= c) && (c <= 'Z')));
}
/**
* 此方法用于检查密码是否合法,用户名密码只能使用英文字母、数字以及-和_,并且首字符必须为字母或数字 密码首字符必须为字母或数字
*
* @param passwordStr
* String
* @return boolean
*/
public static boolean checkUserNameAndPwssword(String passwordStr) {
for (int nIndex = 0; nIndex < passwordStr.length(); nIndex++) {
char cCheck = passwordStr.charAt(nIndex);
if (nIndex == 0 && (cCheck == '-' || cCheck == '_')) {
return false;
}
if (!(isDigit(cCheck) || isAlpha(cCheck) || cCheck == '-' || cCheck == '_')) {
return false;
}
}
return true;
}
/**
* 此方法检查用户名是否包含非法字符 !!!!!!!!!!!此方法暂时注释
*
* @param str
* String
* @return boolean
*/
// public static boolean checkUserName(String str)
// {
// for(int i=0;i
// char c=str.charAt(i);
// if(c=='\'' || c=='\"')
// {
// return false;
// }
// }
// return true;
// }
/**
* 此方法用于替换字符文本中的非法字符
*
* @param str
* String
* @return String
*/
public static String replaceStr(String str) {
// CleanHtmlBr =
// Replace(Replace(Replace(Replace(str,"<","<"),">",">"),,
// "
"), chr(32)&chr(32)," ")
return str.replaceAll("<", "<").replaceAll(">", ">").replaceAll(
"'", "‘").replaceAll("\"", "“");
}
/**
* 此方法用于替换字符文本中"引号;
*
* @param str
* String
* @return String
*/
public static String replaceComma(String str) {
// CleanHtmlBr =
// Replace(Replace(Replace(Replace(str,"<","<"),">",">"),,
// "
"), chr(32)&chr(32)," ")
return str.replaceAll("'", "‘").replaceAll("\"", "“");
}
/**
* 此方法用于把字符“引号转换回普通引号";
*
* @param str
* String
* @return String
*/
public static String replaceReComma(String str) {
// CleanHtmlBr =
// Replace(Replace(Replace(Replace(str,"<","<"),">",">"),,
// "
"), chr(32)&chr(32)," ")
return str.replaceAll("‘", "'").replaceAll("“", "\"");
}
/**
* 此方法检查email有效性 返回提示信息 如果返回字符串ok则通过验证
*
* @param email
* String
* @return String
*/
public static String checkEmail(String email) {
String checkEmail = email;
String ok = "ok";
// 检测输入的EMAIL地址是否以 非法符号"."或"@"作为起始字符
Pattern p = Pattern.compile("^\\.|^\\@");
Matcher m = p.matcher(checkEmail);
if (m.find()) {
ok = "EMAIL地址不能以'.'或作为起始字符";
return ok;
}
// 检测是否以"www."为起始
p = Pattern.compile("^www\\.");
m = p.matcher(checkEmail);
if (m.find()) {
ok = "EMAIL地址不能以'www.'起始";
return ok;
}
// 检测是否包含非法字符
p = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+");
m = p.matcher(checkEmail);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while (result) {
// 如果找到了非法字符那么就设下标记
deletedIllegalChars = true;
// 如果里面包含非法字符如冒号双引号等,那么就把他们消去,加到SB里面
m.appendReplacement(sb, "");
result = m.find();
}
m.appendTail(sb);
checkEmail = sb.toString();
if (deletedIllegalChars) {
ok = "输入的EMAIL地址里包含有冒号、逗号等非法字符,请修改";
}
return ok;
}
}