Hibernate 动态添加数据库(数据库信息不固定)

上篇文章利用spring来连接多个数据库并进行切换数据源,此时,数据库的信息都已经固定,不能更改;在某些项目中,我们要在程序中,动态的添加数据库连接,数据库的信息不确定,需要程序给出。下面是用Hibernate动态连接数据库的实例(项目环境:SSH(利用Hibernate注解))前提是:两个数据库中的数据结构一致:
public class HibernateConfiguration extends AnnotationConfiguration {
public HibernateConfiguration() {
super();
}

public void reset() {
super.reset();
}

public HibernateConfiguration(String dialect, String driverClass,
String ipAddress, String port, String dataBaseName,
String username, String password) {
String connection_url = "";
if (dialect.indexOf("MySQL") > -1) {
connection_url = "jdbc:mysql://" + ipAddress + "/" + dataBaseName;
} else if (dialect.indexOf("SQLServer") > -1) {
connection_url = "jdbc:sqlserver://" + ipAddress + ":" + port
+ ";DataBaseName=" + dataBaseName;
} else if (dialect.indexOf("Oracle") > -1) {
connection_url = "jdbc:oracle:thin:@" + ipAddress + ":" + port
+ ":" + dataBaseName;
} else {
throw new HibernateException("The dialect was not allowed.==fd=="
+ dialect);
}

super.setProperty("hibernate.dialect", dialect);
super.setProperty("hibernate.connection.url", connection_url);
super.setProperty("hibernate.connection.driver_class", driverClass);
super.setProperty("hibernate.connection.username", username);
super.setProperty("hibernate.connection.password", password);
}

public HibernateConfiguration(String dialect, String driverClass,
String ipAddress, String port, String dataBaseName,
String username, String password, String schema, String catalog) {
String connection_url = "";
if (dialect.indexOf("MySQL") > -1) {
connection_url = "jdbc:mysql://" + ipAddress + "/" + dataBaseName;
} else if (dialect.indexOf("SQLServer") > -1) {
connection_url = "jdbc:sqlserver://" + ipAddress + ":" + port
+ ";DataBaseName=" + dataBaseName;
} else if (dialect.indexOf("Oracle") > -1) {
connection_url = "jdbc:oracle:thin:@" + ipAddress + ":" + port
+ ":" + dataBaseName;
} else {
throw new HibernateException("The dialect was not allowed.==fd=="
+ dialect);
}
super.setProperty("hibernate.dialect", dialect);
super.setProperty("hibernate.connection.url", connection_url);
super.setProperty("hibernate.connection.driver_class", driverClass);
super.setProperty("hibernate.connection.username", username);
super.setProperty("hibernate.connection.password", password);
super.setProperty("hibernate.default_schema", schema);
super.setProperty("hibernate.default_catalog", catalog);
super.setProperty("hibernate.show_sql", "true");
}
}

public class TempSessionFactory {
public static HibernateConfiguration configuration = new HibernateConfiguration();
public static SessionFactory sessionFactory;
public static void reflashSessionFactory(
HibernateConfiguration tempConfiguration) {
try {
if (tempConfiguration.getProperty("hibernate.dialect").equals(
configuration.getProperty("hibernate.dialect"))
&& tempConfiguration
.getProperty("hibernate.connection.url")
.equalsIgnoreCase(
configuration
.getProperty("hibernate.connection.url"))
&& tempConfiguration
.getProperty("hibernate.connection.username")
.equals(configuration
.getProperty("hibernate.connection.username"))
&& tempConfiguration
.getProperty("hibernate.connection.password")
.equals(configuration
.getProperty("hibernate.connection.password"))) {

System.out
.println("%%%% TempSessionFactory is created aready!!-fd %%%%");
} else {
configuration.reset();
configuration = tempConfiguration;
sessionFactory = configuration.buildSessionFactory();
System.out
.println("%%%% TempSessionFactory is reflashed here!!-fd %%%%");
}
} catch (Exception e) {
System.err.println("%%%% Error Reflashing TempSessionFactory %%%%");
e.printStackTrace();
}
}
public static Session getSession() {
return sessionFactory.openSession();
}
}

//执行
/***
* 动态连接一个数据库
*/
LgispTerminal ter=(LgispTerminal) this.lgispTerminalService.getById(1);
System.out.println("-----------------terIp="+ter.getTerminalIntip()+"-----------");
HibernateConfiguration dyConfiguration=new HibernateConfiguration("org.hibernate.dialect.MySQLDialect","com.mysql.jdbc.Driver",ter.getTerminalIntip(),"3306","lgisp","root","root");
dyConfiguration.addAnnotatedClass(LgispOrg.class);
TempSessionFactory.reflashSessionFactory(dyConfiguration);
Session session1=TempSessionFactory.getSession();
if(session1!=null){
List listq=session1.createSQLQuery("select * from lgisp_org").list();
System.out.println("---sql----listq.size="+listq.size()+"----------");
List hqlList=session1.createQuery(" from LgispOrg org").list();
System.out.println("---------hqlList.size()="+hqlList.size()+"----------");
Transaction tx=session1.beginTransaction();
LgispOrg org=(LgispOrg) session1.get(LgispOrg.class,33);
System.out.println("-----org00="+org.getOrgName()+"----------------");
org.setOrgName("hibernate Org Add");
// session1.delete(org);
session1.saveOrUpdate(org);
tx.commit();
session1.close();
}else{
System.out.println("------session 为空--------------");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hibernate是一个Java持久化框架,可以连接MySQL数据库。连接MySQL数据库需要以下步骤: 1. 在项目中添加MySQL驱动程序的依赖项。 2. 在Hibernate配置文件中配置MySQL数据库的连接信息,包括数据库URL、用户名、密码等。 3. 在Java代码中使用Hibernate API来连接MySQL数据库,执行SQL语句或者操作数据库。 例如,以下是Hibernate配置文件中连接MySQL数据库的示例: ``` <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> </session-factory> </hibernate-configuration> ``` 其中,`hibernate.connection.driver_class`指定MySQL驱动程序的类名,`hibernate.connection.url`指定数据库的URL,`hibernate.connection.username`和`hibernate.connection.password`指定数据库的用户名和密码,`hibernate.dialect`指定Hibernate使用的MySQL方言,`hibernate.show_sql`指定是否在控制台输出SQL语句。 连接MySQL数据库后,可以使用Hibernate API来操作数据库,例如: ``` Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); // 执行SQL语句或者操作数据库 ... tx.commit(); session.close(); ``` 其中,`sessionFactory`是Hibernate的会话工厂,`session`是Hibernate的会话对象,`tx`是Hibernate的事务对象。在事务中执行SQL语句或者操作数据库,最后提交事务并关闭会话。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值