標籤:des style blog http io ar color os sp
<?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>
jsp採用資料庫連接池的方法擷取資料庫時間戳記context.xml配置,jsp頁面把時間格式化成自己需要的格式