-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Creating database using hibernate
PostPosted: Wed Jun 20, 2007 8:19 am 
Newbie

Joined: Wed May 16, 2007 2:27 am
Posts: 2
Location: India
Hi,

Can i create a database using hibernate?
I am using mysql. My hibernate config file is as follows.

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/testdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>
<property name="hibernate.hbm2ddl.auto">create-update</property>
<!-- Mapping files -->
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

I can create and update the table using SchemaUpdate.
Is there any way such that I can create a db if not present in mysql before creating a table?

Thanks,
Naval.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 6:03 am 
Newbie

Joined: Wed May 16, 2007 2:27 am
Posts: 2
Location: India
From an older post i found the following code. This works but needs a default db e.g. test to connect. Is there a way to create db without connecting to already present db?

org.hibernate.connection.DriverManagerConnectionProvider cn=new org.hibernate.connection.DriverManagerConnectionProvider();
Properties p=new Properties();
// p.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/db1");
p.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
p.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
p.setProperty("hibernate.connection.username","root");
p.setProperty("hibernate.connection.password","admin");
p.setProperty("hibernate.connection.url","jdbc:mysql://localhost:3306/test");
p.setProperty("hibernate.order_updates", "true");
p.setProperty("hibernate.show_sql", "true");
p.setProperty("hibernate.format_sql", "true");
p.setProperty("hibernate.generate_statistics", "true");
p.setProperty("hibernate.use_sql_comments", "true");
p.setProperty("hibernate.connection.autocommit", "true");
cn.configure(p);
//cn.getConnection().setCatalog("db1");
String sql="CREATE DATABASE IF NOT EXISTS "+ "db1";
System.out.println("Executing..."+sql);
cn.getConnection().createStatement().execute(sql);
System.out.println("Execution Completed!!");
cn=null;


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 22, 2007 3:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
why don't you just create it via normal jdbc ? nothing hibernate specific in this.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.