通用数据库查询分析器Java实现

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); }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值