-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Please help me with accessing MySql problem
PostPosted: Wed May 17, 2006 7:07 pm 
Newbie

Joined: Wed May 17, 2006 6:31 pm
Posts: 5
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>


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 8:15 pm 
Newbie

Joined: Wed May 17, 2006 6:06 pm
Posts: 8
Do you get any type of error messages when your run? Do you know if you are connecting to the database?

Dynoweb


Top
 Profile  
 
 Post subject: No error!!!
PostPosted: Thu May 18, 2006 8:10 am 
Newbie

Joined: Wed May 17, 2006 6:31 pm
Posts: 5
There is no error!!! nothing.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 8:28 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 5:33 am
Posts: 31
Does the DB get created at all?
Have you tried setting log4j in debug mode, to see if it can help you?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.