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 3:42:37 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: User.hbm.xml
二月 02, 2013 3:42:37 下午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
二月 02, 2013 3:42:38 下午 org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
二月 02, 2013 3:42:38 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
二月 02, 2013 3:42:38 下午 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
二月 02, 2013 3:42:38 下午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
二月 02, 2013 3:42:38 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" java.lang.UnsupportedOperationException: The application must supply JDBC connections
at org.hibernate.service.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:62)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1395)
at com.HibernateUserDemo.createStu(HibernateUserDemo.java:14)
at com.HibernateUserDemo.main(HibernateUserDemo.java:33)
为什么这儿一直提示需要JDBC connections,数据库服务器也打开了,这是什么情况?? hibernate jdbc sqlserver
[解决办法]
INFO: HHH000041: Configured SessionFactory: null
这个创建的sessionFactory产生的实例是 null
还是加载hibernate核心配置文件的时候报的错!!!