日期:2014-05-16 浏览次数:20382 次
<Context path="/spp" docBase="spp" debug="5" reloadable="true" crossContext="true" > <Resource name="jdbc/mysqlds" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/spms?autoReconnect=true" username="root" password="" maxActive="100" maxIdle="30" maxWait="10000" /> </Context>
<resource-ref> <description>MySQL DataSource</description> <res-ref-name>jdbc/mysqlds</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ page import="java.sql.*" %> <%@ page import="javax.naming.*" %> <%@ page import="javax.sql.DataSource" %> <% try { //初始化Context Context initCtx = new InitialContext(); System.out.println(initCtx); Context envCtx = (Context)initCtx.lookup("java:comp/env"); System.out.println(envCtx); DataSource ds = (DataSource)envCtx.lookup("jdbc/mysqlds"); System.out.println(ds); Connection conn = ds.getConnection(); Statement stmt = conn.createStatement(); //获取服务器端时间,适用于MySQL(还有current_time(),current_date()等) //MS SQLServer为getDate() String strSQL = "select current_timestamp()"; ResultSet rs = stmt.executeQuery(strSQL); rs.next(); Timestamp date = rs.getTimestamp(1); out.print(date.toString()); rs.close(); stmt.close(); conn.close(); }catch(Exception e) { out.print("错误"+e.getMessage()); } %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>test</title> </head> <body> </body> </html>