大神幫忙啊?。?!找不到students表,ERROR: Table 'hibernate.students' doesn't exist
hibernate.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">root</property>
?? ??? ?<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
?? ??? ?<property name="connection.url">jdbc:mysql:///hibernate?userUnicode=true&characterEncoding=UTF-8</property>? ??? ?
?? ??? ?<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
?? ??? ?<property name="show_sql">true</property>
?? ??? ?<property name="format_sql">true</property>? ??? ?
?? ??? ?<property name="hbm2ddl.auto">create</property>
?? ??? ?<mapping resource="Students.hbm.xml"/>
??? </session-factory>
</hibernate-configuration>
Students.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 2016-4-3 15:32:14 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
??? <class name="Students" table="STUDENTS">
??????? <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>
Students.java
import java.util.Date;
//學(xué)生類
public class Students {
?? ?// 1.共有的類
?? ?// 2.提供共有的不帶參數(shù)的默認(rèn)的構(gòu)造方法
?? ?// 3.屬性私有
?? ?// 4.屬性getter/setter方法
?? ?private int sid;
?? ?private String sname;
?? ?private String gender;
?? ?private Date birthday;
?? ?private String address;
?? ?public Students() {
?? ?}
?? ?public Students(int sid, String sname, String gender, Date birthday, String address) {
//?? ??? ?super();
?? ??? ?this.sid = sid;
?? ??? ?this.sname = sname;
?? ??? ?this.gender = gender;
?? ??? ?this.birthday = birthday;
?? ??? ?this.address = address;
?? ?}
?? ?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;
?? ?}
?? ?@Override
?? ?public String toString() {
?? ??? ?return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
?? ??? ??? ??? ?+ ", address=" + address + "]";
?? ?}
?? ?
?? ?
}
StudentsTest.java
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
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(){
?? ??? ?//創(chuàng)建配置對(duì)象
?? ??? ?Configuration config=new Configuration().configure();
?? ??? ?//創(chuàng)建服務(wù)注冊(cè)對(duì)象
?? ??? ?ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
?? ??? ?//創(chuàng)建會(huì)話工廠對(duì)象
?? ??? ?sessionFactory=config.buildSessionFactory(serviceRegistry);
?? ??? ?//會(huì)話對(duì)象
?? ??? ?session=sessionFactory.openSession();
?? ??? ?//開啟事物
?? ??? ?transaction=session.beginTransaction();
?? ??? ?
?? ?}
?? ?
?? ?@After
?? ?public void destory(){
?? ??? ?transaction.commit();
?? ??? ?session.close();
?? ??? ?sessionFactory.close();
?? ??? ?
?? ?}
?? ?
?? ?@Test
?? ?public void testSaveStudents(){
?? ??? ?Students s=new Students(1,"張三豐","男",new Date(),"武當(dāng)山");
?? ??? ?session.save(s);
?? ?}
}
2016-04-04
要不使用自己的數(shù)據(jù)庫連接下 jdbc:mysql://localshost:3306
2016-04-04
你mysql 的數(shù)據(jù)庫有嗎