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());
}
}
}