其中的java代码是在一个servlet类中的代码,此方式并没有在web.xml中添加da tomcat server.xml <Context path="/jndi" reloadable="true" docBase="C:/Documents and Settings/songgang/Workspaces/MyEclipse 8.5/jndi/WebRoot" workDir="C:/Documents and Settings/songgang/Workspaces/MyEclipse 8.5/jndi"> <Resource name="jdbc/mysql" type="javax.sql.DataSource" url="jdbc:mysql://127.0.0.1:3306/student" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" maxIdle="3" maxWait="-1" maxActive="50" /> </Context>Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); DataSource ds = (DataSource) envContext.lookup("jdbc/mysql"); conn = ds.getConnection(); String sql = "select * from _student"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { System.out.print(rs.getInt(1)); System.out.print(rs.getString(2)); System.out.println(rs.getInt(3)); } } catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); rs = null; } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); pstmt = null; } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); conn = null; } catch (SQLException e) { e.printStackTrace(); } } } response.sendRedirect(request.getContextPath() + "/index.jsp");