hibernate动态链接数据库配置文件问题

有时候在用hibernate连接数据库时会将用户名和密码都保存在hibernate.cfg.xml这个文件中,这样每次连接数据库时都用里面的用户名和密码,但是如果用于别的机器上,恰巧数据库的用户名和密码不是配置文件中的内容,则会无法连接。如要修改,只能手动重新修改这个配置文件,这样非常麻烦。于是我写了一个工具类,用于在程序中动态修改用户名和密码,这样在需要调用时就直接调用方法即可。

个人建议在连接数据库时就调用此方法(因为代码只是一个演示功能,只是希望能给大家提供一个参考而已)。话不多说,直接上代码:



import hibernateSF.HibernateSessionFactory;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Console;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import org.hibernate.Session;
import domain.Department;
public class HibernateSettingUtil
{

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{

changeDBLoginInfo("/hibernate.cfg.xml", true, true);


//此项目中已经添加了Hibernate,所以有这个方法。
Session se = HibernateSessionFactory.getSession();


//例子而已
se.save(new Department(1232121, "name1"));


se.beginTransaction().commit();
}


/**
* 获取控制台输入的内容
* @param tooltip eg:Enter the username of the database:
* @return
*/
public static String getConsoleContent(String tooltip)
{
System.out.println(tooltip);
try
{
return new BufferedReader(new InputStreamReader(System.in)).readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}

/**
* 从控制台获取密码,在IDE环境下密码可见,在标准控制台中不会显示输入的密码
* @param tooltip
* @return
*/
public static String getConsolePwd(String tooltip)
{
System.out.println(tooltip);
System.out.println("\t(Tips:There are maybe noting on surface when you input)");
Console cons = System.console();
if (cons == null)
{
System.out.println("\t(This IDE do not support the Console)");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String password;
try
{
password = br.readLine();
return password;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
else
{
char[] passwd = cons.readPassword();
return new String(passwd);
}
}

/**
* 修改xml配置文件的内容
* @param xmlFilePath  eg:"/hibernate.cfg.xml"
* @param isChangeUid 
* @param isChangePwd
*/
public static void changeDBLoginInfo(String xmlFilePath, boolean isChangeUid, boolean isChangePwd)
{

try
{
String filePath = new HibernateSettingUtil().getClass().getResource(xmlFilePath).getFile();
try
{
filePath = URLDecoder.decode(filePath, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}

File file = new File(filePath);
StringBuilder sb = new StringBuilder();
BufferedReader bf = new BufferedReader(new FileReader(file));

String linText = null;

while((linText=bf.readLine())!=null)
{
if(linText.contains("connection.username"))
{
if(isChangeUid)
{
int startIndex = linText.lastIndexOf("\"")+2;
sb.append(linText.substring(0,startIndex));//<property name="connection.username">
sb.append(getConsoleContent("Enter the username of the database:"));//属性的值
sb.append("</property>\r\n");
}
}

else if(linText.contains("connection.password"))
{
if(isChangePwd)
{
int startIndex = linText.lastIndexOf("\"")+2;
sb.append(linText.substring(0,startIndex));//<property name="connection.username">
sb.append(getConsolePwd("Enter the password of the database:"));//属性的值
sb.append("</property>\r\n");
}
}
else
{
sb.append(linText+"\r\n");
}
}

if(bf!=null)
bf.close();

BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(sb.toString());
if(bw!=null)
bw.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值