在HSQLDB创建数据库和基本的数据库访问(一) 中,介绍了怎么用hsqldb创建数据库和连接数据库,以及如何创建数据表。下面实际访问一下hsqldb中的数据,当然可以直接用jdbc连接即可,这里我用现在十分常用的hibernate来连接一下hsqldb,并对其进行操作。 在HSQLDB创建数据库和基本的数据库访问(一) 文章中,我在e:/testhsql/data中创建了数据库,下面咱们来启动它: step1:E:/testhsql/data>java -cp ../hsqldb.jar org.hsqldb.Server -database mydb ,执行这个命令就可以启动数据库引擎了。很简单吧 step2:E:/testhsql/data>java -cp ../hsqldb.jar org.hsqldb.util.DatabaseManager ,执行这句话,就可以打开hsqldb数据库的控制台了,呵呵。咱们选择server的运行方式,如图1
图1 这样就打开了数据库的控制台,可以看到上篇文章 HSQLDB创建数据库和基本的数据库访问(一) 中创建的Customer数据表了吧,呵呵。不多说了,进入正题,建立hibernate的项目吧。这里我没有使用eclipse工具,而是使用Idea创建了这个java工程,根据大家习惯吧,用什么工具不打紧。 ---------------------------- 导入什么hibernate必须的jar包什么的,我就不多说了,不熟悉的朋友可以Google一下,很多这样的文章,官方上更有每个jar包是什么作用的介绍。我的工程截图,如图2示 图2 我现在把基本文件的内容粘贴出来: 第一位的,当然是hibernate的配置文件,呵呵 hibernate.cfg.xml <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.HSQLDialect </property> <property name="connection.driver_class">org.hsqldb.jdbcDriver </property> <property name="connection.url">jdbc:hsqldb:hsql://localhost/ </property> <property name="connection.username">SA </property> <property name="connection.password"/> <mapping resource="com/css/Customer.hbm.xml"/> </session-factory> </hibernate-configuration> 基本说明:红字是需要注意部分,注意是“方言”,“驱动”和“数据库连接”,这里的数据库连接和图1的URL是对应的。啰嗦了,应该大家对这些很熟悉的,呵呵。 第二位的,当然是POJO文件 Customer.java package com.css; /** * Created by IntelliJ IDEA. * User: liuzhy * Date: 2009-1-9 * Time: 9:51:29 */ public class Customer { private Long id; private String firstname; private String lastname; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } } 第三位的,就是映射文件啦,Customer.hbm.xml <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.css.Customer " table="Customer "> <comment>hsqldb hibernate test</comment> <id name="id" column="id"> <generator class="increment"/> </id> <property name="firstname"/> <property name="lastname"/> </class> </hibernate-mapping> 万事俱备,就差调用了,呵呵 第四:InvokMain.java package com.css; import org.hibernate.cfg.Configuration; import org.hibernate.SessionFactory; import org.hibernate.Session; import org.hibernate.Query; import org.hibernate.Transaction; /** * Created by IntelliJ IDEA. * User: liuzhy * Date: 2009-1-9 * Time: 9:44:35 */ public class InvokMain { public static void main(String[] args) { Configuration configuration = new Configuration().configure(); SessionFactory sf = configuration.buildSessionFactory(); Session session = sf.openSession(); Customer customer = new Customer(); customer.setId(new Long(3)); customer.setFirstname("zuo"); customer.setLastname("dangti"); Transaction tx = session.beginTransaction(); session.save(customer); tx.commit(); session.close(); sf.close(); } } 我执行了两次 session.save 操作,当然,第二次的 Customer 对象是这样的 Customer customer = new Customer(); customer.setId(new Long(4)); customer.setFirstname("chen"); customer.setLastname("hui"); 看看数据表里面的情况吧,如图3示 当然,也可以试一下修改(update操作),呵呵 public static void main(String[] args) { Configuration configuration = new Configuration().configure(); SessionFactory sf = configuration.buildSessionFactory(); Session session = sf.openSession(); Customer customer = new Customer(); customer.setId(new Long(4)); customer.setFirstname("陈"); customer.setLastname("慧"); Transaction tx = session.beginTransaction(); session. update (customer); tx.commit(); session.close(); sf.close(); } 再看一下数据库吧 上面就是和hibernate的基本结合了,跟深入的内容和hsqldb就没有关系了,是hibernate的功能了,抛砖引玉,大家可以多试试。呵呵