I use hibernate 4.3.6 and mysql5.6,then I download mysql-connector-java-5.1.33-bin.jar and put it into classpath,I want to visit mysql,like follows:
hibernate.cfg.xml
Code:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Student.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="demo">
<class name="Student" table="tab_stu">
<id name="sid" column="sid"></id>
<property name="sname" column="sname"></property>
<property name="age" column="age"></property>
</class>
</hibernate-mapping>
StudentTest.java
Code:
package demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class StudentTest {
public static void main(String args[]){
Student stu = new Student();
stu.setSid("3");
stu.setAge(25);
stu.setSname("Kate");
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(stu);
session.getTransaction().commit();
session.close();
sf.close();
}
}
Student.java
Code:
package demo;
public class Student {
private String sid;
private String sname;
private int age;
public Student() {
super();
}
public Student(String sid, String sname, int age) {
super();
this.sid = sid;
this.sname = sname;
this.age = age;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", age=" + age+ "]";
}
}
When I run above code,it raise following error,where is wrong? How to correct? Thanks
[java] Oct 09, 2014 7:24:25 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
[java] INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
[java] Oct 09, 2014 7:24:25 PM org.hibernate.Version logVersion
[java] INFO: HHH000412: Hibernate Core {4.3.6.Final}
[java] Oct 09, 2014 7:24:25 PM org.hibernate.cfg.Environment <clinit>
[java] INFO: HHH000206: hibernate.properties not found
[java] Oct 09, 2014 7:24:25 PM org.hibernate.cfg.Environment buildBytecodeProvider
[java] INFO: HHH000021: Bytecode provider name : javassist
[java] Oct 09, 2014 7:24:25 PM org.hibernate.cfg.Configuration configure
[java] INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
[java] Oct 09, 2014 7:24:25 PM org.hibernate.cfg.Configuration getConfigurationInputStream
[java] INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
[java] Oct 09, 2014 7:24:25 PM org.hibernate.cfg.Configuration addResource
[java] INFO: HHH000221: Reading mappings from resource: Student.hbm.xml
[java] Oct 09, 2014 7:24:25 PM org.hibernate.cfg.Configuration doConfigure
[java] INFO: HHH000041: Configured SessionFactory: null
[java] Oct 09, 2014 7:24:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
[java] WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
[java] Oct 09, 2014 7:24:26 PM org.hibernate.dialect.Dialect <init>
[java] INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
[java] org.hibernate.service.spi.ServiceException: Specified JDBC Driver org.hibernate.dialect.MySQLDialect could not be loaded
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:194)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:771)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:221)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:135)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:108)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:483)
[java] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[java] at org.apache.tools.ant.Task.perform(Task.java:348)
[java] at org.apache.tools.ant.Target.execute(Target.java:435)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:456)
[java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
[java] at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
[java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[java] at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
[java] at org.apache.tools.ant.Main.runBuild(Main.java:851)
[java] at org.apache.tools.ant.Main.startAnt(Main.java:235)
[java] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
[java] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
[java] Caused by: org.hibernate.service.spi.ServiceException: Specified JDBC Driver org.hibernate.dialect.MySQLDialect could not be loaded
[java] at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:205)
[java] at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:156)
[java] at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:95)
[java] at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
[java] at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
[java] at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
[java] at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
[java] at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
[java] at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
[java] at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
[java] at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
[java] at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
[java] at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
[java] at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
[java] at demo.StudentTest.main(Unknown Source)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:483)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:217)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:152)
[java] ... 21 more
[java] Caused by: java.lang.ClassCastException: org.hibernate.dialect.MySQLDialect cannot be cast to java.sql.Driver
[java] at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:202)
[java] ... 41 more
[java] Java Result: -1