锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
package app;
import app.UserInfo;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class Test
{
聽/**
聽 * @param args
聽 */
聽public static void main(String[] args)
聽{
聽聽// TODO Auto-generated method stub
聽聽try{
聽聽SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
聽聽Session session = sessionFactory.openSession();
聽聽//HQL鏌ヨ
聽聽//List list = session.createQuery("from UserInfo where username = 'ken'").list();
聽聽Iterator iterator = session.createSQLQuery("select * from TESTUSERINFO").list().iterator();
聽聽//System.out.println(((UserInfo)list.get(0)).getPassword());
聽聽for(;iterator.hasNext(); )
聽聽{
聽聽聽Object[] obj=(Object[])iterator.next();
聽聽聽for(int i=0;i<obj.length;i++)
聽聽聽{
聽聽聽聽System.out.println(obj[i].toString());
聽聽聽}
聽聽}
聽聽/*
聽聽UserInfo user = new UserInfo();
聽聽for (ListIterator iterator = list.listIterator(); iterator.hasNext(); ) {
聽聽聽user = (UserInfo)iterator.next();
聽聽聽聽聽 System.out.println("name: " + user.getUsername());
聽聽}*/
聽聽聽聽聽聽聽 /*user.setUserid("4");
聽聽聽聽聽聽聽 user.setUsername("wukerit");
聽聽聽聽聽聽聽 user.setPassword("password");
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 Transaction tx= session.beginTransaction();
聽聽聽聽聽聽聽 session.save(user);
聽聽聽聽聽聽聽 tx.commit();*/
聽聽聽聽聽聽聽 session.close();
聽聽聽聽聽聽聽 sessionFactory.close();
聽聽}
聽聽catch(PropertyNotFoundException err)
聽聽{
聽聽聽err.printStackTrace();
聽聽}
聽聽catch(Exception err)
聽聽{
聽聽聽err.printStackTrace();
聽聽}
聽聽聽聽聽聽聽 //System.out.println("鏂板璩囨枡OK!璜嬪厛鐢∣racle瑙鐪嬬祼鏋滐紒");
聽聽聽 }
}
鍒氬垰鎺ヨЕcontext榪欎釜璇嶅ソ鍍忚繕鏄湪鐪嬨奐2EE鎶鏈唴騫曘嬮偅鏈功鐨勬椂鍊欙紝閭d釜鏃跺欏浜庝笂涓嬫枃鐨勭悊瑙e熀鏈笂灝辨槸0錛屽彧鐭ラ亾榪欎釜璇嶈屽凡銆?/p>
濡備粖瀵逛簬context榪欎釜璇嶏紝鍒欐湁浜嗙鎭嶇劧澶ф偀鐨勬劅瑙夈備腑鏂囩炕璇戣搗鏉ュ氨鏄滀笂涓嬫枃鈥濓紝浠庡瓧闈㈡剰鎬濇垜浠氨鍙互寰堟竻妤氱殑鍘葷悊瑙c?/p>
棣栧厛鈥滀笂涓嬧濊〃鐜扮殑鏄竴縐嶆壙涓婂惎涓嬬殑鍏崇郴錛屾槸涓縐嶄粠鍓嶄竴涓簨鐗╀紶閫掔粰鍚庝竴涓簨鐗╃殑榪欎箞涓笢瑗褲傜敤闈㈠悜瀵硅薄璇█鏉ヨ錛屽氨鏄粠涓涓猳bject浼犻掑埌鍙﹀涓涓猳bject鐨勬秷鎭屽凡錛屽彧涓嶈繃榪欎釜message閲岄潰鍖呭惈浜嗕竴浜涘叕鐢ㄧ殑淇℃伅銆?/p>
鍏舵錛屸滄枃鈥濇剰鍛崇潃榪欐槸涓縐嶄俊鎭紝涓緇勬枃瀛楋紝涓浜涘唴瀹廣備篃灝辨槸璇翠粬鍚湁浜嗗叾浠栦簨鐗╂墍闇瑕佺殑淇℃伅銆?/p>
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
/**
聽* XDoclet-based session bean.聽 The class must be declared
聽* public according to the EJB specification.
聽*
聽* To generate the EJB related files to this EJB:
聽*聽聽- Add Standard EJB module to XDoclet project properties
聽*聽聽- Customize XDoclet configuration for your appserver
聽*聽聽- Run XDoclet
聽*
聽* Below are the xdoclet-related tags needed for this EJB.
聽*
聽* @ejb.bean name="HelloWorld"
聽*聽聽聽聽聽聽聽聽聽聽 display-name="Name for HelloWorld"
聽*聽聽聽聽聽聽聽聽聽聽 description="Description for HelloWorld"
聽*聽聽聽聽聽聽聽聽聽聽 jndi-name="ejb/HelloWorld"
聽*聽聽聽聽聽聽聽聽聽聽 type="Stateless"
聽*聽聽聽聽聽聽聽聽聽聽 view-type="remote"
聽*/
public class HelloWorld implements SessionBean {
聽/** The session context */
聽private SessionContext context;
聽private String message = "helloworld!";
聽public HelloWorld() {
聽聽// TODO Auto-generated constructor stub
聽}
聽public void ejbActivate() throws EJBException, RemoteException {
聽聽// TODO Auto-generated method stub
聽}
聽public void ejbPassivate() throws EJBException, RemoteException {
聽聽// TODO Auto-generated method stub
聽}
聽public void ejbRemove() throws EJBException, RemoteException {
聽聽// TODO Auto-generated method stub
聽}
聽/**
聽 * Set the associated session context. The container calls this method
聽 * after the instance creation.
聽 *
聽 * The enterprise bean instance should store the reference to the context
聽 * object in an instance variable.
聽 *
聽 * This method is called with no transaction context.
聽 *
聽 * @throws EJBException Thrown if method fails due to system-level error.
聽 */
聽public void setSessionContext(SessionContext newContext)
聽聽throws EJBException {
聽聽context = newContext;
聽}
/**
* 鑷繁鏂板垱寤虹殑涓氬姟鏂規硶
**/
聽public String getMessage()throws EJBException
聽{
聽聽return this.message;
聽聽//return new String("helloworld!");
聽}
///////////////////////////////////////////////////////////////////////////////////鍒嗗壊綰?//////////////////////////////////////////////////////////////////////////
/*
聽* Generated by XDoclet - Do not edit!
聽*/
package com.ejb;
/**
聽* Session layer for HelloWorld.
聽* @xdoclet-generated at ${TODAY}
聽* @copyright The XDoclet Team
聽* @author XDoclet
聽* @version ${version}
聽*/
public class HelloWorldSession
聽聽 extends com.ejb.HelloWorld
聽聽 implements javax.ejb.SessionBean
{
聽聽 public void ejbActivate() throws javax.ejb.EJBException, java.rmi.RemoteException
聽聽 {
聽聽聽聽聽 super.ejbActivate();
聽聽 }
聽聽 public void ejbPassivate() throws javax.ejb.EJBException, java.rmi.RemoteException
聽聽 {
聽聽聽聽聽 super.ejbPassivate();
聽聽 }
聽聽 public void setSessionContext(javax.ejb.SessionContext ctx) throws javax.ejb.EJBException
聽聽 {
聽聽聽聽聽 super.setSessionContext(ctx);
聽聽 }
聽聽 public void unsetSessionContext()
聽聽 {
聽聽 }
聽聽 public void ejbRemove() throws javax.ejb.EJBException, java.rmi.RemoteException
聽聽 {
聽聽聽聽聽 super.ejbRemove();
聽聽 }
聽聽 public void ejbCreate() throws javax.ejb.CreateException
聽聽 {
聽聽 }
}
//////////////////////////////////////////////////////////////////////////////鍒嗗壊綰?///////////////////////////////////////////////////////////////////////
/*
聽* Generated by XDoclet - Do not edit!
聽*/
package com.interfaces;
import javax.ejb.EJBException;
import java.rmi.*;
/**
聽* Remote interface for HelloWorld.
聽* @xdoclet-generated at ${TODAY}
聽* @copyright The XDoclet Team
聽* @author XDoclet
聽* @version ${version}
聽*/
public interface HelloWorld
聽聽 extends javax.ejb.EJBObject
{
聽聽 /**
聽聽聽 * An example business method
聽聽聽 * @throws EJBException Thrown if method fails due to system-level error.聽聽聽 */
聽聽 /*public void replaceWithRealBusinessMethod(聽 )
聽聽聽聽聽 throws java.rmi.RemoteException;*/
聽聽 public String getMessage()throws EJBException,RemoteException;
}
//////////////////////////////////////////////////////////////////////////////鍒嗗壊綰?////////////////////////////////////////////////////////////////////////////
/*
聽* Generated by XDoclet - Do not edit!
聽*/
package com.interfaces;
/**
聽* Home interface for HelloWorld.
聽* @xdoclet-generated at ${TODAY}
聽* @copyright The XDoclet Team
聽* @author XDoclet
聽* @version ${version}
聽*/
public interface HelloWorldHome
聽聽 extends javax.ejb.EJBHome
{
聽聽 public static final String COMP_NAME="java:comp/env/ejb/HelloWorld";
聽聽 public static final String JNDI_NAME="ejb/HelloWorld";
聽聽 public com.interfaces.HelloWorld create()
聽聽聽聽聽 throws javax.ejb.CreateException,java.rmi.RemoteException;
}
/////////////////////////////////////////////////////////////////////////////鍒嗗壊綰?////////////////////////////////////////////////////////////////////////////
/*
聽* Generated file - Do not edit!
聽*/
package com.interfaces;
/**
聽* Utility class for HelloWorld.
聽* @xdoclet-generated at ${TODAY}
聽* @copyright The XDoclet Team
聽* @author XDoclet
聽* @version ${version}
聽*/
public class HelloWorldUtil
{
聽聽 /** Cached remote home (EJBHome). Uses lazy loading to obtain its value (loaded by getHome() methods). */
聽聽 private static com.interfaces.HelloWorldHome cachedRemoteHome = null;
聽聽 private static Object lookupHome(java.util.Hashtable environment, String jndiName, Class narrowTo) throws javax.naming.NamingException {
聽聽聽聽聽 // Obtain initial context
聽聽聽聽聽 javax.naming.InitialContext initialContext = new javax.naming.InitialContext(environment);
聽聽聽聽聽 try {
聽聽聽聽聽聽聽聽 Object objRef = initialContext.lookup(jndiName);
聽聽聽聽聽聽聽聽 // only narrow if necessary
聽聽聽聽聽聽聽聽 if (java.rmi.Remote.class.isAssignableFrom(narrowTo))
聽聽聽聽聽聽聽聽聽聽聽 return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo);
聽聽聽聽聽聽聽聽 else
聽聽聽聽聽聽聽聽聽聽聽 return objRef;
聽聽聽聽聽 } finally {
聽聽聽聽聽聽聽聽 initialContext.close();
聽聽聽聽聽 }
聽聽 }
聽聽 // Home interface lookup methods
聽聽 /**
聽聽聽 * Obtain remote home interface from default initial context
聽聽聽 * @return Home interface for HelloWorld. Lookup using COMP_NAME
聽聽聽 */
聽聽 public static com.interfaces.HelloWorldHome getHome() throws javax.naming.NamingException
聽聽 {
聽聽聽聽聽 if (cachedRemoteHome == null) {
聽聽聽聽聽聽聽聽聽聽聽 cachedRemoteHome = (com.interfaces.HelloWorldHome) lookupHome(null, com.interfaces.HelloWorldHome.COMP_NAME, com.interfaces.HelloWorldHome.class);
聽聽聽聽聽 }
聽聽聽聽聽 return cachedRemoteHome;
聽聽 }
聽聽 /**
聽聽聽 * Obtain remote home interface from parameterised initial context
聽聽聽 * @param environment Parameters to use for creating initial context
聽聽聽 * @return Home interface for HelloWorld. Lookup using COMP_NAME
聽聽聽 */
聽聽 public static com.interfaces.HelloWorldHome getHome( java.util.Hashtable environment ) throws javax.naming.NamingException
聽聽 {
聽聽聽聽聽聽 return (com.interfaces.HelloWorldHome) lookupHome(environment, com.interfaces.HelloWorldHome.COMP_NAME, com.interfaces.HelloWorldHome.class);
聽聽 }
聽聽 /** Cached per JVM server IP. */
聽聽 private static String hexServerIP = null;
聽聽 // initialise the secure random instance
聽聽 private static final java.security.SecureRandom seeder = new java.security.SecureRandom();
聽聽 /**
聽聽聽 * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
聽聽聽 * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
聽聽聽 *
聽聽聽 * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
聽聽聽 */
聽聽 public static final String generateGUID(Object o) {
聽聽聽聽聽聽 StringBuffer tmpBuffer = new StringBuffer(16);
聽聽聽聽聽聽 if (hexServerIP == null) {
聽聽聽聽聽聽聽聽聽聽 java.net.InetAddress localInetAddress = null;
聽聽聽聽聽聽聽聽聽聽 try {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 // get the inet address
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 localInetAddress = java.net.InetAddress.getLocalHost();
聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽 catch (java.net.UnknownHostException uhe) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 System.err.println("HelloWorldUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 // todo: find better way to get around this...
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 uhe.printStackTrace();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return null;
聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽 byte serverIP[] = localInetAddress.getAddress();
聽聽聽聽聽聽聽聽聽聽 hexServerIP = hexFormat(getInt(serverIP), 8);
聽聽聽聽聽聽 }
聽聽聽聽聽聽 String hashcode = hexFormat(System.identityHashCode(o), 8);
聽聽聽聽聽聽 tmpBuffer.append(hexServerIP);
聽聽聽聽聽聽 tmpBuffer.append(hashcode);
聽聽聽聽聽聽 long timeNow聽聽聽聽聽 = System.currentTimeMillis();
聽聽聽聽聽聽 int timeLow聽聽聽聽聽聽 = (int)timeNow & 0xFFFFFFFF;
聽聽聽聽聽聽 int node聽聽聽聽聽聽聽聽聽 = seeder.nextInt();
聽聽聽聽聽聽 StringBuffer guid = new StringBuffer(32);
聽聽聽聽聽聽 guid.append(hexFormat(timeLow, 8));
聽聽聽聽聽聽 guid.append(tmpBuffer.toString());
聽聽聽聽聽聽 guid.append(hexFormat(node, 8));
聽聽聽聽聽聽 return guid.toString();
聽聽 }
聽聽 private static int getInt(byte bytes[]) {
聽聽聽聽聽聽 int i = 0;
聽聽聽聽聽聽 int j = 24;
聽聽聽聽聽聽 for (int k = 0; j >= 0; k++) {
聽聽聽聽聽聽聽聽聽聽 int l = bytes[k] & 0xff;
聽聽聽聽聽聽聽聽聽聽 i += l << j;
聽聽聽聽聽聽聽聽聽聽 j -= 8;
聽聽聽聽聽聽 }
聽聽聽聽聽聽 return i;
聽聽 }
聽聽 private static String hexFormat(int i, int j) {
聽聽聽聽聽聽 String s = Integer.toHexString(i);
聽聽聽聽聽聽 return padHex(s, j) + s;
聽聽 }
聽聽 private static String padHex(String s, int i) {
聽聽聽聽聽聽 StringBuffer tmpBuffer = new StringBuffer();
聽聽聽聽聽聽 if (s.length() < i) {
聽聽聽聽聽聽聽聽聽聽 for (int j = 0; j < i - s.length(); j++) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 tmpBuffer.append('0');
聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽 }
聽聽聽聽聽聽 return tmpBuffer.toString();
聽聽 }
}
////////////////////////////////////////////////////////////////////////////////////////鍒嗗壊綰?//////////////////////////////////////////////////////////////////
閰嶇疆鏂囦歡濡備笅錛?br />ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>