为什么在Juilder中不能直接运行?

04-05-07 skyyjl
我已经设置了datasource,在web层运行就能获得连接显示数据,而jbuilder环境下在main方法中直接运行就获不到连接呢?

public class DBConnectionManager {

private static DBConnectionManager dbc = null;

public static synchronized DBConnectionManager getInstance() {

if (dbc == null) {

dbc = new DBConnectionManager();

}

return dbc;

}

/**

* 获取上下文

* @throws NamingException

* @return Context

*/

private Context getInitialContext() throws NamingException {

Hashtable environment = new Hashtable();

environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

environment.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

environment.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");

return new InitialContext(environment);

}

/**.

* 获取连接

*/

public Connection getConnection(String dataSourceName) {

DataSource ds = null;

Connection conn = null;

try {

InitialContext ctx = getInitialContext();

ds = (DataSource) ctx.lookup(dataSourceName);

return conn;

}

catch (Exception ex) {

}

return null;

}

public ResultSet rs_row(){

Connection conn = DBConnectionManager.getInstance().getConnection("java:/ntsky");

System.out.println("conn = " + conn);

String sql = "select * from t_user";

ResultSet rs = null;

try {

Statement stmt = conn.createStatement();

rs = stmt.executeQuery(sql);

return rs;

}

catch (Exception ex) {

System.out.println("ex"+ex.getMessage());

}

finally{

try {

conn.close();

}

catch (Exception e) {

// logger.error("DB conn 关闭数据库连接错误 " + e.getMessage());

}

}

return null;

}

public static void main(String[] args) {

DBConnectionManager dbcm = new DBConnectionManager();

ResultSet rs = dbcm.rs_row();

try{

System.out.println("1 : " + rs.getString(1));

}

catch(Exception e){

System.out.println("main "+e.getMessage());

}

}

}

newold
2004-05-08 09:49
你这样是从application server取连接,应该不能在外部取的

猜你喜欢