会话bean中连接数据库的方法

public class CommonManage implements SessionBean {

private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;


public HashMap getComponent(String courseId){
  HashMap hm=new HashMap();
  try{
    getResultSet("select * from bupt_course where kcid='"+courseId+"'");
   if(rs!=null) {
     rs.next();
   hm.put("c1",rs.getString("isallow"));
   hm.put("c2",rs.getString("iscourseware"));
   hm.put("c3",rs.getString("isdataware"));
   hm.put("c4",rs.getString("isevalue"));
   hm.put("c5",rs.getString("isexercise"));
   hm.put("c6",rs.getString("ispub"));
   hm.put("c7",rs.getString("isquestion"));
   hm.put("c8",rs.getString("istalk"));
   hm.put("c9",rs.getString("istest"));

   }
    close();
}
catch(SQLException e) {
  e.printStackTrace();
   }
catch(Exception e) {
 e.printStackTrace() ;
  }
finally{
  return hm;
}

private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = null;
String password = null;
Properties properties = null;
try {
  properties = new Properties();
  properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
  properties.put(Context.PROVIDER_URL, url);
  if (user != null) {
    properties.put(Context.SECURITY_PRINCIPAL, user);
    properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
  }

  return new InitialContext(properties);
}
catch(Exception e) {
  throw e;
}
}


public boolean isExistWare(String warename,String courseId)
 {
   try{
     //get naming context
       Context ctx = getInitialContext();
      //look up jndi name
       Object ref = ctx.lookup("Courseware");
       //cast to Home interface
       CoursewarePK warePK=new CoursewarePK(courseId,warename);
       CoursewareHome wareHome = (CoursewareHome) PortableRemoteObject.narrow(ref, CoursewareHome.class);
       Courseware ware=wareHome.findByPrimaryKey(warePK);

       return true;
   }
   catch(Exception e) {
//    e.printStackTrace() ;
//     System.out.println("Primary key does not exist");
    return false;
   }
 }

private void close()
{
    try
    {
         if(rs!=null) rs.close();
         if(stmt!=null) stmt.close();
         if(conn!=null) conn.close();
    }
    catch(Exception e)
    {
      System.err.println(e.toString());
    }
}


private void getConnection() throws SQLException{

try{
  //get naming context
    Context ctx = getInitialContext();
   //look up jndi name
    javax.sql.DataSource ds=(javax.sql.DataSource) ctx.lookup("DataSource") ;
    conn=ds.getConnection() ;
}
catch(Exception e) {
 e.printStackTrace() ;
}
}
private void getResultSet(String sql) throws SQLException{

 try{
  getConnection();
  stmt=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE);
  rs=stmt.executeQuery(sql);
 }
 catch(SQLException e)
 {
   e.printStackTrace() ;
 }
 catch(Exception e) {
   e.printStackTrace() ;
 }
}
private void executeUpdate(String sql)throws SQLException
{

        try
        {
          getConnection();
          stmt=conn.createStatement();
          stmt.executeUpdate(sql);
        }
        catch(SQLException e)
        {
                System.err.println("excecuteUpdate:"+e.getMessage());
        }
}
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值