在用Hibernate访问数据库时,我想播放一段动画,但动画白屏,请高手帮忙看看(Java Application,非网站)我完整的代码是
           
import java.io.*;
import java.util.List;import javax.swing.*;import org.hibernate.Transaction;
public class A extends JFrame { /**
 * @param args
 */
public static void main(String[] args) {
new A().init();
} public void init() {
ProgressDialog dlg = new ProgressDialog();
dlg.run();
int i = 0;

XDAO dao = new XDAO();
Transaction tran = dao.getSession().beginTransaction();
List  list=dao.findAll();
tran.commit();
dao.getSession().close();; }}
import javax.swing.*;
import javax.swing.JDialog;
import javax.swing.JLabel;
public class ProgressDialog extends JDialog implements Runnable{
/**
 * 
 */
private static final long serialVersionUID = -8295658766174610037L; public  ProgressDialog() {

}
public  synchronized void run() {
ImageIcon icon = new ImageIcon(
"F:\\MyEclipse 8.x\\AlarmFusion\\bin\\progress.gif"); // Animated
this.setSize(200, 100);
getContentPane().add(new JButton(icon));
this.setVisible(true); }


}package cn.edu.jlu.alarmcorrelator.hb;import java.util.List;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;/**
 * A data access object (DAO) providing persistence and search support for Alarm
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see cn.edu.jlu.alarmcorrelator.hb.Alarm
 * @author MyEclipse Persistence Tools
 */public class XDAO extends BaseHibernateDAO {
@SuppressWarnings("unchecked")
public List findAll() {
log.debug("finding all Alarm instances");
try {
String queryString = "from Alarm";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
} }
import org.hibernate.Session;/**
 * Data access object (DAO) for domain model
 * 
 * @author MyEclipse Persistence Tools
 */
public class BaseHibernateDAO implements IBaseHibernateDAO { public Session getSession() {
return HibernateSessionFactory.getSession();
}}
[code]
package cn.edu.jlu.alarmcorrelator.hb;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;/**
 * Configures and provides access to Hibernate sessions, tied to the current
 * thread of execution. Follows the Thread Local Session pattern, see
 * {@link http://hibernate.org/42.html }.
 */
public class HibernateSessionFactory { /**
 * Location of hibernate.cfg.xml file. Location should be on the classpath
 * as Hibernate uses #resourceAsStream style lookup for its configuration
 * file. The default classpath location of the hibernate config file is in
 * the default package. Use #setConfigFile() to update the location of the
 * configuration file for the current session.
 */
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static String configFile = CONFIG_FILE_LOCATION;
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
 * Close the single hibernate session instance.
 * 
 * @throws HibernateException
 */
public static void closeSession() throws HibernateException {
Session session = threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
 * return hibernate configuration
 * 
 */
public static Configuration getConfiguration() {
return configuration;
} /**
 * Returns the ThreadLocal Session instance. Lazy initialize the
 * <code>SessionFactory</code> if needed.
 * 
 * @return Session
 * @throws HibernateException
 */
public static Session getSession() throws HibernateException {
Session session = threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} /**
 * return session factory
 * 
 */
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
 * Rebuild hibernate session factory
 * 
 */
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
 * return session factory
 * 
 * session factory will be rebuilded in the next call
 */
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
} private HibernateSessionFactory() {
}}[/code]

解决方案 »

  1.   

    想播放一段动画,等待执行数据库操作?那应该在run()中启动数据库操作,操作dlg传到数据库操作类中,操作完成后,关闭dlg
      

  2.   

    我试了,不好用啊。能说得具体点不?ProgressDialog 在程序其他部分多次使用,都没问题(拷贝文件的时候也播放过代码),只有访问数据库的时候会卡死
    import java.io.*;import javax.swing.*;public class A extends JFrame {/**
     * @param args
     */
    public static void main(String[] args) {
    new A().init();
    }public void init() {
    ProgressDialog dlg = new ProgressDialog();
    dlg.run();
    int i = 0;new CopyFile().copy ("e:\\important.sys","c:\\abc.exe");}}class CopyFile {
    public boolean copy(String file1, String file2) {
    try // must try and catch,otherwide will compile error
    {
    // instance the File as file_in and file_out
    java.io.File file_in = new java.io.File(file1);
    java.io.File file_out = new java.io.File(file2);
    FileInputStream in1 = new FileInputStream(file_in);
    FileOutputStream out1 = new FileOutputStream(file_out);
    byte[] bytes = new byte[1024];
    int c;
    while ((c = in1.read(bytes)) != -1)
    out1.write(bytes, 0, c);
    in1.close();
    out1.close();
    return (true); // if success then return true
    }catch (Exception e) {
    System.out.println("Error!");
    return false;
    }
    }
    }