Please help me with accessing MySql problem. I want create database first, then write some data,but it doesn't work. i don't know why?
i have 3 directory: lib, src,classes. lib contains Mysql driver and hibernate3.jar, src contains Customer.java,Test.java, Customer.hbm.xml. root directory contains hibernate.cfg.xml and build.xml. here is my code.
Customer.java
public class Customer {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setId(int id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
Test.java
import org.hibernate.*;
import org.hibernate.cfg.*;
public class Test {
public static void main(String[] args) {
try {
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
for (int i = 0; i < 200; i++) {
Customer customer = new Customer();
customer.setUsername("customer" + i);
customer.setPassword("customer");
session.save(customer);
}
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
Customer.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">
<hibernate-mapping>
<class name="Customer" table="CUSTOMER">
<id name="id" column="CID">
<generator class="increment" />
</id>
<property name="username" column="USERNAME" />
<property name="password" column="PASSWORD" />
</class>
</hibernate-mapping>
build.xml
<?xml version="1.0" ?>
<project name="My1stHibernate" default="build" basedir=".">
<property name="base.dir" value="." />
<property name="src.dir" value="src" />
<property name="lib.dir" value="lib" />
<property name="build.dir" value="classes" />
<path id="myclasspath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
<pathelement location="${build.dir}" />
</path>
<target name="init">
<mkdir dir="${build.dir}" />
</target>
<target name="build" depends="init" description="compile the source files">
<javac classpathref="myclasspath" srcdir="${src.dir}" destdir="${build.dir}" />
<copy todir="${build.dir}" >
<fileset dir="${src.dir}" >
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="run" depends="build">
<java classpathref="myclasspath" classname="Test" fork="true" />
</target>
<target name="clean">
<delete includeemptydirs="true">
<fileset dir="${build.dir}" />
</delete>
</target>
hibernate.cfg.xml
</project>
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">abc</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</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.NoCacheProvider</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
|