2。用java.util.ResourceBundlecȝgetBundle()Ҏ(gu)
CZQ?br /> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3。用java.util.PropertyResourceBundlecȝ构造函?br />CZQ?br /> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)I(yng)nputStream in = new BufferedInputStream(new FileInputStream(name));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)ResourceBundle rb = new PropertyResourceBundle(in);
4。用class变量的getResourceAsStream()Ҏ(gu)
CZQ?br /> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)I(yng)nputStream in = JProperties.class.getResourceAsStream(name);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Properties p = new Properties();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)p.load(in);
5。用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()Ҏ(gu)
CZQ?br /> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)I(yng)nputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Properties p = new Properties();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)p.load(in);
6。用java.lang.ClassLoadercȝgetSystemResourceAsStream()?rn)态方?br />CZQ?br /> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)I(yng)nputStream in = ClassLoader.getSystemResourceAsStream(name);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Properties p = new Properties();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)p.load(in);
补充
Servlet中可以用javax.servlet.ServletContext的getResourceAsStream()Ҏ(gu)
CZQ?br /> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)I(yng)nputStream in = context.getResourceAsStream(path);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Properties p = new Properties();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)p.load(in);
完整的示例,可以参考附件文?/font>
JProperties.java文g
/**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**/
package com.kindani;
//import javax.servlet.ServletContext;
import java.util.*;
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
/**
(tng)* 使用J2SE API読取Properties文g的六E方?br /> (tng)* User: SYNFORM
(tng)* Date: 2005/07/12
(tng)* Time: 18:40:55
(tng)* To change this template use File | Settings | File Templates.
(tng)*/
public class JProperties {
(tng) (tng) (tng) public final static int BY_PROPERTIES = 1;
(tng) (tng) (tng) public final static int BY_RESOURCEBUNDLE = 2;
(tng) (tng) (tng) public final static int BY_PROPERTYRESOURCEBUNDLE = 3;
(tng) (tng) (tng) public final static int BY_CLASS = 4;
(tng) (tng) (tng) public final static int BY_CLASSLOADER = 5;
(tng) (tng) (tng) public final static int BY_SYSTEM_CLASSLOADER = 6;
(tng) (tng) (tng) public final static Properties loadProperties(final String name, final int type) throws IOException {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) Properties p = new Properties();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) InputStream in = null;
(tng) (tng) (tng) (tng) (tng) (tng) (tng) if (type == BY_PROPERTIES) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) in = new BufferedInputStream(new FileInputStream(name));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) assert (in != null);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) p.load(in);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) } else if (type == BY_RESOURCEBUNDLE) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) assert (rb != null);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) p = new ResourceBundleAdapter(rb);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) } else if (type == BY_PROPERTYRESOURCEBUNDLE) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) in = new BufferedInputStream(new FileInputStream(name));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) assert (in != null);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) ResourceBundle rb = new PropertyResourceBundle(in);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) p = new ResourceBundleAdapter(rb);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) } else if (type == BY_CLASS) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) assert (JProperties.class.equals(new JProperties().getClass()));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) in = JProperties.class.getResourceAsStream(name);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) assert (in != null);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) p.load(in);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) // (tng) (tng) (tng) (tng) (tng) (tng) (tng) return new JProperties().getClass().getResourceAsStream(name);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) } else if (type == BY_CLASSLOADER) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) assert (JProperties.class.getClassLoader().equals(new JProperties().getClass().getClassLoader()));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) in = JProperties.class.getClassLoader().getResourceAsStream(name);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) assert (in != null);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) p.load(in);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) // (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) return new JProperties().getClass().getClassLoader().getResourceAsStream(name);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) } else if (type == BY_SYSTEM_CLASSLOADER) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) in = ClassLoader.getSystemResourceAsStream(name);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) assert (in != null);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) p.load(in);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) if (in != null) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) in.close();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) return p;
(tng) (tng) (tng) }
(tng) (tng) (tng) // ---------------------------------------------- servlet used
/*
(tng) (tng) (tng) public static Properties loadProperties(ServletContext context, String path) throws IOException {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) assert (context != null);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) InputStream in = context.getResourceAsStream(path);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) assert (in != null);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) Properties p = new Properties();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) p.load(in);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) in.close();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) return p;
(tng) (tng) (tng) }
*/
(tng) (tng) (tng) // ---------------------------------------------- support class
(tng) (tng) (tng) /**
(tng) (tng) (tng) (tng) * ResourceBundle Adapter class.
(tng) (tng) (tng) (tng) */
(tng) (tng) (tng) public static class ResourceBundleAdapter extends Properties {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) public ResourceBundleAdapter(ResourceBundle rb) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) assert (rb instanceof java.util.PropertyResourceBundle);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) this.rb = rb;
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) java.util.Enumeration e = rb.getKeys();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) while (e.hasMoreElements()) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) Object o = e.nextElement();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) this.put(o, rb.getObject((String) o));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) private ResourceBundle rb = null;
(tng) (tng) (tng) (tng) (tng) (tng) (tng) public ResourceBundle getBundle(String baseName) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) return ResourceBundle.getBundle(baseName);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) public ResourceBundle getBundle(String baseName, Locale locale) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) return ResourceBundle.getBundle(baseName, locale);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) public ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) return ResourceBundle.getBundle(baseName, locale, loader);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) public Enumeration<String> getKeys() {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) return rb.getKeys();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) public Locale getLocale() {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) return rb.getLocale();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) public Object getObject(String key) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) return rb.getObject(key);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) public String getString(String key) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) return rb.getString(key);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) public String[] getStringArray(String key) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) return rb.getStringArray(key);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) (tng) (tng) (tng) (tng) protected Object handleGetObject(String key) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) return ((PropertyResourceBundle) rb).handleGetObject(key);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) }
(tng) (tng) (tng) }
}
JPropertiesTest.java文g
/**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**/
package com.kindani.test;
import junit.framework.*;
import com.kindani.JProperties;
//import javax.servlet.ServletContext;
import java.util.Properties;
public class JPropertiesTest extends TestCase {
(tng) (tng) (tng) JProperties jProperties;
(tng) (tng) (tng) String key = "helloworld.title";
(tng) (tng) (tng) String value = "Hello World!";
(tng) (tng) (tng) public void testLoadProperties() throws Exception {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) String name = null;
(tng) (tng) (tng) (tng) (tng) (tng) (tng) Properties p = new Properties();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) name = "C:\\IDEAP\\Properties4Methods\\src\\com\\kindani\\test\\LocalStrings.properties";
(tng) (tng) (tng) (tng) (tng) (tng) (tng) p = JProperties.loadProperties(name, JProperties.BY_PROPERTIES);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) assertEquals(value, p.getProperty(key));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) name = "com.kindani.test.LocalStrings";
(tng) (tng) (tng) (tng) (tng) (tng) (tng) p = JProperties.loadProperties(name,JProperties.BY_RESOURCEBUNDLE);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) assertEquals(value, p.getProperty(key));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) name = "C:\\IDEAP\\Properties4Methods\\src\\com\\kindani\\test\\LocalStrings.properties";
(tng) (tng) (tng) (tng) (tng) (tng) (tng) p = JProperties.loadProperties(name, JProperties.BY_PROPERTYRESOURCEBUNDLE);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) assertEquals(value, p.getProperty(key));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) assertEquals(value,((JProperties.ResourceBundleAdapter)p).getString(key));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) name = "\\com\\kindani\\test\\LocalStrings.properties";
(tng) (tng) (tng) (tng) (tng) (tng) (tng) p = JProperties.loadProperties(name, JProperties.BY_SYSTEM_CLASSLOADER);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) assertEquals(value, p.getProperty(key));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) name = "\\com\\kindani\\test\\LocalStrings.properties";
(tng) (tng) (tng) (tng) (tng) (tng) (tng) p = JProperties.loadProperties(name, JProperties.BY_CLASSLOADER);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) assertEquals(value, p.getProperty(key));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) name = "test\\LocalStrings.properties";
(tng) (tng) (tng) (tng) (tng) (tng) (tng) p = JProperties.loadProperties(name, JProperties.BY_CLASS);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) assertEquals(value, p.getProperty(key));
(tng) (tng) (tng) }
/*
(tng) (tng) (tng) public void testLoadProperties2() throws Exception {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) ServletContext context = null;
(tng) (tng) (tng) (tng) (tng) (tng) (tng) String path = null;
(tng) (tng) (tng) (tng) (tng) (tng) (tng) Properties p = null;
(tng) (tng) (tng) (tng) (tng) (tng) (tng) path = "/WEB-INF/classes/LocalStrings.properties";
(tng) (tng) (tng) (tng) (tng) (tng) (tng) p = JProperties.loadProperties(context, path);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) assertEquals(value, p.getProperty(key));
(tng) (tng) (tng) }
*/
}
properties文g与JPropertiesTest.java文g相同的目录下
LocalStrings.properties文g
# $Id: LocalStrings.properties,v 1.1 2000/08/17 00:57:52 horwat Exp $
# Default localized resources for example servlets
# This locale is en_US
helloworld.title=Hello World!
requestinfo.title=Request Information Example
requestinfo.label.method=Method:
requestinfo.label.requesturi=Request URI:
requestinfo.label.protocol=Protocol:
requestinfo.label.pathinfo=Path Info:
requestinfo.label.remoteaddr=Remote Address:
requestheader.title=Request Header Example
requestparams.title=Request Parameters Example
requestparams.params-in-req=Parameters in this request:
requestparams.no-params=No Parameters, Please enter some
requestparams.firstname=First Name:
requestparams.lastname=Last Name:
cookies.title=Cookies Example
cookies.cookies=Your browser is sending the following cookies:
cookies.no-cookies=Your browser isn't sending any cookies
cookies.make-cookie=Create a cookie to send to your browser
cookies.name=Name:
cookies.value=Value:
cookies.set=You just sent the following cookie to your browser:
sessions.title=Sessions Example
sessions.id=Session ID:
sessions.created=Created:
sessions.lastaccessed=Last Accessed:
sessions.data=The following data is in your session:
sessions.adddata=Add data to your session
sessions.dataname=Name of Session Attribute:
sessions.datavalue=Value of Session Attribute:
package com.oreilly.hh;
import java.sql.Time;
import java.util.Date;
import net.sf.hibernate.HibernateException;
import com.oreilly.hh.dao.TrackDAO;
import com.oreilly.hh.dao._RootDAO;
/**
* Try creating some data using the Hibernate Synchronizer approach.
*/
public class CreateTest2 {
(tng) (tng) (tng) (tng)public static void main(String[] args) throws HibernateException {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)// Load the configuration file
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)_RootDAO.initialize();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)// Create some sample data
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)TrackDAO dao = new TrackDAO();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Track track = new Track("Russian Trance", "vol2/album610/track02.mp3",
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Time.valueOf("00:03:30"), new Date(), (short)0);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)dao.save(track);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)track = new Track("Video Killed the Radio Star",
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)"vol2/album611/track12.mp3", Time.valueOf("00:03:49"), new Date(),
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)(short)0);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)dao.save(track);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)// We don't even need a track variable, of course:
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)dao.save(new Track("Gravity's Angel", "/vol2/album175/track03.mp3",
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Time.valueOf("00:06:06"), new Date(), (short)0));
(tng) (tng) (tng) (tng)}
}
<property name="hibernate.transaction.factory_class">
(tng) (tng) (tng) (tng) (tng) (tng) net.sf.hibernate.transaction.JTATransactionFactory
</property>
<property name="jta.UserTransaction">
(tng) (tng) (tng) (tng) (tng) (tng) java:comp/UserTransaction
</property>
package com.oreilly.hh;
import java.sql.Time;
import java.util.ListIterator;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import com.oreilly.hh.dao.TrackDAO;
import com.oreilly.hh.dao._RootDAO;
/**
* Use Hibernate Synchronizer's DAOs to run a query
*/
public class QueryTest3 {
(tng) (tng) (tng) (tng)public static void main(String[] args) throws HibernateException {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)// Load the configuration file and get a session
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)_RootDAO.initialize();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Session session = _RootDAO.createSession();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)try {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)// Print the tracks that will fit in five minutes
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Query query = session.getNamedQuery(
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)TrackDAO.QUERY_COM_OREILLY_HH_TRACKS_NO_LONGER_THAN);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)query.setTime("length", Time.valueOf("00:05:00"));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)for (ListIterator iter = query.list().listIterator() ;
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) iter.hasNext() ; ) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Track aTrack = (Track)iter.next();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)System.out.println("Track: \"" + aTrack.getTitle() +
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) "\", " + aTrack.getPlayTime());
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)}
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)} finally {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)// No matter what, close the session
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)session.close();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)}
(tng) (tng) (tng) (tng)}
}
package com.oreilly.hh.dao;
import java.sql.Time;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import com.oreilly.hh.base.BaseTrackDAO;
/**
* This class has been automatically generated by Hibernate Synchronizer.
* For more information or documentation, visit The Hibernate Synchronizer page
* at http://www.binamics.com/hibernatesync or contact Joe Hudson at joe@binamics.com.
*
* This is the object class that relates to the TRACK table.
* Any customizations belong here.
*/
public class TrackDAO extends BaseTrackDAO {
(tng) (tng) (tng) (tng)// Return the tracks that fit within a particular length of time
(tng) (tng) (tng) (tng)public static List getTracksNoLongerThan(Time time)
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)throws HibernateException
(tng) (tng) (tng) (tng){
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Session session = _RootDAO.createSession();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)try {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)// Print the tracks that will fit in five minutes
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Query query = session.getNamedQuery(
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)QUERY_COM_OREILLY_HH_TRACKS_NO_LONGER_THAN);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)query.setTime("length", time);
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)return query.list();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)} finally {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)// No matter what, close the session
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)session.close();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)}
(tng) (tng) (tng) (tng)}
}
(tng) (tng) (tng) (tng)public static void main(String[] args) throws HibernateException {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)// Load the configuration file and get a session
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)_RootDAO.initialize();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)// Print the tracks that fit in five minutes
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)List tracks = TrackDAO.getTracksNoLongerThan(Time.valueOf("00:05:00"));
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)for (ListIterator iter = tracks.listIterator() ;
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) iter.hasNext() ; ) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)Track aTrack = (Track)iter.next();
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)System.out.println("Track: \"" + aTrack.getTitle() +
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) "\", " + aTrack.getPlayTime());
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)}
(tng) (tng) (tng) (tng)}