總網頁瀏覽量

2017年2月2日 星期四

[JAVA] Calendar 類別

Oracle 文章寫這樣

Returns the value of the given calendar field. In lenient mode, all calendar fields are normalized. In non-lenient mode, all calendar fields are validated and this method throws an exception if any calendar fields have out-of-range values. The normalization and validation are handled by the complete() method, which process is calendar system dependent.

完全看不懂

所以自己測


import java.util.Calendar;

public class dataaaaa{
    public static void main(String args[]){
        Calendar ca = Calendar.getInstance();
        for(int i = 0 ; i <= 10 ; i++)
            System.out.println(i+": "+ca.get(i));
    }
}


output :

0: 1
1: 2017
2: 1
3: 5
4: 1
5: 3
6: 34
7: 6
8: 1
9: 0
10: 11


看不懂 ? 正常

ID  Value  Description
--  -----  -----------
 0      1  Era (BC/AD for Gregorian).
 1   2017  Year.年
 2      1  Month (zero-based).月(以0開始算)
 3      5  Week-of-year.(今年第幾個禮拜)
 4      1  Week-of-month.(這個月第個禮拜)
 5      3  Date/day-of-month.(這個月第幾天)
 6     34  Day-of-year.(今年第幾天)
 7      6  Day-of-week.(這禮拜第幾天)
 8      1  Day-of-week-in-month.(今天是這個月的第幾個禮拜)
 9      0  AM/PM selector.(上午0/下午1)
10     11  Hour.(幾點 時部分)
 
 
[1] https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#get(int) 
[2] http://stackoverflow.com/questions/6605471/what-does-object-calendar-do 

2017年1月24日 星期二

[JAVA] 與phpmyadmin建立連線參數

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;

public class ConnectiontoDB{
    static final String DRIVER = "com.mysql.jdbc.Driver";
    static final String DATABASE_URL =
             "jdbc:mysql://[主機位置]:3306/[資料庫名稱]?autoReconnect=true&useSSL=false";
   
    public static void main(String args[]){
        Connection connection = null;       
        try{
    Class.forName(DRIVER);
        }catch(ClassNotFoundException e){
    System.out.println("DRIVER ERROR: "+e);}
       
        try{           
            connection = DriverManager.getConnection(DATABASE_URL,"[帳號]","[密碼]");
        }catch(Exception e){
    System.out.println("forName: "+e);
  }
    }
}

加上藍色這段主要是因為不佳的時候會出現錯誤訊息
Tue Jan 24 23:17:26 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

雖然是版本問題, 但是如果改成useSSL=false就像是跳過SSL連線依樣, 其實蠻糟糕的,
所以使用的時候還是記得用useSSL= true 安全一點.

至於現在用兩個try-catch主要是讓我更知道錯誤的訊息在哪出現,
雖然蠻多餘的  ㄎㄎ. 不過這樣就知道是DRIVER錯誤還是連線錯,

雖然這次的錯誤訊息是版本問題的錯誤....

[JAVA] String 字串比對

在JAVA的字串比對常用的幾個方法,
這裡不討論記憶體比對啥啥啥的問題,
單純的函式來比較.


equalsIgnoreCase


public boolean equalsIgnoreCase(String anotherString)

與anotherString比較字串是否相同,
equalsIgnoreCase會針對長度與每個字元比較, 比較3種情況
1. 利用 " == ", 比較字元
2. 利用 Character.toUpperCase(char) 判斷
3. 利用 Character.toLowerCase(char) 判斷

回傳: true 非null, 且以上判斷結果都相同; 否則回傳 false.

public class string_equalsIgnoreCase{
    public static void main(String args[]){
        String a = "abcdefg";
        String b = "ABCDEFG";
        String c = "AbCdEfG";
        String d = "abc";
        String e = "ABC";
        String f = "AbC";
        String g = "";
    
                               Output:
        System.out.println(a.equalsIgnoreCase(a));    true
        System.out.println(a.equalsIgnoreCase(b));    true
        System.out.println(a.equalsIgnoreCase(c));    true
        System.out.println(a.equalsIgnoreCase(d));    false
        System.out.println(a.equalsIgnoreCase(e));    false
        System.out.println(a.equalsIgnoreCase(f));     false
        System.out.println(a.equalsIgnoreCase(g));    false
    }
}


...以要改用SyntaxHighlighter來顯示 不然這樣看得好累

equals


public boolean equals(Object anObject)

與anObject比較字串是否相同,

回傳: true 非null, 且字串與anObject相同; 否則回傳 false.

public class string_equals{
    public static void main(String args[]){
        String a = "abcdefg";
        String b = "ABCDEFG";
        String c = "AbCdEfG";
        String d = "abc";
        String e = "ABC";
        String f = "AbC";
        String g = "";
        Object y = "abcdefg";
        Object z = "ABCDEFG";
  
                          Output:       
        System.out.println(a.equals(a));    true     
        System.out.println(a.equals(b));    false
        System.out.println(a.equals(c));    false
        System.out.println(a.equals(d));    false
        System.out.println(a.equals(e));    false
        System.out.println(a.equals(f));     false
        System.out.println(a.equals(g));    false
        System.out.println(a.equals(y));    true
        System.out.println(a.equals(z));    false
    }
}

compareTo

public int compareTo(String anotherString)

利用字典序 Lexicographic order 做排列, 
基本上就是利用String 裡面的每個字元的 Unicode value 做為比較依據.
如果字串比anotherString還要前面, 輸出就會出現負值. 
如果字串比anotherString後面, 竟會出現正值.
如果字串都依樣, 就會出現 0.
其中出現0的前提是, compareTo會利用equals做前提比較, equals也回傳0.









其實字串比對還回牽扯到記憶體位置等等的, 那個懶得去想了. ㄎㄎ

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object)







2016年12月28日 星期三

[JAVA] 壓縮檔範例 001 zip

壓縮檔範例 001 [1][2]
import java.io.*;
import java.util.zip.*;

public class ziptest_001{ 
    public static void main(String[] args){ 
        try{ 
            BufferedReader in= new BufferedReader(
                                            new InputStreamReader(
                                            new FileInputStream(要加入壓縮檔的檔案),讀取檔案編碼方式));
            //範例中用IOS8859_1中文編碼的方式讀取要壓縮檔的檔案
            FileOutputStream f = new FileOutputStream(檔案名稱 . zip); 
            CheckedOutputStream ch=new CheckedOutputStream(f,new CRC32());
            //校驗和可用於驗證輸出數據的完整性. 利用計算數據流了CRC32().
可用於計算數據流的 CRC-32 的類
需要維護寫入數據校驗和的輸出流。校驗和可用於驗證輸出數據的完整性。
原文網址:https://read01.com/o28E4.html
            ZipOutputStream out = new ZipOutputStream ( new BufferedOutputStream(ch)); 
需要維護寫入數據校驗和的輸出流。校驗和可用於驗證輸出數據的完整性。
原文網址:https://read01.com/o28E4.html
需要維護寫入數據校驗和的輸出流。校驗和可用於驗證輸出數據的完整性。
原文網址:https://read01.com/o28E4.html

            int c; 
            out.putNextEntry(new ZipEntry(要加入壓縮檔的檔案));
            // 指定名稱創建, 並開始輸入壓縮檔
            while((c=in.read())!=-1) 
                out.write(c); 
            in.close(); 
            out.close(); 
        }catch(Exception e){ 
            e.printStackTrace(); 
        } 
    } 
}

會在與執行檔同個目錄底下看到所新增的zip檔


[1] http://big5.webasp.net/article/7/6873.htm
[2] https://read01.com/o28E4.html

2016年11月21日 星期一

[C++] POCO 安裝步驟

POCO C++ Libraries: Overview 官方網站
https://pocoproject.org/

只打POCO可能會跑到其他地方

Get Started Download the latest release

POCOPRO C++ Frameworks
Additional libraries, frameworks and tools based on POCO by Applied Informatics
Download Free Evaluation Version
選擇要安裝的作業系統版本



載好後就會看到.exe檔了



點選 I accept the terms of the License Agreement
[Next]