<?xml version="1.0" encoding="UTF-8"?> <!-- 数据库连接池配置文件 --> <Context> <Resource name="jdbc/ConnectionPool" auth="Application" type="javax.sql.DataSource" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/pwapplication?relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull" maxActive="8" maxIdle="4" /> </Context>
然后在JSP页面添加代码,获取时间后,需要将时间格式化成为自己需要的格式。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <%@ page import="javax.naming.*" %> <%@ page import="javax.sql.DataSource,java.text.Format,java.text.DateFormat" %> <%@ page import="java.text.SimpleDateFormat" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘MyJsp.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <% Context initCtx = new InitialContext(); Context ctx = (Context) initCtx.lookup("java:comp/env"); //获取连接池对象 DataSource ds =(DataSource)ctx.lookup("jdbc/ConnectionPool"); //创建连接 Connection conn = ds.getConnection(); System.out.println("conn="+conn); Statement stmt = conn.createStatement(); String mysql="select * from notification_tb"; ResultSet myrs=stmt.executeQuery(mysql); //执行查询 %> <body> 通知消息列表: <% while(myrs.next()) { %> <% String notiID=myrs.getString("notiID"); String notiTheme=myrs.getString("notiTheme"); String notiContent=myrs.getString("notiContent"); String notifier=myrs.getString("notifier"); Timestamp notiTime=myrs.getTimestamp("notiTime"); //设置时间格式 SimpleDateFormat spf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); //格式化时间 String fmtTime=spf.format(notiTime); %> <% System.out.println(notiTheme); System.out.println(notiContent); System.out.println(notiTime); System.out.println(fmtTime); %> <dt>>><a href="notidetail?notiID=<%=notiID %>"><%=notiTheme %></a></dt> <dd> <div align="right">发布人:<%= notifier%> </div> </dd> <% } %> </body> </html>
时间: 2024-10-27 06:09:56