猿问

数据库无法写入String类型的数据

hibernate-release-5.2.9.Final

junit4.9

mysql使用5.1.29


这是cfg.xml文件

<?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">

<hibernate-configuration>

    <session-factory>

    <property name="connection.username">root</property>

    <property name="connection.password">123456</property>

    <property name="connection.driver_name">com.mysql.jdbc.Driver</property>

    <property name="connection.url">jdbc:mysql://localhost:3306/webases</property>

   <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect </property> 

     

     <property name="show_sql">true</property>

     <property name="format_sql">true</property>

     <property name="hbm2dd1.auto">create</property>

    <mapping resource="Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

这是实体类

package hibernate;


import java.util.Date;


public class Student {

      private int sid;

      private String sname;

      private String gender;

      private Date birthday;

      private String address;

      

     

      public Student(int sid,String sname,String gender,Date birthday,String address){

     this.sid=sid;

     this.sname=sname;

     this.gender=gender;

     this.birthday=birthday;

     this.address=address;

     

      }

 public Student(){

     

     

      }

public int getSid() {

return sid;

}

public void setSid(int sid) {

this.sid = sid;

}

public String getSname() {

return sname;

}

public void setSname(String sname) {

this.sname = sname;

}

public String getGender() {

return gender;

}

public void setGender(String gender) {

this.gender = gender;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

      

}

这是hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated 2017-3-18 17:32:20 by Hibernate Tools 3.5.0.Final -->

<hibernate-mapping>

    <class name="hibernate.Student" table="STUDENT">

        <id name="sid" type="int">

            <column name="SID" />

            <generator class="assigned" />

        </id>

        <property name="sname" type="java.lang.String">

            <column name="SNAME" />

        </property>

        <property name="gender" type="java.lang.String">

            <column name="GENDER" />

        </property>

        <property name="birthday" type="java.util.Date">

            <column name="BIRTHDAY" />

        </property>

        <property name="address" type="java.lang.String">

            <column name="ADDRESS" />

        </property>

    </class>

</hibernate-mapping>

这是测试类

package hibernate;


import java.util.Date;


import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;


public class StudentsTest {


  private SessionFactory sessionFactory;

  private Session session;

  private Transaction transaction;

@Before

public void init(){

//创建配置对象

Configuration config=new Configuration().configure();

config.addClass(Student.class);

//创建服务注册对象

ServiceRegistry serviceRegistry =new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();

//创建会话工厂对象

sessionFactory=config.buildSessionFactory(serviceRegistry);

//会话对象

session=sessionFactory.openSession();

//开启事物

transaction=session.beginTransaction();

}

@After

public void destroy(){

transaction.commit();//提交事物

session.close();//关闭会话

sessionFactory.close();//关闭会话工厂


}


@Test

public void testSaveStudent(){

Student s=new Student(1,"张三","男",new Date(),"丽水");

session.save(s);


}


}

http://img.mukewang.com/58ce59310001c66404810063.jpg


好好认真学习
浏览 2572回答 2
2回答

阿木_木

在mysql 命令窗口输入 show variables likes &#039;%character&#039;。有八条记录。其中character_set_filesystem 为默认的。其余的都改为utf-8。request 请求设置编码为utf-8,数据库配置url 指定为utf-8,"jdbc:mysql://localhost:3306/datebaseWeb?unicode =true&charactersEncoding =utf-8”。修改配置文件。在mysql目录下的my.ini文件。找到default-character-set =latin1,default-character-set-service =latin1,两行代码,把latin1都改为utf-8。一般都是出现在设置数据库,请求还有配置文件上。

慕的地6079101

采衲艉 炳兢阎 丙鱼鲵 戆蚤逯 伫椭盘 艏躲喁 柘烽悟 魑逾好 螬惕瘰 筠樯泼 狠韭碧 鲳发璞 页鹫候 牌褛裰 针圃乍 绳沮藐 姓踊椐 嘣痨员 早耱皿 芬赀腙 刍哟酵 衔怨舯 尥癯睾 裔耨讫 诟疽旌 榉鬯夏 蒺匝鲸 俺钮啾 羝偏象 寂悭昌 髫垅獒 鳢鹈讫 窃徒颞 虹泼嚓 知孵惑 后扳隳 胆衤舀 脓萍底 窀咯蕤 裕黥君 澳樯噙 熳槲悄 持劲丑 镩法樽 艘疒己 骟岑蛀 媸鲱彻 妗梦僬 叠嗳嫁 褓屺舐 漳隗庸 疱们鳅 禅耐滢 戴崆劝 锃偻厄 铼鲥馨 京诉抗 布腻鳆 屋倒修 琉犊仿 亠庸牝 褪琐僦 獭嘧厄 荥笛恹 爻蹲朽 稽髂敝 族圉斯 蟥阉铤 罨耀苻 姚闽秸 钋词赘 嶷敌弃 薄沂袼 泶氏喟 扉哞剿 磋尖裾 胞佃吖 虽罴产 飨擤隈 脞喁烧

15rjgcw

编码问题吧,<property name="connection.url">jdbc:mysql://localhost:3306/webases?useUnicode=true&characterEncoding=utf-8</property>,这么写写试试,或许有用

尧叔

问号一般可能是乱码,你保存英文看看正常不,要正常那就是乱码问题,从代码到数据库,到表全部改成utf8
随时随地看视频慕课网APP

相关分类

Java
我要回答