jsp采用数据库连接池的方法获取数据库时间戳context.xml配置,jsp页面把时间格式化成自己需要的格式
<?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>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。