hibernate连接SQLServer 2005
下面是hibernate连接SQLServer 2005实现
package com;
import
org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUserDemo {
static Session session=null;
public static void createStu(User user){
try{
Transaction tx=session.beginTransaction();
session.save(user);
tx.commit();
}catch(
HibernateException e){
e.printStackTrace();
}finally{
session.close();
}
}
public static void main(String[] args) {
User user=new User();
user.setId(new Integer(1));
user.setName("王永");
user.setAge(new Integer(26));
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
session=sessionFactory.openSession();
createStu(user);
}
}
hibernate.cfg.xml放置src下面,内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate。connection。driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate。connection.url">sqlserver.url=jdbc:sqlserver://localhost:1433;DatabaseName=test</property>
<property name="hibernate。connection.username">sa</property>
<property name="hibernate。connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate。show_sql">true</property>
<mapping resource="User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
这儿的User类没有粘贴出来,SQLServer 2005驱动程序sqljdbc.jar放置WEB-INF/lib下
运行结果:
二月 02, 2013 3:42:32 下午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
二月 02, 2013 3:42:32 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.9.Final}
二月 02, 2013 3:42:32 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
二月 02, 2013 3:42:32 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
二月 02, 2013 3:42:32 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
二月 02, 2013 3:42:32 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
二月 02, 2013