2016年6月30日 星期四

Eclipse內創造註解和一般方法

在Eclipse內寫程式時,可以為自創的Function加上適當的註解,也可以自動產生一般方法而免去手動撰寫。

例如先創造一個Bean:






再用滑鼠右鍵選單選擇【Generate Element Commet】和【Generate Getters and Setters】,便會有相對應的結果出現:













在點擊【Generate Getters and Setters】後,會出現要針對哪些變數來自動產生方法:
























註解寫好後,以後只要將滑鼠移動到該程式碼上,便會出現一般我們所常看到的Tip了:

2016年6月29日 星期三

PostgreSQL的更改欄位類型方式

話說PostgreSQL的Console讓我覺得有一些地方不方便,創造欄位後便不可以介面式的移動位置、更改欄位類型等諸多限制,尤其是當我想要二次更改時,就只好刪除重建,但是這樣一來位置又會走掉,所以常常會為了更改一個欄位,就毀掉大半表格又重建(當然,實際上欄位位置不重要,一樣可以使用,但我就是希望看得舒服@@)。

所以搜尋和嘗試了一小段時間,找到了可以用SQL語法變更欄位類型的方式。

例如,在名為"test"的schema內,將一個名為"check_time"的欄位更改為"時間且不含時區"的格式:
ALTER TABLE msoc.test
ALTER COLUMN check_time TYPE timestamp without time zone
USING check_time::timestamp without time zone

例如,在名為"test"的schema內,將一個名為"id"的欄位更改為"字串"的格式:
ALTER TABLE msoc.test
ALTER COLUMN id TYPE varchar
USING id::varchar

主要是參考下列網址內的資訊:
https://www.postgresql.org/docs/9.1/static/sql-altertable.html

TYPE的部分,有時會和在介面內創造時的名稱不一樣,例如在介面內是"character varying",在SQL語法內是"varchar",所以要改變類別時,最好去上列網址內看看想要改變的類別在TYPE處該怎麼寫。

2016年6月27日 星期一

Java的現在時間取法,和延遲用法

首先是我需要使用Java來取得現在時間,Java裡和時間相關的Class有好幾個,看了看網上的資訊後我決定使用java.time來實作。
  1. import java.time.*;
  2.  
  3. public class MainProcess{
  4.  
  5.     public static void main(String[] args)
  6.     {
  7.         LocalDateTime currentDateTime = LocalDateTime.now();
  8.  
  9.         System.out.println( currentDateTime );
  10.     }
  11. }

要注意的是,currentDateTime只是儲存了LocalDateTime.now()所取得的時間值,故一會兒後再使用currentDateTime的話,裡面的時間值可不會自動改變喔。每次要取得最新的"現在時間值"的話,就必須反覆使用LocalDateTime.now()。

LocalDateTime的結果並不包含時區,如果要直接取得時間 + 時區的結果,可以使用ZonedDateTime的Class。
  1. import java.time.*;
  2.  
  3. public class MainProcess{
  4.  
  5.     public static void main(String[] args)
  6.     {
  7.         ZonedDateTime currentDateTime = currentZoneTime.now();
  8.  
  9.         System.out.println( currentZoneTime );
  10.     }
  11. }

如果是想要單純取得時區,可以使用ZonedDateTime下的一些方法,通常可以取得的結果有兩種:時區的區域名稱,和時區的時差數值。
  1. import java.time.*;
  2.  
  3. public class MainProcess{
  4.  
  5.     public static void main(String[] args)
  6.     {
  7.         ZonedDateTime currentDateTime = currentZoneTime.now();
  8.  
  9.         System.out.println( currentZoneTime.getZone() );
  10.         System.out.println( currentZoneTime.getOffset() );
  11.     }
  12. }

然後是如果執行程式時需要延遲一會兒,有Thread.sleep()和TimeUnit.sleep這兩個用法,這邊我選擇TimeUnit.sleep,因為有比較完整的用法,還可分為從TimeUnit.DAYS到TimeUnit.NANOSECONDS各種不同時間單位的使用方式,這邊使用TimeUnit.SECONDS來實作。
  1. import java.time.*;
  2. import java.util.*;
  3. import java.util.concurrent.*;
  4.  
  5. public class MainProcess{
  6.  
  7.     public static void main(String[] args) throws InterruptedException
  8.     {
  9.         for( int i = 0; i < 10; i++ )
  10.         {
  11.             TimeUnit.SECONDS.sleep( 1 );
  12.  
  13.             System.out.println( LocalDateTime.now() );
  14.         }
  15.     }
  16. }

2016年6月16日 星期四

在Eclipse內使用PostgreSQL資料庫

最近工作需要在Eclipse內和PostgreSQL資料庫相連,因此這邊記錄一下使用方式,免得自己以後要用時又得東找西找的。

首先將PostgreSQL的JAR檔匯入到Eclipse內:


接著在Eclipse內撰寫:
  1. import java.sql.*;
  2.  
  3. public class MainProcess {
  4.  
  5.     public static void main(String[] args)
  6.     {
  7.         Connection tempConnection = null;
  8.         Statement tempStatement = null;
  9.         ResultSet tempResultSet = null;
  10.         String tempAction = "";
  11.         String tempSentence = "";
  12.  
  13.         switch( tempAction )
  14.         {
  15.             case "Select":
  16.                 tempSentence = "SELECT * FROM Schemas.Tables WHERE account = 'Kimdick' AND password = '123'";
  17.  
  18.                 break;
  19.             case "Insert":
  20.                 tempSentence = "INSERT INTO Schemas.Tables ( account, password ) VALUES ( 'Kimdick', '123' )";
  21.  
  22.                 break;
  23.             case "Update":
  24.                 tempSentence = "Update Schemas.Tables SET password = '123', email = 'Kimdick@mail' WHERE account = 'Kimdick'";
  25.  
  26.                 break;
  27.             case "Delete":
  28.                 tempSentence = "DELETE FROM Schemas.Tables WHERE account = 'Kimdick'";
  29.  
  30.                 break;
  31.         }
  32.  
  33.         try
  34.         {
  35.             Class.forName( "org.postgresql.Driver" );
  36.             tempConnection = DriverManager.getConnection( "jdbc:postgresql://127.0.0.1:5432/datastream", "postgres", "1234" );
  37.             tempStatement = tempConnection.createStatement();
  38.             tempStatement.executeQuery( tempSentence );
  39.  
  40.             if( tempResultSet != null )
  41.             {
  42.                 tempResultSet.close();
  43.                 tempResultSet = null;
  44.             }
  45.  
  46.             if( tempStatement != null )
  47.             {
  48.                 tempStatement.close();
  49.                 tempStatement = null;
  50.             }
  51.  
  52.             if( tempConnection != null )
  53.             {
  54.                 tempConnection.close();
  55.                 tempConnection = null;
  56.             }
  57.         }
  58.         catch( Exception e )
  59.         {
  60.             e.printStackTrace();
  61.         }
  62.     }
  63. }

  1. DriverManager.getConnection( "jdbc:postgresql://127.0.0.1:5432/datastream", "postgres", "1234" );
這裡是DriverManager.getConnection( "jdbc:postgresql://資料庫IP:Port/Database", 帳號, 密碼 );

2016年6月14日 星期二

C#的拆解字串

在Unity內、使用C#時,有時會需要拆解字串和重組字串,字串的拆解通常都是訂定拆解字元來執行,但有時也會需要訂定拆解字串來執行。
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class MainProcess : MonoBehaviour {
  6.  
  7.     private string data = "ABCDEFGH";
  8.     private string[] splitSeparators = new string[]{ "DEF" };
  9.  
  10.     void Start ()
  11.     {
  12.         Debug.Log( data.split( 'D' )[0] );
  13.         Debug.Log( data.split( 'D' )[1] );
  14.  
  15.         Debug.Log( data.split( splitSeparators, System.StringSplitOptions.None )[0] );
  16.         Debug.Log( data.split( splitSeparators, System.StringSplitOptions.None )[1] );
  17.     }
  18. }

2016年6月13日 星期一

C#的Dictionary之取得資料

Dictionary是類似Hashtable的架構,用Key和Value的方式來相對應。
而要來依序取得裡面的資料時,foreach裡引用的架構和一般有點不同,主要是keyValuePair。
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class MainProcess : MonoBehaviour {
  6.  
  7.     private Dictionary<int, string> tempDictionary = new Dictionary<int, string>();
  8.  
  9.     void Start ()
  10.     {
  11.         tempDictionary.Add( 1, "XXX" );
  12.         tempDictionary.Add( 2, "YYY" );
  13.         tempDictionary.Add( 3, "ZZZ" );
  14.  
  15.         foreach( KeyValuePair<int, string> x in tempDictionary )
  16.         {
  17.             Debug.Log( x.Key + " = " + x.Value );
  18.         }
  19.     }
  20. }

Unity內轉換Direction和EulerAngles為Quaternion

一般在3D間內有三種可表現方向的方式:
Direction:向量方式,例如( 1, 0, 0 )表示面向正左方。
EulerAngles:尤拉角(由拉角、歐拉角)方式,例如( 0, 180, 0 )表示面向正後方。
Qaternion:四元數。

Direction和EulerAngles都很好理解,而Qaternion即使我能理解它的原理,但還是不擅使用。
偏偏有不少關於方向的使用都得是Qaternion型式,所以轉換就很重要。

Direction轉換成Qaternion,可以使用Qaternion.LookRotation:
  1. Qaternion LookRotation( Vector3 forward, Vector3 upwards = Vector3.up );
例如Qaternion rotation = Qaternion.LookRotation( new Vector3( 1, 0, 0 ), Vector3.up );

EulerAngles轉換成Qaternion,可以使用Qaternion.Euler:
  1. Quaternion Euler( float x, float y, float z );
例如Quaternion rotation = Quaternion.Euler( 0, 30, 0 );

這樣當需要使用Quaternion時,我就可以用這些方式去轉換了。

2016年6月8日 星期三

Unity內偵測有否點擊到UGUI作出來的介面

Unity現在可使用方便的UGUI功能作介面,而當要操控2D介面或是作3D物件控制時,都是使用滑鼠左鍵;所以一般邏輯為當滑鼠初次點擊在2D介面上時,就不會啟動3D物件控制功能,因此需要偵測滑鼠是否點擊在任何UGUI作出來的2D介面上。

UGUI在創建時,會自動在【Hierarchy】下產生Canvas』和EventSystem』兩個物件,EventSystem』便是用來處理點擊等事件的存在,所以會使用它來得知是否有滑鼠點擊任何UGUI物件,並可取得所點擊的介面物件。


  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.EventSystems;
  4.  
  5. public class MainProcess : MonoBehaviour {
  6.  
  7.     void Update ()
  8.     {
  9.         if( Input.GetMouseButtonDown( 0 ) )
  10.         {
  11.             if( EventSystem.current.IsPointerOverGameObject() )
  12.             {
  13.                 Debug.Log( "有介面被點擊到!" );
  14.                 Debug.Log( EventSystem.current.currentSelectedGameObject );
  15.             }
  16.         }
  17.     }
  18. }

2016年6月7日 星期二

C#的尋找在List內之Structure資料

在Unity內、使用C#時,創造包含Structure的List,然後用條件尋找的方式。
先在List內找到符合條件的該筆資料Index,再用Index去調出要尋找的該筆資料其他部分:
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class MainProcess : MonoBehaviour {
  6.  
  7.     private List<tempStructure> tempStructureList = new List<tempStructure>();
  8.  
  9.     private struct tempStructure
  10.     {
  11.         public int tempNumber;
  12.         public string tempName;
  13.     }
  14.  
  15.     void Start ()
  16.     {
  17.         tempStructure xxx = new tempStructure();
  18.         tempStructure yyy = new tempStructure();
  19.  
  20.         xxx.tempNumber = 1;
  21.         xxx.tempName = "XXX";
  22.         yyy.tempNumber = 2;
  23.         yyy.tempName = "YYY";
  24.  
  25.         tempStructureList.Add( xxx );
  26.         tempStructureList.Add( yyy );
  27.  
  28.         tempIndex = tempStructureList.FindIndex( z => z.tempName == "XXX" );
  29.  
  30.         Debug.log( tempStructureList[tempIndex].tempNumber );
  31.     }
  32. }

C#的Structure

在Unity內、使用C#時,創造並使用Structure的方式:
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MainProcess : MonoBehaviour {
  5.  
  6.     private struct tempStructure
  7.     {
  8.         public int tempIndex;
  9.         public string tempName;
  10.     }
  11.  
  12.     void Start ()
  13.     {
  14.         tempStructure xxx = new tempStructure();
  15.  
  16.         xxx.tempIndex = 1;
  17.         xxx.tempName = "Name";
  18.  
  19.         Debug.log( xxx.tempIndex  );
  20.         Debug.log( xxx.tempName );
  21.     }
  22. }

這就是Google的Blogger @@

在很久很久以前,有使用過Yahoo的部落格,玩了一下就沒繼續了。

過了很久,現在是個程式工程師的我,常常會在網路上尋找很多資訊。有自己會用到的資訊時,便會把網頁給整個儲存起來,可是東西一多了之後,往往會很不好找。

這時靈光一閃,有看過一些人會把自己使用到的資訊,記錄在網誌上,讓我萌起了這個也想做看看的念頭。

接下來,就看我有沒有恆心來玩囉......