eclipse ssh连接sqlserver express

public static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static String url = "jdbc:sqlserver://ender-pc\\sqlexpress:1433;DatabaseName=test;";
public static String user = "sa";
public static String pwd = "xx2000";

 

一定要手动设置sqlexpress的端口号为1433

protocols for sqlexpress->tcp-ip->ip all->1433

 

另外在创建Hibernate Console Configuration 时出现could not parse configuration ...\hibernate.cfg.xml的错误提示时

估计要把hibernate.cfg.xml中的doctype头设置为本地dtd如下:

d:\j2ee_workspace\test\dtd\hibernate-configuration-3.0.dtd

 

当然要建立dtd文件夹,并把hibernate-configuration-3.0.dtd文件拷贝到dtd文件夹中

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><!-- 或者d:\dtd\hibernate-configuration-3.0.dtd -->
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.password">en2000</property>
<property name="hibernate.connection.url">jdbc:sqlserver://ender-pc\sqlexpress:1433;databaseName=test</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.show_sql">true</property>
<!-- <property name="hibernate.hbm2ddl.auto">create</property> -->
<property name="connection.pool_size">150</property>
</session-factory>
</hibernate-configuration>

另外要注意文件的编码utf-8 ,能被当前项目识别

 

注意hbm.xml中类型的转换

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-8-1 22:04:42 by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class catalog="test" name="User" schema="dbo" table="[user]">
<id name="id" type="int">
<column name="Id"/>
<generator class="assigned"/>
</id>
<property generated="never" lazy="false" name="user" type="string">
<column name="[user]" sql-type="nvarchar(max)"/>
</property>
<property generated="never" lazy="false" name="pass" type="string">
<column name="pass" sql-type="nvarchar(180)"/>
</property>
</class>
</hibernate-mapping>

插入记录例子

Configuration cfg = new Configuration().configure().addClass(User.class);

String sfName = cfg.getProperty(Environment.SESSION_FACTORY_NAME);
System.out.println("cfname:" + sfName);
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build();

SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
Session session = sf.openSession();

session.beginTransaction();

User theUser = new User();
theUser.setId(77);
theUser.setUser("한국어");
theUser.setPass("一二三");
session.save(theUser);


User theUser1 = new User();
theUser1.setId(47);
theUser1.setUser("你麻痹");
theUser1.setPass("一二三");
session.save(theUser1);

session.getTransaction().commit();
session.close();
sf.close();

转载于:https://www.cnblogs.com/coolyylu/p/4687963.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值