<急求!!!>关于Hibernate

lhy305 2006-04-22 10:15:53
我的BusinessService.java在编译的时候为什么找不到net包
代码如下:
ackage mypack;
import javax.servlet.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import java.io.*;
import java.util.*;

public class BusinessService
{
public static SessionFactory sessionFactory;

/**初始化Hibernate,创建SessionFactory实例*/
static
{
try
{
//根据默认位置的Hibernate配置文件的信息,创建一个Configuration实例
Configuration config=new Configuration();
config.addClass(User.class);

//创建SessionFactory实例*/
sessionFactory=config.buildSessionFactory();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**查询所需要的User 对象,然后调用printUser()方法打印User对象信息*/
public void findAllUser(ServletContext context,OutputStream out) throws Exception
{

// Ask for a session using the JDBC information we've configured
Session session = sessionFactory.openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
List user=session.find("from User as u order by u.name asc");
for (Iterator it =user.iterator(); it.hasNext();)
{
printUser(context,out,(User) it.next());
}
// We're done; make our changes permanent
tx.commit();
}
catch (Exception e)
{
if (tx != null)
{
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
}
finally
{
// No matter what, close the session
session.close();
}
}
/**持久化一个User对象*/
public void saveUser(User user) throws Exception
{
// Ask for a session using the JDBC information we've configured
Session session = sessionFactory.openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
session.save(user);
// We're done; make our changes permanent
tx.commit();
}
catch (Exception e)
{
if (tx != null)
{
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
}
finally
{
// No matter what, close the session
session.close();
}
}
/**按照OID加载一个User对象,然后修改它的属性*/
public void loadAndUpdateUser(String username) throws Exception
{
// Ask for a session using the JDBC information we've configured
Session session = sessionFactory.openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();

User c=(User)session.load(User.class,username);
c.setUsername(username);
// We're done; make our changes permanent
tx.commit();

}
catch (Exception e)
{
if (tx != null)
{
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
}
finally
{
// No matter what, close the session
session.close();
}
}
/**把User对象的信息输出到动态网页*/
private void printUser(ServletContext context,ServletOutputStream out,User user)throws Exception
{
out.println("------以下是"+user.getName()+"的个人信息------"+"<br>");
out.println("NAME: "+user.getId()+"<br>");
out.println("PASSWORD: "+user.getPassword()+"<br>");
String sex=user.getSex()=='M'? "男":"女";
out.println("SEX: "+sex+"<br>");
out.println("E-Mail: "+user.getEmail()+"<br>");
}
public void test(ServletContext context,OutputStream out) throws Exception{
User user=new User();
user.setName("brad");
user.setEmail("lihuiyang305@yahoo.com.cn");
user.setPassword("123");
user.setSex('M');

saveUser(user);
findAllUser(context,out);

loadAndUpdateUser(user.getUsername(),"lihuiyang");
findAllUser(context,out);
}
public static void main(String args[]) throws Exception
{
new BusinessService().test(null,System.out);
sessionFactory.close();
}
}
...全文
199 回复 点赞 打赏 收藏 举报
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
Web 开发
加入

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2006-04-22 10:15
社区公告
暂无公告