Weblogic 连接池的问题,郁闷了好久,各位有空来帮小弟看看!

jzit510 2005-08-06 12:44:32
下面的Servlet为什么不能从数据库里读出数据呢?ConnectPool和DataSource都配置好了,测试没有问题。此程序运行只显示:myJDBCReadServlet,也就是说此句
out.println("<h1>myJDBCReadServlet</h1>"); 后面的程序都没有执行。·##¥#¥,请各位指教!

import java.io.*;
import java.sql.*;
import javax.servlet.http.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class myJDBCReadServlet extends HttpServlet {

public void init() {

try {
ctx = new InitialContext();

/* Create a new DataSource by Locating It in the Naming Service:
*/
ds = (javax.sql.DataSource)
ctx.lookup ("MyJNDI");

} catch (Exception E) {
/*
Handle exception here.
*/
System.out.println("Init Error: " + E);
}
}


public void service(HttpServletRequest requ,
HttpServletResponse resp)
throws IOException

{
Connection myConn = null;

try {

PrintWriter out = resp.getWriter();

out.println("<html>");
out.println("<head><title>myJDBCReadServlet</title></head>");
out.println("<body>");

out.println("<h1>myJDBCReadServlet</h1>");

/* Get a new JDBC connection from the DataSource:
*/
myConn = ds.getConnection();

/* Create an Instance of the java.sql.Statement class
and use the factory method called createStatement()
available in the Connection class to create a new statement.
*/
stmt = myConn.createStatement();

/* Use the shortcut method the available in the Statement
class to execute our query. We are selecting all rows
from the EMPLOYEE table.
*/
rs = stmt.executeQuery("SELECT * FROM myuser ");

/* This enumerates all of the rows in the ResultSet and
prints out the values at the columns named ID, NAME,
LOCATION.
*/
while (rs.next()) {
out.println(rs.getInt("user_id") + "- " +
rs.getString("user_name") + "- " +
rs.getString("tel") + "<p>");
}

/* Release the ResultSet and Statement.
*/
rs.close();
stmt.close();

} catch (Exception E) {
/*
Handle exception here.
*/
System.out.println("Service Error: " + E);
} finally {
if (rs != null) {
try { rs.close(); } catch (Exception ignore) {};
}
if (stmt != null) {
try { stmt.close(); } catch (Exception ignore) {};
}
if (myConn != null) {
try { myConn.close(); } catch (Exception ignore) {};
}
}
}

/*
* Local Variables
*/
Context ctx;
DataSource ds;
Statement stmt;
ResultSet rs;

}


...全文
227 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
老無所依 2006-03-18
  • 打赏
  • 举报
回复
在weblogic中访问JNDI需要BEA的T3协议指定服务器地址,还要将weblogi的一些信息注册到环境中:代码如下:

public class testPool {
public testPool() {
}

public static void main(String[] args) throws NamingException, SQLException {
String url = "t3://java:7001";//你的服务器地址
Context ctx = null;
javax.sql.DataSource ds = null;
java.sql.Connection conn = null;
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
Properties properties = null;
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);

try {
ctx = new InitialContext(properties);
ds = (DataSource) ctx.lookup("test");

conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from addressbook_table");
if (!rs.next()) {
System.out.println("There is any data");

}
System.out.println("--------------");
while (rs.next()) {

System.out.println(rs.getString(1) + "|" + rs.getString(2) + "|" +
rs.getString(3) + "|" + rs.getString(4) + "|");
System.out.println("------------");
}

}
catch (Exception e) {
System.out.println(e.getStackTrace());
}
finally {
try {
if (ctx != null) {
ctx.close();
}
}
catch (Exception e) {}
try {
if (stmt != null) {
stmt.close();
}
}
catch (Exception e) {}
try {
if (rs != null) {
rs.close();
}
}
catch (Exception e) {}
try {
if (conn != null) {
conn.close();
}
}
catch (Exception e) {}
}

}

}
coding叶青 2006-03-10
  • 打赏
  • 举报
回复
try {
ctx = new InitialContext();

/* Create a new DataSource by Locating It in the Naming Service:
*/
ds = (javax.sql.DataSource)
ctx.lookup ("MyJNDI");

}
应该是这里的代码不对,没有正确执行,看看你的类环境应该错了,或者少东西
MAXIMO51 2006-02-15
  • 打赏
  • 举报
回复
gz

1,220

社区成员

发帖
与我相关
我的任务
社区描述
企业软件 中间件技术
社区管理员
  • 中间件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧