2021年3月16日 星期二

Tomcat 6 安裝測試DBCP(範例MySql)

 tomcat 簡易使用Database connection pool需三個步驟

一設定context.xml

二設定web.xml

三改寫Servlet或jsp

(附註:需視事先將jdbc drive的jar檔,放於tomcat/lib目錄下,讓tomcat容器可事先載入)

一設定context.xml (以下內容)

<?xml version='1.0' encoding='utf-8'?>
<Context>

    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Resource name="jdbc/TestMySqlDB" auth="Container" type="javax.sql.DataSource"
               <!-- maxActive="100" maxIdle="30" maxWait="10000" --> <!-- dbcp1 used -->
               maxTotal="100" maxIdle="30" maxWaitMillis="10000" <!-- dbcp2 used -->

               username="root" password="a123456B+" driverClassName="com.mysql.cj.jdbc.Driver"
               url="jdbc:mysql://192.168.1.104:3306/sakila"/>
</Context>

二設定web.xml (以下內容)

 <?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

    <description>
      Project1
    </description>
    <display-name>Project1</display-name>
    <servlet>
        <servlet-name>firstServlet</servlet-name>
    <servlet-class>ServletTestDBCP</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>firstServlet</servlet-name>
    <url-pattern>/firstWebPag.do</url-pattern>
    </servlet-mapping>
    <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestMySqlDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
     </resource-ref>
</web-app>

三改寫Servlet或jsp(以下內容) 

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.sql.*;

public class ServletTestDBCP extends HttpServlet{
    public void doGet(HttpServletRequest request,HttpServletResponse response)
     throws IOException{
        PrintWriter out=response.getWriter();
        java.util.Date date=new java.util.Date();
        out.println(date);
        // Look up our data source
          try {
               Connection con = null;
            // Obtain our environment naming context
               Context initCtx = new InitialContext();
               Context envCtx = (Context) initCtx.lookup("java:comp/env");
               DataSource ds = (DataSource)envCtx.lookup("jdbc/TestMySqlDB");
               try{
                   con = ds.getConnection();
                if (con!=null){
                    System.out.println("DB CONNECTION IS Success!");
                }else{
                    System.out.println("DB CONNECTION IS Fail!");
                }
            }finally{
                if(con!=null){
                    con.close();
                }
            }
   
          } catch (NamingException | SQLException e) {
               e.printStackTrace();
          }
    }

}

2021年1月30日 星期六

2020年12月17日 星期四

Python List Comprehension

  List Comprehension沒有統一譯名(數學推論出的式子)

#列表推導式

# 列表高效率單行建構
arr1 = [i for i in range(10)]

# 列表內元素限制產生
arr2 = [x for x in arr1 if x % 2 == 0]

# 列表內元素多重限制產生
arr3 = [x for x in arr1 if x >= 3 and x % 2]

# 巢狀列表元素產生
arr4 = [(x, y) for x in range(3) for y in range(4)]

print(arr1)
print(arr2)
print(arr3)
print(arr4)

2020年12月9日 星期三

SQL 一次insert 多筆

 -- 傳統

 insert into test.customer(pin,first_name,last_name) values(601,"Bush","Kim");

 insert into test.customer(pin,first_name,last_name) values(602,"Grace","Kun");

 insert into test.customer(pin,first_name,last_name) values(603,"Jully","LAin");


-- oracle 

insert all 

into test.customer(pin,first_name,last_name) values(601,"Bush","Kim")

into test.customer(pin,first_name,last_name) values(602,"Grace","Kun")

into test.customer(pin,first_name,last_name) values(603,"Jully","LAin");


-- mysql

insert into customer(pin,first_name,last_name) 

values(601,"Bush","Kim"),

(602,"Grace","Kun"),

(603,"Jully","LAin");

2020年11月4日 星期三

 Python 的日期字串格式摘要:

Python's strftime directives

CodeMeaningExample
%a Weekday as locale’s abbreviated name. Mon
%A Weekday as locale’s full name. Monday
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 1
%d Day of the month as a zero-padded decimal number. 30
%-d Day of the month as a decimal number. (Platform specific) 30
%b Month as locale’s abbreviated name. Sep
%B Month as locale’s full name. September
%m Month as a zero-padded decimal number. 09
%-m Month as a decimal number. (Platform specific) 9
%y Year without century as a zero-padded decimal number. 13
%Y Year with century as a decimal number. 2013
%H Hour (24-hour clock) as a zero-padded decimal number. 07
%-H Hour (24-hour clock) as a decimal number. (Platform specific) 7
%I Hour (12-hour clock) as a zero-padded decimal number. 07
%-I Hour (12-hour clock) as a decimal number. (Platform specific) 7
%p Locale’s equivalent of either AM or PM. AM
%M Minute as a zero-padded decimal number. 06
%-M Minute as a decimal number. (Platform specific) 6
%S Second as a zero-padded decimal number. 05
%-S Second as a decimal number. (Platform specific) 5
%f Microsecond as a decimal number, zero-padded on the left. 000000
%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).
%Z Time zone name (empty string if the object is naive).
%j Day of the year as a zero-padded decimal number. 273
%-j Day of the year as a decimal number. (Platform specific) 273
%U Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. 39
%W Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. 39
%c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013
%x Locale’s appropriate date representation. 09/30/13
%X Locale’s appropriate time representation. 07:06:05
%%A literal '%' character.%

2020年10月16日 星期五

Python令人混淆的底線命名

 _name:

class 中的成員變數的變數名稱前有一底線,代表預計標示為是class內部用(非強制性)

module內定義的方法名稱前有一底線,代表不公開,需使用FQN可使用
      也可改寫__all__()表示出外界使用
      還有如果直接讓module於python的shell執行也會顯示使用

name_:變數名稱後有一底線,只是為解決關鍵字衝突

_ _name:

class 中的成員變數的變數名稱前有二底線,代表會在前加上一底線及一class名稱改寫,呈現給外部使用
      但class內部使用依舊使用無改寫原名稱


反向植入成員變數,如命名規則一底線及一class名稱再加二底線的變數名,例"_ClassName_ _VarName"
後來出現這Class名稱的class定義,雖內部無此變數,但會自動加入
_ConfuseClass__confuseVar1=45
   
class ConfuseClass:
    def testFun(self):
         return __confuseVar1

ConfuseClass().testFun()

_ _name_ _:
   代表python內class的特殊成員(不好的用法)如__init__()

_ :在python command shell 中代表最後一個物件,可能是一個值或list等

2019年2月13日 星期三

Java 8 後新觀念理解順序:
1.Type Inference:
    https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html

2.Local variable type inference:
    https://codertw.com/%E4%BC%BA%E6%9C%8D%E5%99%A8/145426/

3.Generic Types:
   https://docs.oracle.com/javase/tutorial/java/generics/why.html