2013年7月23日 星期二

[Servlet/JSP] 取得連接 Eclipse.Tomcat <-> SQL server *使用DataSource

請拋棄DataManager.getConnection (... , ... , ...)
查詢 Doc.  ->  JNDI Datasource HOW-TO
<DataSource  設定>
1.將JDBC Driver 加入Tomcat
     > sqljdbc4.jar 加入Tomcat安裝目錄\lib
     * 如果使用Tomcat的 launch configuration ,在Java Resource -> Libraries ->Apache Tomcat v7.0 中看不到sqljdbc4.jar

2.在Tomcat裡設定DataSource 
     > WebContent -> META-INF -> 新增 context.xml   
     (可以在三個位置新增context.xml 。 補充:另一種方法)
     (context內容可以在文件中可以找到)


<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Resource  name="jdbc/MyDB" auth="Container" 
             type="javax.sql.DataSource"
             maxActive="100" maxIdle="30" maxWait="10000"
             username="sa" password="passw0rd" 
             driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
             url="jdbc:sqlserver://localhost:1433;database=JDBC"/>
</Context>

PS.若使用文件中的格式,需要改:
name >常取”jdbc/XXX”的格式;
type >”javax.sql.DataSource”;
username >資料庫用戶帳號
password >資料庫用戶密碼
driveClassName >"com.microsoft.sqlserver.jdbc.SQLServerDriver"
*資料庫驅動 可從
Java Resource -> Libraries ->Apache Tomcat v7.0 ->sqljdbc4.jar -> com.microsoft.sqlserver -> jdbc.SQLServerDriver.class
按 右鍵 -> Copy Qualified Name ->貼到driveClassName (記得刪掉 .class)
url >"jdbc:sqlserver://localhost:1433;database=JDBC"
*google : "sql server url"
---補充---
maxIdle: 資料庫連接的最大閒置時間。超過閒置時間,資料庫連接將被標記为不可用,然後被釋放。設為0表示無限制。
maxActive: Connection Pool 的最大資料庫連接數。設為0表示無限制。
maxWait: 最大建立連接等待時間。如果超過此時間將接到異常。設為-1表示無限制。

3.將web.xml 設定DataSource (此處的 web.xml 是在WebContent\WEB-INF中)

<resource-ref>
  <res-ref-name>jdbc/MyDB</res-ref-name> 
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
  <res-sharing-scope>Shareable</res-sharing-scope></resource-ref>

* <res-ref-name>、<res-type>、 <res-auth> 同 context.xml 中 name、type、auth

* <res-sharing-scope> 只有 Shareable , Unshareable 兩種選項(請使用alt+/選擇)

what it exactly means to have <res-sharing-scope> ?

The Bean Provider can control the shareability of the connections acquired from the resource manager connection factory. By default, connections to a resource manager are shareable across other enterprise beans in the application that use the same resource in the same transaction context. The Bean Provider can specify that connections obtained from a resource manager connection factory reference are not shareable by specifying the value of the "res-sharing-scope" deployment descriptor element to be "Unshareable". The sharing the connections to a resource manager allows the container to optimize the use of connections and enables the container's use of local transactions optimizations.

4.寫程式測試DataSource (test.jsp)

-test.jsp-
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="java.sql.*" %>
<%
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MyDB");
Connection conn = ds.getConnection();
//JDBC
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select * from dept");
while(rset.next()) {
        String col1 = rset.getString(1);
        out.println(col1+"<Br>");
}
rset.close();
stmt.close();
conn.close();
%>
</body>
</html>

---------
*lookup("java:comp/env/jdbc/MyDB") 底線部分記得要跟web.xml中 res-ref-name 一樣

2013年6月7日 星期五

Thread 02 基本使用重點

延續上一篇 <Thread 基本建構方法>

使用thread請不要override start() method , 執行時請用start();

創建一個Thread 如果有使用
Thread t1 = new Thread(xxx);
或是
Thread t1 = new Thread(){public void run(){...}};
使用下列程式碼,即可即可run執行緒
t1.start();
...但是如果使用
t1.run();
會發生甚麼事情呢....?

只能說這絕對不是你想要使用的執行緒方式

我很無聊的測試很多種組合之後,得到一個重點:

使用thread請不要override start() method , 執行時請用start();

至於我的測試結果是甚麼呢...有空再說吧Orz

2013年6月6日 星期四

Thread 01 基本建構方法

建立執行緒主要有三種方法:

 1. 繼承(extends)Thread 類別
class Mar1 extends Thread{
 public void run(){
  System.out.println("Hi");
}}
public class Type1 {
 public static void main(String argv[]){
  Mar1 mar1 =new Mar1();
  mar1.start();
}}
2. 實作(implements) Runnable 介面(interface) < 一定要實作run() >
class Mar2 implements Runnable{
 public void run(){
  System.out.println("Hi");
}}
public class Type2 {
 public static void main(String argv[]){
  Mar2 mar2 =new Mar();
  Thread t2 = new Thread(mar2);
  //請new一個Thread去接,不然找不到start()
  t2.start();
}}
 3. 使用匿名巢狀類別(anomymous nested class)
有幾種...
3-a:
public class Type3a {
 public static void main(String argv[]){
  Thread t3a =new Thread(new Runnable(){
   public void run(){
    System.out.println("Hi");
   }
  });
  t3a.start();
}}
或是讓Thread也匿名
3-b:
public class Type3b {
 public static void main(String argv[]){
  new Thread(new Runnable(){
   public void run(){
    System.out.println("Hi");
   }
  }).start();
}}
甚至連Runnable也不要了
3-c:
public class Type3c {
 public static void main(String argv[]){
  new Thread(){
   public void run(){
    System.out.println("Hi");
   }
  }.start();
}}
以上是基本建構方法,使用方法請看<Thread 基本使用重點>

2013年5月22日 星期三

Eclipse Note


1.Workspace : 一個目錄,放"Eclipse專案"、內部設定檔  (可啟動多個Eclipse)

2.View : Eclipse 提供一個 "功能"

3.Perspective :  由 View 組成 ,代表 Eclipse 內建的開發環境

4.Project : Eclipes 管理程式資源的最小單位

5.小知識&快速鍵

* Eclipse 要 Save 才會 Compile
* 快速 Import : Ctrl + Shift + O
* 提示小視窗 : Alt + /   (例如: sysout + ( Alt + / ) )
*<Ctrl+Shift+/>  區塊註解




*安裝Plugin:
1.找到安裝頁面,如http://www.eclipse.org/windowbuilder/download.php
2.點選"link",複製其網址
3.Eclipse 中的 “Help” -> “Install New Software”
4.在work with 後面輸入"link"的網址



以下轉貼來源: http://bobo52310.blogspot.com/2011/09/eclise10.html

◎快速鍵(熱鍵)修改處:Window->Preference->General->Keys

快捷键组合 : 按下Ctrl+Shift+L來查看

<Ctrl+Shift+O>:自動匯入所需要的類別:
簡易說明:
假設我們在Eclipse中有使用到某個class但是還沒有Import時,此時Eclipse會警示說沒有匯入類別,這時我們只要按下Ctrl+Shift+O,它就會自動幫我們Import類別。

【Alt + /】:程式碼提示,自動補齊 Template
修改處:視窗->喜好設定->工作台->按鍵->編輯->內容輔助。
簡易說明:編輯程式碼時,打sysout +Template啟動鍵,就會自動出現:System.out.println(); 。設定Template的格式:視窗->喜好設定->Java->編輯器->範本。

【
Ctrl+Shift+F】:程式碼自動排版
修改處:視窗->喜好設定->工作台->按鍵->程式碼->格式。
自動排版設定:視窗->喜好設定->Java->程式碼格式製作程式。
樣式頁面->將插入tab(而非空白鍵)以內縮,該選項取消勾選,下面空格數目填4,這樣在自動編排時會以空格4作縮排。

【
Ctrl + F11】:快速執行程式
修改處:視窗->喜好設定->工作台->按鍵->執行->啟動前一次的啟動作業。
個人習慣:F5
簡易說明:第一次執行時,它會詢問您執行模式,設置好後,以後只要按這個熱鍵,它就會快速執行。

【
Ctrl+滑鼠左鍵點擊】:查看使用類別的原始碼
簡易說明:可以看到您所使用類別的原始碼。
或者是連到對應的Method,這個很好用。
【
Ctrl+/】:將選取的文字多行註解起來
簡易說明:Debug時很方便。
修改處:視窗->喜好設定->工作台->按鍵->程式碼->註解


【Ctrl+F8】:視景切換
修改處:視窗->喜好設定->工作台->按鍵->視窗->下一個視景。
簡易說明:可以方便我們快速切換編輯、除錯等視景。


【Ctrl+Shift+P 】:轉至匹配的括號 

【F3】:找變數宣告處

【F6】:逐行執行(deBug狀態時)

【Ctrl+Shift+T】:快速搜尋 class

【Ctrl+T】:顯示繼承樹

【Ctrl+.】:尋找下一個error位置

【Ctrl+,】:尋找前一個error位置

【
Alt+方向鍵左鍵】:前往上一個工作區

【 Ctrl+'+'。另外你也可以試試看減'-'】:Expand (程式碼展開) 

Ctrl+Shift+NUM_KEYPAD_DIVIDE

【Alt+方向鍵下↓ 。另外你也可以試試看方向鍵上】:當前行與下一行對調位置(特別實用,可以省去先剪下再貼上)

【Ctrl+Shift+r】:Open Resource(開啟當下資源)
搜尋工作區的檔案,只需要輸入文件檔名前幾個字,就會搜尋出配對的檔案。
【Ctrl+o】:快速outline

【Ctrl+1】:顯示快速修復!可能的錯誤更正,其效果等同按下error行旁邊的X圖示。

【Ctrl+2】:為本地變數賦值,重構時好用。

【Ctrl+3】:Quick Access

【Ctrl+q】:前往上一步編輯所在的行(Last Edit Location)

【Ctrl+L】:前往某一行

【Ctrl+e】:搜尋已開啟的編輯頁面檔名