您的位置:首页 > 数据库

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

2016-04-09 20:19 323 查看
有时候在用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();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java Hibernate Mysql