根据database_params.conf数据库配置信息重写hibernate.cfg.xml文件

具体请点击:http://www.verydemo.com/demo_c146_i16183.html

根据同事的需求写的。

用这个工具可以解决配置文件只需要配一次数据库信息。

 package com.utstar.nms.vmm.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Hashtable;

....................................................................


/**
 * Author: Steven Zhu
 * Date:2008-1-18 下午05:27:08
 * NMS, UTStarcom, Shenzhen
 */

public class HibernateConfigFileTool
{

 public static void reWriteHibernateConfigFile(String fromFile, String toFile)
 {
   String database_params =fromFile;
  
   File hibernateConfigFile = new File(toFile);
   File tmpConfigFile = new File("conf/qpf/hibernate/hibernate_tmp.cfg.xml");
  
   BufferedReader br;
   BufferedWriter bw;
   String line="";
  try
  {
   Hashtable dbParamHash =readDatabaseConfigInfo(database_params);
   br = new BufferedReader(new FileReader(hibernateConfigFile));
   bw = new BufferedWriter(new FileWriter(tmpConfigFile));
   line = br.readLine();
   
   while(line !=null)
   {
    line = reWriteDatabaseInfo(line,dbParamHash);
    bw.write(line);
    bw.newLine();
    line=br.readLine();
   }
   
   if(br !=null)  br.close();
      if(bw != null)  bw.close();
      //把替换后的结果覆盖hibernate配置文件
      FileTool.copySingleFile(tmpConfigFile, hibernateConfigFile);
      //删除临时文件
      FileTool.deleteFile(tmpConfigFile.getAbsolutePath());
  }
  catch (Exception e)
  {
           e.printStackTrace();
  }
     
        
 }
 
 public static String replaceMiddleString(String inString,String prefix,String suffix,String replaceStr )
 {
   String result=prefix+replaceStr+suffix;  
  return result;
 }

 public static Hashtable readDatabaseConfigInfo(String stringConfigurationFileName)
 {
  
  try
  {
   Hashtable hashresult = WacosConfigurator.readDatabaseParametersConfigurationFile(stringConfigurationFileName); 
     return hashresult;
  }
  catch (Exception e)
  {
           e.printStackTrace();
          
  }
  return null;
     
 }
   
 public static String reWriteDatabaseInfo(String line,Hashtable hash)
 {
  //System.out.println("before:"+line);
  String result="";
  String prefix="";
  String suffix="</property>";
  String replacement="";
  String connection_username = "<property name=/"connection.username/">";
  String connection_password = "<property name=/"connection.password/">";
  String connection_url = "<property name=/"connection.url/">";
  String connection_drivername="<property name=/"connection.driver_class/">";
  try
  {
   
   if(line.indexOf(connection_username)>=0)
   {//本行用配置文件的用户名来替换
     prefix=connection_username;
     replacement= (String)(hash.get("username"));
    
    result =replaceMiddleString(line,prefix,suffix,replacement);
   }
   else if(line.indexOf(connection_password)>=0)
   {   //本行用配置文件的密码来替换
    prefix=connection_password;
       replacement= (String)(hash.get("password"));
    
    result =replaceMiddleString(line,prefix,suffix,replacement);
   }
   else if(line.indexOf(connection_url)>=0)
   {
     //本行用配置文件的url来替换
    prefix=connection_url;
       replacement= (String)(hash.get("url"));
    
    result =replaceMiddleString(line,prefix,suffix,replacement);
   }else if(line.indexOf(connection_drivername)>=0){
    prefix=connection_drivername;
       replacement= (String)(hash.get("drivername"));
    
    result =replaceMiddleString(line,prefix,suffix,replacement);
   }
   else
   {
    result=line;
   }
  
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  //System.out.println("after:"+result);
  return result;
 }
 
 
 public static void main(String[] args)
 {
  //reWriteHibernateConfigFile();

 }
}

相关推荐