现在的位置: 首页 > 综合 > 正文

在jsp中进行JNDI数据源调试

2012年08月31日 ⁄ 综合 ⁄ 共 842字 ⁄ 字号 评论关闭

<!-- JNDI TEST -->

<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<%@ page import="javax.naming.Context"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="java.sql.*"%>
<%
Connection connection = null;
try {
    Context initContext = new InitialContext();
    Context envContext  = (Context)initContext.lookup("java:/comp/env");
    DataSource ds = (DataSource)envContext.lookup("jdbc/njs");
    connection = ds.getConnection();
    PreparedStatement ps = connection.prepareStatement("select * from users");
    ResultSet it = ps.executeQuery();
    while (it.next()) {
        out.write(it.getObject(1).toString()+"<br>");
    }
   
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        if (connection!=null) {
            connection.close();
            connection = null;
        }
    } catch (Exception e2) {
        e2.printStackTrace();
    }
}
%>

抱歉!评论已关闭.