TryDone
搜索
会员列表
返回首页
IP查询
热门主题
最新主题
WebMUD
会员名称:
登入密码:
自动登入:
登入
注册
忘记密码
启用我的帐号
Using a DataSource (JNDI Datasource HOW-TO) example
论坛首页
»
项目开发
»
Java开发交流
发表人
内容
2007-03-03 01:09:40
文章主题:
Using a DataSource (JNDI Datasource HOW-TO) example
Admin
注册时间: 2006-11-18 20:56:41
文章: 140
来自: 福州
离线
<%@page import="java.sql.*, javax.sql.*, javax.naming.*"%> <html> <head> <title>Using a DataSource</title> </head> <body> <h1>Using a DataSource</h1> <% DataSource ds = null; Connection conn = null; ResultSet result = null; Statement stmt = null; ResultSetMetaData rsmd = null; try{ Context context = new InitialContext(); Context envCtx = (Context) context.lookup("java:comp/env"); ds = (DataSource)envCtx.lookup("jdbc/address"); if (ds != null) { conn = ds.getConnection(); stmt = conn.createStatement(); result = stmt.executeQuery("SELECT * FROM AddressList"); } } catch (SQLException e) { System.out.println("Error occurred " + e); } int columns=0; try { rsmd = result.getMetaData(); columns = rsmd.getColumnCount(); } catch (SQLException e) { System.out.println("Error occurred " + e); } %> <table width="90%" border="1"> <tr> <% // write out the header cells containing the column labels try { for (int i=1; i<=columns; i++) { out.write("<th>" + rsmd.getColumnLabel(i) + "</th>"); } %> </tr> <% // now write out one row for each entry in the database table while (result.next()) { out.write("<tr>"); for (int i=1; i<=columns; i++) { out.write("<td>" + result.getString(i) + "</td>"); } out.write("</tr>"); } // close the connection, resultset, and the statement result.close(); stmt.close(); conn.close(); } // end of the try block catch (SQLException e) { System.out.println("Error " + e); } // ensure everything is closed finally { try { if (stmt != null) stmt.close(); } catch (SQLException e) {} try { if (conn != null) conn.close(); } catch (SQLException e) {} } %> </table> </body> </html>
这篇文章被编辑了 2 次. 最近一次更新是在 2007-04-02 22:55:09
相互帮助,共同进步.
论坛首页
»
项目开发
»
Java开发交流
前往:
选择一个版面
初来乍到
磅磅潭
JForum论坛
Jabber IM
源源不断
Java开发交流
ASP/DOTNET
C/C++
B/S开发
软件工程
Samples
病毒防治
安全相关
IT综合
需求交流
周未动员
MUD 交流
Powered by
JForum 2.1.8
©
JForum Team
ICP:
闽ICP备06052876号
Template:
TryDone