<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url"> jdbc:mysql://localhost/projectmovie?characterEncoding=UTF-8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">validate</property> <property name="current_session_context_class">thread</property> <!-- Mapping files --> <mapping class="annotaion.Time" /> </session-factory> </hibernate-configuration>
package util; import org.hibernate.SessionFactory; import org.hibernate.Session; import org.hibernate.HibernateException; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; public class HibernateUtil { private static final SessionFactory sessionFactory; private static final ThreadLocal<Session> threadSession = new ThreadLocal<Session>(); private static final ThreadLocal<Transaction> threadTransaction = new ThreadLocal<Transaction>(); static { try { sessionFactory = new AnnotationConfiguration().configure() .buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static Session getSession() { Session session = (Session) threadSession.get(); try { if (session == null) { session = sessionFactory.openSession(); threadSession.set(session); } } catch (HibernateException ex) { throw ex; } return session; } public static void closeSession() { try { Session session = (Session) threadSession.get(); threadSession.set(null); if (session != null && session.isOpen()) session.close(); } catch (HibernateException ex) { throw ex; } } public static void beginTransaction() { Transaction tx = (Transaction) threadTransaction.get(); try { if (tx == null) { tx = getSession().beginTransaction(); threadTransaction.set(tx); } } catch (HibernateException ex) { throw ex; } } public static void commitTransaction() { Transaction tx = (Transaction) threadTransaction.get(); try { if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) { tx.commit(); } threadTransaction.set(null); } catch (HibernateException ex) { rollbackTransaction(); throw ex; } } public static void rollbackTransaction() { Transaction tx = (Transaction) threadTransaction.get(); try { threadTransaction.set(null); if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) { tx.rollback(); } } catch (HibernateException ex) { throw ex; } } }
package dao; import util.HibernateUtil; import annotaion.Time; import java.util.List; import java.util.Arrays; import org.hibernate.Criteria; import org.hibernate.Session; public class TimeDAO { public void insert(Time time) throws Exception { try { Session session = HibernateUtil.getSession(); session.save(time); } catch (Exception ex) { throw ex; } } public void update(Time time) throws Exception { try { Session session = HibernateUtil.getSession(); session.update(time); } catch (Exception ex) { throw ex; } } public void delete(Time time) throws Exception { try { Session session = HibernateUtil.getSession(); session.delete(time); } catch (Exception ex) { throw ex; } } public Time findByPK(String TimID) throws Exception { Time time = null; try { Session session = HibernateUtil.getSession(); time = (Time) session.get(Time.class, TimID); return time; } catch (Exception ex) { throw ex; } } public List<Time> findAll() throws Exception { List<Time> list = null; try { Session session = HibernateUtil.getSession(); Criteria crit = session.createCriteria(Time.class); list = toList(crit.list()); } catch (Exception ex) { throw ex; } return list; } public static List<Time> toList(final List<?> beans) { if (beans == null) return null; if (beans.isEmpty()) return null; int size = beans.size(); Time[] list = new Time[size]; list = (Time[]) beans.toArray(list); return Arrays.asList(list); } }
package annotaion; import javax.persistence.Entity; import javax.persistence.Table; import javax.persistence.Id; import javax.persistence.Column; import java.util.Date; @Entity @Table(name = "time") public class Time { @Id @Column(name = "TimeID") private String TimeID; @Column(name = "Time") private Date Time; public String getTimeID() { return TimeID; } public void setTimeID(String timeID) { TimeID = timeID; } public Date getTime() { return Time; } public void setTime(Date time) { Time = time; } public Time() { } public Time(String timeID,Date time) { this.TimeID = timeID; this.Time = time; } }
package client; import annotaion.Time; import util.HibernateUtil; import dao.TimeDAO; import java.util.List; public class ClientTime { public static void main(String[] args) { ClientTime tim = new ClientTime(); tim.addTime(); } public void addTime() { Time tm1 = new Time("T001", 9:00); Time tm2 = new Time("T002", 13:00); Time tm3 = new Time("T003", 19:00); try { HibernateUtil.beginTransaction(); TimeDAO timeDAO = new TimeDAO(); timeDAO.insert(tm1); timeDAO.insert(tm2); timeDAO.insert(tm3); HibernateUtil.commitTransaction(); } catch (Exception ex) { ex.printStackTrace(); HibernateUtil.rollbackTransaction(); System.out.print("เกิดข้อผิดพลาดในการเพิ่มข้อมูล Time"); } finally { HibernateUtil.closeSession(); } } }
เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง