通用数据库查询分析器Java实现
2016-06-17 12:57
465 查看
package com.test.db; import java.sql.Connection; import java.sql.SQLException; import java.util.List; import java.util.Map; /** *数据库查询接口 *QQ:414361207 */ public interface Query { /** * 执行查询 * @param sql * @return */ public abstract Map<String,Object> query(String sql); /** * 执行增删改 * @param sql * @return */ public abstract Map<String,Object> update(String sql); /** * 返回dbName中所有的表、视图、存储过程对象 * @param dbName 数据库 * @param map数据连接信息 * @return */ public abstract Map<Type,List<SampleData>> getTables(Map<String,String> map) throws SQLException; /** * 查询表的列对象 * @param map数据连接信息 * @param tableName表名 * @return * @throws SQLException */ public abstract Map<Type,List<SampleData>> getColumnsOfTable(Map<String,String> map,String tableName) throws SQLException; /** * 查询表的键对象 * @param map数据连接信息 * @param tableName表名 * @return * @throws SQLException */ public abstract Map<Type,List<SampleData>> getKeysOfTable(Map<String,String> map,String tableName) throws SQLException; /** * 查询表的索引对象 * @param map数据连接信息 * @param tableName表名 * @return * @throws SQLException */ public abstract Map<Type,List<SampleData>> getIndexsOfTable(Map<String,String> map,String tableName) throws SQLException; /** * 返回当前的数据库名称 * @param sql * @return */ public abstract String getDbName(); /** * 获取数据库的链接信息 * @return */ public abstract Map<String,String> getLinkInfo(); /** * 释放连接 */ public abstract void closeConnection(); /** * 重新设置连接 * @param connection */ public abstract void setConnection(Connection connection); } package com.test.db; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; import java.util.Date; import java.util.Map; import java.util.Properties; import java.util.Set; import java.sql.Connection ; public class Tool { public static Properties prop; private static boolean state=false; static { prop=new Properties(); try { prop.load(Tool.class.getResourceAsStream("dbInfo.properties")); } catch (IOException e) { e.printStackTrace(); } } /** * 对应数据库的Query对象 * @param connection * @param linkInfo * @param type * @return * @throws IllegalArgumentException * @throws SecurityException * @throws InstantiationException * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws ClassNotFoundException */ public static Query getQuery(Connection connection,Map<String,String> linkInfo,String type) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException { if("true".equals(prop.getProperty("CommonQuery"))) { return new CommonQuery(connection,linkInfo,prop.getProperty("getDbName_"+type),prop.getProperty("getTables_"+type),prop.getProperty("getColumnsOfTable_"+type),prop.getProperty("getIndexsOfTable_"+type),prop.getProperty("getKeysOfTable_"+type)); } return (Query) Class.forName(prop.getProperty(type)).getConstructor( Connection.class,Map.class).newInstance(connection,linkInfo); } /** * 获取所有的数据库类型 * @return */ public static String[] getAllDBType() { return prop.getProperty("database_type").split(","); } /** * 获取数据库驱动类 * @param key Driver_数据库类型 * @return */ public static String getDriverClass(String key) { return prop.getProperty(key); } /** * 验证期限 * @return true可用 false不可用 */ public static boolean invalidate() { try { if(!state) { Properties register=new Properties(); String currentDir=System.getProperty("currentDir"); register.load(new FileInputStream(currentDir+"\\register.properties")); Set keys=register.keySet(); int count=0; String key=null;//使用期限 String value=null;//密码 if(keys.size()>1) { return false; } for(Object o:keys) { if(count==0) { key=o.toString(); value=register.getProperty(key); break; } } if((!DBUtil.isEmpty(key))&&(!DBUtil.isEmpty(value))) { Long rs=Long.valueOf(decrypt(key,value)); if(System.currentTimeMillis()<rs) { state=true; return true; } } } else { return state; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } /** * 注册 */ public static void register() { Date current=new Date(); int month=current.getMonth(); current.setMonth(month+1); long registerTime=current.getTime(); state=true; try { String currentDir=System.getProperty("currentDir"); PrintWriter out=new PrintWriter(new File(currentDir+"\\register.properties")); out.println(registerTime+"="+encrypt(String.valueOf(registerTime))); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 加密 * @param key 最后使用期限 * @return */ private static String encrypt(String key) { String str=key+"this is a common sql query operation and get register code before link to database instance"; String str2="ZZCH-"+key+decode(str); return change(str2); } /** * 解密 * @param key 最后使用期限 * @param value 密码 * @return */ private static String decrypt(String key,String value) { String code=encrypt(key); if(code.equals(value)) {//两次密码一致验证通过 return key; } return null; } private static int decode(String s) { int i = 0; char[] ac = s.toCharArray(); int j = 0; int k = ac.length; while (j < k) { i = 31 * i + ac[j]; ++j; } return Math.abs(i); } private static String change(String s) { byte[] abyte0 = s.getBytes(); char[] ac = new char[s.length()]; int i = 0; int k = abyte0.length; while (i < k) { int j = abyte0[i]; if ((j >= 48) && (j <= 57)) j = (j - 48 + 5) % 10 + 48; else if ((j >= 65) && (j <= 90)) j = (j - 65 + 13) % 26 + 65; else if ((j >= 97) && (j <= 122)) j = (j - 97 + 13) % 26 + 97; ac[i] = (char)j; ++i; } return String.valueOf(ac); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android之获取手机上的图片和视频缩略图thumbnails
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序