顯示具有 coding 標籤的文章。 顯示所有文章
顯示具有 coding 標籤的文章。 顯示所有文章

[Xcode]簡易加入libcurl到Xproject

序言

Curl函式庫常使用在進行Http的操作,這篇文章將使用已編譯完成的libcurl(來源為Nick's software)加入一個Xcode的Project所需的設定。

環境

設定函式庫

  1. 下載ioscurl-7.32.0.tar.gz,解壓縮後可以得到[ioscurl-7.32.0]目錄
  2. 進入[ioscurl-7.32.0/iOScURL]目錄會找到[libcurl-device.a]與[libcurl-simulator.a]兩個檔案與一個[curl]目錄,將這三個東西複製到自己專案的根目錄。

    (同層目錄下也有一個xCode的Project可以做為設定的參考)

    壓縮檔內的檔案


  3. 在專案中加入所需要的函式庫。

    設定的方式為點選專案的[TARGETS→Build Phases→Link Binary With Libraries],點選下面的[+]

    加入iOS原有的[Security.framework]、[libz.dylib]與剛才複製過來的兩個.a檔[libcurl-device.a][libcurl-simulator.a]
    設定後的函式庫
  4. 專案的[TARGETS→Build Settings→Search Paths],設定[Header Search Paths],加入"$(SRCROOT)/curl"

使用範例

這邊只寫個小程式測試編譯後可以執行,詳細CURL用法請參考相關文章

#ifndef xcurl_myLib_h
#define xcurl_myLib_h

#include <curl.h>
int myLib_test1(){
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl;
CURLcode res;
curl=curl_easy_init();
{
curl_easy_setopt(curl, CURLOPT_URL,"http://www.google.com.tw");
res=curl_easy_perform(curl);
curl_easy_cleanup(curl);
}

return (int)res;
}

#endif

相關文章

[C#]MS-SQL資料庫存取函式

序言

我將C#存取MS-SQL的程式包了一層方便使用。

環境

  • VS2008
  • MS-SQL 2008

存取函式類別

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Data;

namespace MyCommon.DAO
{

    /// 
    /// MS-SQL連線物件
    /// 
    public class MSSQLBaseIO
    {
        SqlConnection sqlConn = null;
        /// 
        /// 建立連線物件
        /// 
        /// 連線字串,如Data Source=localhost;Initial Catalog=dbName;User id=Account;Password=Password;
        public MSSQLBaseIO(string connectionString)
        {
            sqlConn = new SqlConnection(connectionString);
        }
        /// 
        /// 建立連線物件
        /// 
        /// 可為localhost / domainname  / 或 IP
        /// 若Host為IP,則需為true
        /// 不設定則填0,則會用MSSQL預設port
        /// 不設定則填null,express版預設為SQLEXPRESS,正式版預設為MSSQLSERVER,從服務組態可以看到
        /// 資料庫名稱
        /// 是否使用 Windows 整合驗証方式連線,false則必需填帳號密碼
        /// 登入帳號
        /// 登入密碼
        /// 連線逾期,不設定則填0
        public MSSQLBaseIO(string host,bool isHostIP,int port,
            string instanceName, string dbName,
            bool isIntegratedSecurity,
            string id, string password,
            int timeout)
        {
            StringBuilder connectionString = new StringBuilder();
            if (isHostIP) connectionString.Append("Data Source=tcp:" + host);
            else connectionString.Append("Data Source=" + host);
            if (instanceName != null) connectionString.Append("\\" + instanceName);
            if (port != 0) connectionString.Append("," + port);
            connectionString.Append(";");
            connectionString.Append("Initial Catalog=" + dbName + ";");
            if (isIntegratedSecurity) connectionString.Append("integrated security=true;");
            if (id!=null) connectionString.Append("User id=" + id + ";");
            if (password!=null) connectionString.Append("Password=" + password + ";");
            if (timeout>0) connectionString.Append("Connect Timeout=" + timeout + ";");
            sqlConn = new SqlConnection(connectionString.ToString());
        }
        /// 
        /// 取得目前連線物件
        /// 
        /// 
        public SqlConnection getConnection()
        {
            return sqlConn;
        }

        Exception _exception = null;
        /// 
        /// 取得上次執行後的錯誤
        /// 
        /// 
        public Exception getException(){
            return _exception;
        }
        /// 
        /// 關閉連線
        /// 
        public void closeConn()
        {
            closeConn(true);
        }
        /// 
        /// 關閉連線
        /// 
        /// 是否關閉連線
        public void closeConn(bool isCloseConn){
            if (sqlConn != null && isCloseConn && sqlConn.State!=System.Data.ConnectionState.Closed)
            {
                sqlConn.Close();
            }
        }
        /// 
        /// 取得新的Transaction物件
        /// 
        /// Transaction物件
        public SqlTransaction getNewTransaction()
        {
            if (sqlConn.State != System.Data.ConnectionState.Open)
                sqlConn.Open();
            return sqlConn.BeginTransaction();
        }

        /// 
        /// 執行SQL命令,執行後會關閉SQL連線
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 變動筆數
        public int ExecuteNonQuery(string sqlcmd, bool isStoredProcedure)
        {
            return ExecuteNonQuery(sqlcmd,isStoredProcedure, true);
        }
        /// 
        /// 執行SQL命令
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 是否關閉連線
        /// 變動筆數
        public int ExecuteNonQuery(string sqlcmd, bool isStoredProcedure, bool isCloseConn)
        {
            SqlParameter[] sqlparams = null;
            return ExecuteNonQuery(sqlcmd, ref sqlparams, isStoredProcedure, isCloseConn);
        }
        /// 
        /// 執行SQL命令
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// 是否關閉連線
        /// 變動筆數
        public int ExecuteNonQuery(string sqlcmd, ref SqlParameter[] sqlparams, bool isStoredProcedure, bool isCloseConn)
        {
            return ExecuteNonQuery(sqlcmd, ref sqlparams, isStoredProcedure, null, isCloseConn);
        }
        /// 
        /// 執行SQL命令
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// SqlTransaction,可為null
        /// 是否關閉連線
        /// 變動筆數
        public int ExecuteNonQuery(string sqlcmd, ref SqlParameter[] sqlparams,bool isStoredProcedure, SqlTransaction trans, bool isCloseConn)
        {
            int result = -1;
            _exception = null;
            try
            {
                if (sqlConn.State != System.Data.ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand cmd;
                if (trans==null)
                    cmd = new SqlCommand(sqlcmd, sqlConn);
                else
                    cmd = new SqlCommand(sqlcmd, sqlConn, trans);
                if(isStoredProcedure)
                    cmd.CommandType = CommandType.StoredProcedure;
                if (sqlparams!=null)
                    cmd.Parameters.AddRange(sqlparams);
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                _exception = ex;
            }
            finally
            {
                closeConn(isCloseConn);
            }
            return result;
        }
        /// 
        /// 取得多列資料,執行後會關閉SQL連線
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 是否關閉連線
        /// 多列資料
        public List ExecuteReader(string sqlcmd, bool isStoredProcedure)
        {
            return ExecuteReader(sqlcmd, isStoredProcedure, true);
        }
        /// 
        /// 取得多列資料
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 是否關閉連線
        /// 多列資料
        public List ExecuteReader(string sqlcmd, bool isStoredProcedure, bool isCloseConn)
        {
            SqlParameter[] sqlparams = null;
            return ExecuteReader(sqlcmd, ref sqlparams, isStoredProcedure, isCloseConn);
        }
        /// 
        /// 取得多列資料
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// 是否關閉連線
        /// 多列資料
        public List ExecuteReader(string sqlcmd, ref SqlParameter[] sqlparams, bool isStoredProcedure, bool isCloseConn)
        {
            return ExecuteReader(sqlcmd, ref sqlparams, isStoredProcedure, null, isCloseConn);
        }
        /// 
        /// 取得多列資料
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// SqlTransaction,可為null
        /// 是否關閉連線
        /// 多列資料
        public List ExecuteReader(string sqlcmd, ref SqlParameter[] sqlparams, bool isStoredProcedure, SqlTransaction trans, bool isCloseConn)
        {
            List result = null;
            _exception = null;
            SqlDataReader reader = null;
            try
            {
                if (sqlConn.State != System.Data.ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand cmd;
                if (trans == null)
                    cmd = new SqlCommand(sqlcmd, sqlConn);
                else
                    cmd = new SqlCommand(sqlcmd, sqlConn, trans);
                if (isStoredProcedure)
                    cmd.CommandType = CommandType.StoredProcedure;
                if (sqlparams != null)
                    cmd.Parameters.AddRange(sqlparams);
                reader = cmd.ExecuteReader();
                object[] item;
                if (reader.HasRows)
                {
                    result = new List();
                    while (reader.Read())
                    {
                        item = new object[reader.FieldCount];
                        for (int j = 0; j < reader.FieldCount; j++)
                        {
                            item[j] = reader[j];
                        }
                        result.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                _exception = ex;
            }
            finally
            {
                if (reader!=null) reader.Close();
                closeConn(isCloseConn);
            }
            return result;
        }

        /// 
        /// 取得單一值,執行後會關閉SQL連線
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 單一值
        public object ExecuteScalar(string sqlcmd, bool isStoredProcedure)
        {
            return ExecuteScalar(sqlcmd, isStoredProcedure, true);
        }
        /// 
        /// 取得單一值
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 是否關閉連線
        /// 單一值
        public object ExecuteScalar(string sqlcmd, bool isStoredProcedure, bool isCloseConn)
        {
            SqlParameter[] sqlparams = null;
            return ExecuteScalar(sqlcmd, ref sqlparams, isStoredProcedure, isCloseConn);
        }
        /// 
        /// 取得單一值
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// 是否關閉連線
        /// 單一值
        public object ExecuteScalar(string sqlcmd,ref SqlParameter[] sqlparams, bool isStoredProcedure, bool isCloseConn)
        {
            return ExecuteScalar(sqlcmd,ref sqlparams, isStoredProcedure, null, isCloseConn);
        }
        /// 
        /// 取得單一值
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// SqlTransaction,可為null
        /// 是否關閉連線
        /// 單一值
        public object ExecuteScalar(string sqlcmd,ref SqlParameter[] sqlparams, bool isStoredProcedure, SqlTransaction trans, bool isCloseConn)
        {
            object result = null;
            _exception = null;
            try
            {
                if (sqlConn.State != System.Data.ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand cmd;
                if (trans == null)
                    cmd = new SqlCommand(sqlcmd, sqlConn);
                else
                    cmd = new SqlCommand(sqlcmd, sqlConn, trans);
                if (isStoredProcedure)
                    cmd.CommandType = CommandType.StoredProcedure;
                if (sqlparams != null)
                    cmd.Parameters.AddRange(sqlparams);
                result = cmd.ExecuteScalar();
              
            }
            catch (Exception ex)
            {
                _exception = ex;
            }
            finally
            {
                closeConn(isCloseConn);
            }
            return result;
        }

        /// 
        /// 取得單一列資料,執行後會關閉SQL連線
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 單一列資料
        public object[] ExecuteReaderSingleRow(string sqlcmd, bool isStoredProcedure)
        {
            return ExecuteReaderSingleRow(sqlcmd, isStoredProcedure, true);
        }
        /// 
        /// 取得單一列資料
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 是否關閉連線
        /// 單一列資料
        public object[] ExecuteReaderSingleRow(string sqlcmd, bool isStoredProcedure, bool isCloseConn)
        {
            SqlParameter[] sqlparams = null;
            return ExecuteReaderSingleRow(sqlcmd, ref sqlparams, isStoredProcedure, isCloseConn);
        }
        /// 
        /// 取得單一列資料
        /// 
        /// SQL命令,參數部份以 @參數名稱 標示
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// 是否關閉連線
        /// 單一列資料
        public object[] ExecuteReaderSingleRow(string sqlcmd,ref SqlParameter[] sqlparams, bool isStoredProcedure, bool isCloseConn)
        {
            return ExecuteReaderSingleRow(sqlcmd,ref sqlparams, isStoredProcedure, null, isCloseConn);
        }
        /// 
        /// 取得單一列資料
        /// 
        /// SQL命令,參數部份以 @參數名稱 標示
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// SqlTransaction,可為null
        /// 是否關閉連線
        /// 單一列資料
        public object[] ExecuteReaderSingleRow(string sqlcmd, ref SqlParameter[] sqlparams, bool isStoredProcedure, SqlTransaction trans, bool isCloseConn)
        {
            object[] result = null;
            _exception = null;
            SqlDataReader reader = null;
            try
            {
                if (sqlConn.State != System.Data.ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand cmd;
                if (trans == null)
                    cmd = new SqlCommand(sqlcmd, sqlConn);
                else
                    cmd = new SqlCommand(sqlcmd, sqlConn, trans);
                if (isStoredProcedure)
                    cmd.CommandType = CommandType.StoredProcedure;
                if (sqlparams != null)
                    cmd.Parameters.AddRange(sqlparams);
                reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
                object[] item;
                if (reader.HasRows)
                {
                    reader.Read();
                    item = new object[reader.FieldCount];
                    for (int j = 0; j < reader.FieldCount; j++)
                    {
                        item[j] = reader[j];
                    }
                    result = item;
                }
            }
            catch (Exception ex)
            {
                _exception = ex;
            }
            finally
            {
                if (reader != null) reader.Close();
                closeConn(isCloseConn);
            }
            return result;
        }
    }
}
 

[JBoss]Java的工作排程套件-Quartz

序言

作為一個Application Server,JBoss裡的應用程式開發起來主要都是被動的等待Client呼叫啟動。但實際應用上還是有可能需要有個程序能夠在特定時間執行某些命令,例如於午夜12點整進行資料的Reset這種工作。
在不依賴作業系統或是另外執行一支程式處裡工作的狀況,其實JBoss預設就有推薦使用的工作排程套件-Quartz
就Quartz的功能而言,有的功能是與Java內建的Timer重複的,但它功能更多更強大,所以相對的要做一些設定才能使用。
以下我就舉幾個我用的方法來一探Quartz的設定(當然,這不見得是最好的作法)。

開發環境

JBoss設定

JBoss內建就有Quartz的套件,但版本很老舊,從網路上得到的資訊來看,可能會有一些問題存在。
因此建議的作法是把內建的套件刪除掉,換上新的版本。
  1. 刪除Jboss下common\lib\目錄的quartz.jar檔
  2. 刪除Jboss下server發佈目錄下的quartz-ra.rar,如jboss\server\default\deploy目錄的quartz-ra.rar
  3. quartz套件的壓縮檔打開,將裡面的quartz-all-1.7.3.jar檔案複製到jboss\common\lib(或jboss\server\default\lib)

Quartz設定檔

Quartz 提供了兩種不同的方式用來把排程工作存在記憶體(RAMJobStore)或資料庫(JDBCJobStore。)中。
  • RAMJobStore:是預設的方法,因為所有資料都存在記憶體中,所以速度最快,但是如果伺服器停止後,所有排程的工作都會消失
  • JDBCJobStore:透過 JDBC 把所有工作排成資訊存在資料庫中,但代價就是效能降低和設定複雜度的提高(尤其是有參數要透過JobDataMap在工作執行時使用時)。
在這邊我使用最簡單的RAMJobStore來進行工作排程。
這時會需要建立一個設定檔【quartz-service.xml】來設定工作排程套件的參數。這個檔案會放在JBoss的發佈目錄下,如jboss\server\default\deploy
<?xml version="1.0" encoding="UTF-8"?>

<server>

<mbean code="org.quartz.ee.jmx.jboss.QuartzService" name="user:service=QuartzService,name=QuartzService">
<attribute name="JndiName">Quartz</attribute>
<attribute name="StartScheduler">true</attribute>
<attribute name="Properties">
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.xaTransacted = false

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 4

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
</attribute>
</mbean>

</server>

工作類別

工作要執行的內容必須實作【org.quartz.Job】介面,如
package control;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MyJob implements Job {
static int jobs=0;
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
JobDataMap dataMap = arg0.getJobDetail().getJobDataMap();
Object data1 =dataMap.get("data1");
Object data2 =dataMap.get("data2");
System.out.println("Start MyJob");
System.out.println("data1="+data1);
System.out.println("data2="+data2);
}

}

排程工作

排程的方式有兩種,一種是設定多久執行工作一次(SimpleTrigger),一種是設定什麼時間要執行工作一次(CronTrigger)。
兩者最主要差別在於描述方式。相同的是兩者都可以設定排程啟動時間,結束時間,重複次數等設定。
  • 使用SimpleTrigger
 String groupName = "Default";  //工作群組
String jobName = "MySimpleJob"; //工作名稱
InitialContext ctx;
ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup("Quartz");
//如果工作已排程,則刪除同樣工作群組-工作名稱的工作
if (scheduler.getJobDetail(jobName, groupName) != null) {
scheduler.deleteJob(jobName, groupName);
}
JobDetail jobDetail = new JobDetail(jobName, groupName,MyJob.class); //設定工作執行的類別為MyJob
//設定要傳給工作的參數
jobDetail.getJobDataMap().put("data1", "Job1");
jobDetail.getJobDataMap().put("data2", jobName);
SimpleTrigger simpleTrigger = new SimpleTrigger(jobName, groupName); //Trigger也會設定群組與名稱,但我不確定是有什麼功用
simpleTrigger.setRepeatCount(-1); //設定-1無限次重複,如果設為0則就只會執行一次
simpleTrigger.setRepeatInterval(15*60*1000); //每15分鐘做一次

Calendar curCal = Calendar.getInstance();
curCal.add(Calendar.MINUTE, 10);
simpleTrigger.setStartTime(curCal.getTime()); //現在時間的10分鐘後啟動工作,如果不設定的話排程開始就會執行第一次

scheduler.scheduleJob(jobDetail, simpleTrigger);
if (!scheduler.isShutdown()) {
scheduler.start();
}
  • 使用CronTrigger
 String groupName = "Default";  //工作群組
String jobName = "MyCronJob"; //工作名稱
InitialContext ctx;
ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup("Quartz");
//如果工作已排程,則刪除同樣工作群組-工作名稱的工作
if (scheduler.getJobDetail(jobName, groupName) != null) {
scheduler.deleteJob(jobName, groupName);
}
JobDetail jobDetail = new JobDetail(jobName, groupName,MyJob.class); //設定工作執行的類別為MyJob
//設定要傳給工作的參數
jobDetail.getJobDataMap().put("data1", "Job2");
jobDetail.getJobDataMap().put("data2", jobName);
CronTrigger trigger = new CronTrigger(jobName, groupName,"0 0 0 * * ?"); //每天0點整執行
scheduler.scheduleJob(jobDetail, trigger);
if (!scheduler.isShutdown()) {
scheduler.start();
}

在應用程式第一次被呼叫時排程工作

在Java Web Application中目前我還沒找到方法能在Jboss啟動時呼叫某個function,只能想到兩種方式當有第一個人啟動這個網站中的網頁時進行第一次的初始設定。
第一種方法是在static instance建立時在被New的物件建構函式中寫要做的工作,像是下面的程式。
但這種做法最大壞處是你沒辦法餵給它關於Web的Session、ServletContext等資訊,因為他是在Web內容還沒建立起來時就產生的。
import java.util.Calendar;

import javax.naming.InitialContext;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;

public class InitJob {
private static InitJob instance=new InitJob();
private InitJob(){
try {
String groupName = "Default"; //工作群組
String jobName = "MySimpleJob"; //工作名稱
InitialContext ctx;
ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup("Quartz");
//如果工作已排程,則刪除同樣工作群組-工作名稱的工作
if (scheduler.getJobDetail(jobName, groupName) != null) {
scheduler.deleteJob(jobName, groupName);
}
JobDetail jobDetail = new JobDetail(jobName, groupName,MyJob.class); //設定工作執行的類別為MyJob
//設定要傳給工作的參數
jobDetail.getJobDataMap().put("data1", "Job1");
jobDetail.getJobDataMap().put("data2", jobName);
SimpleTrigger simpleTrigger = new SimpleTrigger(jobName, groupName); //Trigger也會設定群組與名稱,但我不確定是有什麼功用
simpleTrigger.setRepeatCount(-1); //設定-1無限次重複,如果設為0則就只會執行一次
simpleTrigger.setRepeatInterval(15*60*1000); //每15分鐘做一次

Calendar curCal = Calendar.getInstance();
curCal.add(Calendar.MINUTE, 10);
simpleTrigger.setStartTime(curCal.getTime()); //現在時間的10分鐘後啟動工作,如果不設定的話排程開始就會執行第一次

scheduler.scheduleJob(jobDetail, simpleTrigger);
if (!scheduler.isShutdown()) {
scheduler.start();
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
第二種方法是在Session建立時,設定一個Static 布林變數來判斷是否是第一次啟動,然後在裡面執行要做的工作,像是下面的程式。但相對付出的代價就是每個人進來時都會判斷一次。
如果要用這種方法就要再專案的web.xml中增加listener標籤,如
<?xml version="1.0" encoding="UTF-8"?>
<web-app...>
...
<listener>
<listener-class>control.InitSession</listener-class>
</listener>
...
</web-app>
程式如下
package control;

import javax.naming.InitialContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;

public class InitSession implements HttpSessionListener {
static boolean isInited=false;
@Override
public void sessionCreated(HttpSessionEvent arg0) {
if(!isInited){
//可取得Web相關的資訊
ServletContext servletContext = arg0.getSession().getServletContext();
HttpSession session = arg0.getSession();
//工作設定
String groupName = "Default"; //工作群組
String jobName = "MyCronJob"; //工作名稱
InitialContext ctx;
ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup("Quartz");
//如果工作已排程,則刪除同樣工作群組-工作名稱的工作
if (scheduler.getJobDetail(jobName, groupName) != null) {
scheduler.deleteJob(jobName, groupName);
}
JobDetail jobDetail = new JobDetail(jobName, groupName,MyJob.class); //設定工作執行的類別為MyJob
//設定要傳給工作的參數
jobDetail.getJobDataMap().put("data1", "Job2");
jobDetail.getJobDataMap().put("data2", jobName);
CronTrigger trigger = new CronTrigger(jobName, groupName,"0 0 0 * * ?"); //每天0點整執行
scheduler.scheduleJob(jobDetail, trigger);
if (!scheduler.isShutdown()) {
scheduler.start();
}
isInited=true;
}
}

@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
}
}

總結

以上就是在JBoss上面我使用Quartz的使用經驗,至於是否有更好的作法或是我的作法有什麼缺陷,歡迎留言告訴我,謝謝

參考資料

[Java]MySQL/Oracle資料庫存取函式

序言

延續上一篇,這篇針對MySQL資料庫的存取寫出的函式提供給大家參考,開發環境與資料庫連接器的設定可參考上一篇。
我在這邊有兩種建立連線物件的方式,預設是來自JNDI的連線設定,另一種是直接產生的方式,可在建立物件時做選擇。
使用範例的部分我改用JUnit來進行我自己的原始碼驗證,大家可直接參考呼叫的方式。
PS.2011/6/21 更新,加入Oracle的連線方法(但分頁查詢的函式只能用在MySQL)

原始碼函式類別

package common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBBaseIO {
private static boolean _isDebug=false;
public enum DBType{
MySQL
,Oracle
}

public static void setDebug(boolean isDebug) {
_isDebug = isDebug;
}

public static boolean isIsdebug() {
return _isDebug;
}

private Connection con = null; // Database objects
public Connection getConnection() {
return con;
}

private Statement stat = null;
private ResultSet rs = null;
private PreparedStatement pst = null;
private boolean isClosed=false;
public boolean isClosedConn(){
return isClosed;
}

protected void finalize () {
if(!isClosed)CloseConn();
}

/**
* 建立資料庫存取物件(使用JNDI XML設定,名稱為MySqlDS)
* @throws SQLException
* @throws NamingException
*/
public DBBaseIO() throws SQLException, NamingException {
setupConnMySQLByXML();
}
/**
* 建立資料庫存取物件(使用JNDI XML設定)
* @param dsName 設定名稱
* @throws SQLException
* @throws NamingException
*/
public DBBaseIO(String dsName) throws SQLException, NamingException {
setupConnMySQLByXML(dsName);
}
/**
*
* @param sourceType
* @throws ClassNotFoundException
* @throws SQLException
* @throws NamingException
*/
public DBBaseIO(int sourceType) throws ClassNotFoundException, SQLException, NamingException{
switch(sourceType){
case 0:
setupConnMySQL("localhost:3306", "root", "", "mysql");
break;
case 1:
setupConnMySQLByXML();
break;
}
}
/**
* 建立資料庫存取物件
*
* @param str_SQLIP
* 資料庫IP
* @param str_SQLID
* 登入帳號
* @param str_SQLPSWD
* 登入密碼
* @param str_SQLDefaultDBName
* 預設資料庫
* @throws SQLException
* @throws ClassNotFoundException
*/
public DBBaseIO(String str_SQLIP, String str_SQLID, String str_SQLPSWD,
String str_SQLDefaultDBName) throws ClassNotFoundException, SQLException {
setupConnMySQL(str_SQLIP, str_SQLID, str_SQLPSWD, str_SQLDefaultDBName);
}
/**
* 建立資料庫存取物件
*
* @param dbtype
* 資料庫類型
* @param str_SQLIP
* 資料庫IP
* @param str_SQLID
* 登入帳號
* @param str_SQLPSWD
* 登入密碼
* @param str_SQLDefaultDBName
* 預設資料庫
* @throws SQLException
* @throws ClassNotFoundException
*/
public DBBaseIO(DBType dbtype,String str_SQLIP, String str_SQLID, String str_SQLPSWD,
String str_SQLDefaultDBName) throws ClassNotFoundException, SQLException {
if(dbtype==DBType.MySQL)
setupConnMySQL(str_SQLIP, str_SQLID, str_SQLPSWD, str_SQLDefaultDBName);
else if(dbtype==DBType.Oracle)
setupConnOracle(str_SQLIP, str_SQLID, str_SQLPSWD, str_SQLDefaultDBName);
else
throw new ClassNotFoundException("DBType un-define");
}
private void setupConnMySQLByXML() throws NamingException, SQLException{setupConnMySQLByXML("MySqlDS"); }
private void setupConnMySQLByXML(String name) throws NamingException, SQLException{
if(isIsdebug()){
System.out.println("DataSource:"+"java:/" + name);
}

try{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/" + name);
con = ds.getConnection();
} catch (NamingException e) {
System.err.println("Connection XML Load Error :" + e.toString());
throw e;
} catch (SQLException e) {
System.err.println("Get Connection Error :" + e.toString());
throw e;
}
}

private void setupConnMySQL(String str_SQLIP, String str_SQLID,
String str_SQLPSWD, String str_SQLDefaultDBName) throws ClassNotFoundException,SQLException {
if(isIsdebug()){
System.out.println("DataSource:"+"jdbc:mysql://" + str_SQLIP + "/"
+ str_SQLDefaultDBName
+ "?useUnicode=true&characterEncoding=UTF8");
System.out.println("ID:"+str_SQLID+"\tPassword:"+str_SQLPSWD);
System.out.println("JDBC Driver:" + "com.mysql.jdbc.Driver");
}
try {
Class.forName("com.mysql.jdbc.Driver");

con = DriverManager.getConnection("jdbc:mysql://" + str_SQLIP + "/"
+ str_SQLDefaultDBName
+ "?useUnicode=true&characterEncoding=UTF8", str_SQLID,
str_SQLPSWD);

} catch (ClassNotFoundException e) {
System.err.println("DriverClassNotFound :" + e.toString());
throw e;
}
catch (SQLException x) {
System.err.println("Exception :" + x.toString());
throw x;
}
}
private void setupConnOracle(String str_SQLIP, String str_SQLID,
String str_SQLPSWD, String str_SQLDefaultDBName) throws ClassNotFoundException,SQLException {
if(isIsdebug()){
System.out.println("DataSource:"+"jdbc:oracle:thin:"+str_SQLID+"/"+str_SQLPSWD+"@//" + str_SQLIP + "/"
+ str_SQLDefaultDBName);
}
try {
Class.forName("oracle.jdbc.OracleDriver");

con = DriverManager.getConnection("jdbc:oracle:thin:"+str_SQLID+"/"+str_SQLPSWD+"@//" + str_SQLIP + "/"
+ str_SQLDefaultDBName);

} catch (ClassNotFoundException e) {
System.err.println("DriverClassNotFound :" + e.toString());
throw e;
}
catch (SQLException x) {
System.err.println("Exception :" + x.toString());
throw x;
}
}

/**
* 執行insert update delete等更新指令用
*
* @param str_SQL
* 查詢語法
* @return 資料庫變動筆數
*/
public int executeUpdate(String str_SQL) throws SQLException,NullPointerException {
return executeUpdate(str_SQL, null);
}

/**
* 執行insert update delete等更新指令用
*
* @param str_SQL
* 查詢語法
* @param param
* 參數設定,SQL語法中以?指定,順序即為?的順序,如 where name=? (此參數可為null)
* @return 資料庫變動筆數
*/
public int executeUpdate(String str_SQL, Object[] param) throws SQLException ,NullPointerException{
return executeUpdate(str_SQL, param, true);
}

/**
* 執行insert update delete等更新指令用
*
* @param str_SQL
* 查詢語法
* @param param
* 參數設定,SQL語法中以?指定,順序即為?的順序,如 where name=? (此參數可為null)
* @param isCloseConn
* 是否關閉連線
* @return 資料庫變動筆數
*/
public int executeUpdate(String str_SQL, Object[] param, boolean isCloseConn) throws SQLException,NullPointerException {
if(isIsdebug()){
System.out.println("executeUpdate:"+str_SQL);
if(param!=null)
for(int i=0;i<param.length;i++)
System.out.println("Parameter "+ i +":"+param[i]);
System.out.println("isCloseConn :"+isCloseConn);
}

int result = 0;
if (con != null) {
try {
int i = 0;
if (param == null) {
stat = con.createStatement();
result = stat.executeUpdate(str_SQL);
} else {
pst = con.prepareStatement(str_SQL);
for (i = 0; i < param.length; i++) {
pst.setObject(i + 1, param[i]);
}
result = pst.executeUpdate();
try {
pst.clearParameters();
} catch (Exception e) {
CloseConn();
}
}
} catch (SQLException e) {
System.err.println("executeUpdate Exception :" + e.toString());
throw e;
} finally {
CloseConn(isCloseConn);
}
} else {
System.err.println("Connection is null");
throw new NullPointerException("Connection is null");
}
return result;
}


/**
* 批次執行SQL指令使用
*
* @param str_SQL
* 查詢語法的陣列
* @param isCloseConn
* 是否關閉連線
* @return 資料庫變動筆數的陣列
*/
public int[] executeBatch(String[] str_SQL, boolean isCloseConn) throws SQLException,NullPointerException {
if(isIsdebug()){
System.out.println("executeBatch:"+str_SQL);
}

int[] result=null;
if (con != null) {
try {
int i = 0;
stat = con.createStatement();
for (i = 0; i < str_SQL.length; i++) {
stat.addBatch(str_SQL[i]);
}
result=stat.executeBatch();
if(isIsdebug()){
System.out.println("executeBatch result:");
for (i = 0; i < result.length; i++) {
System.out.println(i+":"+result[i]);
}
}
} catch (SQLException e) {
System.err.println("executeBatch Exception :" + e.toString());
throw e;
} finally {
CloseConn(isCloseConn);
}
} else {
System.err.println("Connection is null");
throw new NullPointerException("Connection is null");
}
return result;
}

/**
* 批次執行SQL指令使用
* @param str_SQL 查詢語法
* @param param 以相同數量的參數形成的ArrayList
* @param isCloseConn 是否關閉連線
* @return 資料庫變動筆數的陣列
* @throws SQLException
* @throws NullPointerException
*/
public int[] addBatch(String str_SQL, ArrayList<Object[]> param, boolean isCloseConn) throws SQLException,NullPointerException {

int i = 0;
int j = 0;
if(isIsdebug()){
System.out.println("executeBatch:"+str_SQL);
if(param!=null)
for (i = 0; i < param.size(); i++) {
for (j = 0;j < param.get(i).length; j++) {
System.out.println("Parameter "+ i+","+j +":"+param.get(i)[j]);
}
}
}

int[] result=null;
if (con != null) {
try {
pst = con.prepareStatement(str_SQL);
for (i = 0; i < param.size(); i++) {
for (j = 0;j < param.get(i).length; j++) {
pst.setObject(j + 1, param.get(i)[j]);
}
pst.addBatch();
}
result=pst.executeBatch();
if(isIsdebug()){
System.out.println("executeBatch result:");
for (i = 0; i < result.length; i++) {
System.out.println(i+":"+result[i]);
}
}
} catch (SQLException e) {
System.err.println("executeBatch Exception :" + e.toString());
throw e;
} finally {
CloseConn(isCloseConn);
}
} else {
System.err.println("Connection is null");
throw new NullPointerException("Connection is null");
}

return result;
}

/**
* 查詢資料
*
* @param str_SQL
* 查詢語法
* @return 每列一筆資料,每筆資料的欄位存於 Object Array中
*/
public ArrayList<Object[]> executeQuery(String str_SQL) throws SQLException,NullPointerException {
return executeQuery(str_SQL, null);
}
/**
* 查詢資料
*
* @param str_SQL
* 查詢語法
* @param param
* 參數設定,SQL語法中以?指定,順序即為?的順序,如 where name=? (此參數可為null)
* @return 每列一筆資料,每筆資料的欄位存於 Object Array中
*/
public ArrayList<Object[]> executeQuery(String str_SQL, Object[] param) throws SQLException,NullPointerException {
return executeQuery(str_SQL, param, true);
}

/**
* 查詢資料
*
* @param str_SQL
* 查詢語法
* @param param
* 參數設定,SQL語法中以?指定,順序即為?的順序,如 where name=? (此參數可為null)
* @param isCloseConn
* 是否關閉連線
* @return 每列一筆資料,每筆資料的欄位存於 Object Array中
*/
public ArrayList<Object[]> executeQuery(String str_SQL, Object[] param,
boolean isCloseConn) throws SQLException,NullPointerException {
if(isIsdebug()){
System.out.println("executeQuery:"+str_SQL);
if(param!=null)
for(int i=0;i<param.length;i++)
System.out.println("Parameter "+ i +":"+param[i]);
System.out.println("isCloseConn :"+isCloseConn);
}
ArrayList<Object[]> result = new ArrayList<Object[]>();
if (con != null) {
try {
int i = 0;
if (param == null) {
stat = con.createStatement();
rs = stat.executeQuery(str_SQL);
} else {
pst = con.prepareStatement(str_SQL);
for (i = 0; i < param.length; i++) {
pst.setObject(i + 1, param[i]);
}
rs = pst.executeQuery();
pst.clearParameters();
}

ArrayList<Object> arr;
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
while (rs.next()) {
arr = new ArrayList<Object>();
for (i = 1; i < numberOfColumns + 1; i++) {
arr.add(rs.getObject(i));
}
result.add(arr.toArray());
}
} catch (SQLException e) {
System.err.println("executeQuery Exception :" + e.toString());
throw e;
} finally {
CloseConn(isCloseConn);
}
} else {
System.err.println("Connection is null");
throw new NullPointerException("Connection is null");
}
return result;
}

/**
* 查詢資料(請記得要關閉Result Set)
*
* @param str_SQL
* 查詢語法
* @return 回傳查詢得到的result set
*/
public ResultSet executeQueryResultset(String str_SQL) throws SQLException,NullPointerException {
return executeQueryResultset(str_SQL,null);
}
/**
* 查詢資料(請記得要關閉Connection)
*
* @param str_SQL
* 查詢語法
* @param param
* 參數設定,SQL語法中以?指定,順序即為?的順序,如 where name=? (此參數可為null)
* @return 回傳查詢得到的result set
*/
public ResultSet executeQueryResultset(String str_SQL, Object[] param) throws SQLException,NullPointerException {
if(isIsdebug()){
System.out.println("executeQuery:"+str_SQL);
if(param!=null)
for(int i=0;i<param.length;i++)
System.out.println("Parameter "+ i +":"+param[i]);
}

ResultSet result = null;
if (con != null) {
try {
int i = 0;
if (param == null) {
stat = con.createStatement();
rs = stat.executeQuery(str_SQL);
} else {
pst = con.prepareStatement(str_SQL);
for (i = 0; i < param.length; i++) {
pst.setObject(i + 1, param[i]);
}
rs = pst.executeQuery();
pst.clearParameters();
}

result=rs;
} catch (SQLException e) {
System.err.println("executeQueryResultset Exception :" + e.toString());
throw e;
}
} else {
System.err.println("Connection is null");
throw new NullPointerException("Connection is null");
}
return result;
}

/**
* 查詢資料,並傳回查詢所傳回的結果集第一個資料列的第一個資料行
*
* @param str_SQL
* 查詢語法
* @return 每列一筆資料,每筆資料的欄位存於 Object Array中
*/
public Object executescalar(String str_SQL) throws SQLException,NullPointerException {
return executescalar(str_SQL, null);
}
/**
* 查詢資料,並傳回查詢所傳回的結果集第一個資料列的第一個資料行
*
* @param str_SQL
* 查詢語法
* @param param
* 參數設定,SQL語法中以?指定,順序即為?的順序,如 where name=? (此參數可為null)
* @return 每列一筆資料,每筆資料的欄位存於 Object Array中
*/
public Object executescalar(String str_SQL, Object[] param) throws SQLException,NullPointerException {
return executescalar(str_SQL, param, true);
}

/**
* 查詢資料,並傳回查詢所傳回的結果集第一個資料列的第一個資料行
*
* @param str_SQL
* 查詢語法
* @param param
* 參數設定,SQL語法中以?指定,順序即為?的順序,如 where name=? (此參數可為null)
* @param isCloseConn
* 是否關閉連線
* @return 每列一筆資料,每筆資料的欄位存於 Object Array中
*/
public Object executescalar(String str_SQL, Object[] param,
boolean isCloseConn) throws SQLException,NullPointerException {
if(isIsdebug()){
System.out.println("executeQuery:"+str_SQL);
if(param!=null)
for(int i=0;i<param.length;i++)
System.out.println("Parameter "+ i +":"+param[i]);
System.out.println("isCloseConn :"+isCloseConn);
}
Object result = null;
if (con != null) {
try {
int i = 0;
if (param == null) {
stat = con.createStatement();
rs = stat.executeQuery(str_SQL);
} else {
pst = con.prepareStatement(str_SQL);
for (i = 0; i < param.length; i++) {
pst.setObject(i + 1, param[i]);
}
rs = pst.executeQuery();
pst.clearParameters();
}

ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
if (rs.next() && numberOfColumns>=1) {
result=rs.getObject(1);
}
} catch (SQLException e) {
System.err.println("executescalar Exception :" + e.toString());
throw e;
} finally {
CloseConn(isCloseConn);
}
} else {
System.err.println("Connection is null");
throw new NullPointerException("Connection is null");
}
return result;
}


/**
* 執行SQL
*
* @param str_SQL
* 查詢語法
* @return 是否成功
*/
public boolean execute(String str_SQL) throws SQLException,NullPointerException {
return execute(str_SQL, null);
}

/**
* 執行SQL
*
* @param str_SQL
* 查詢語法
* @param param
* 參數設定,SQL語法中以?指定,順序即為?的順序,如 where name=? (此參數可為null)
* @return 是否成功
*/
public boolean execute(String str_SQL, Object[] param) throws SQLException,NullPointerException {
return execute(str_SQL, param, true);
}

/**
* 執行SQL
*
* @param str_SQL
* 查詢語法
* @param param
* 參數設定,SQL語法中以?指定,順序即為?的順序,如 where name=? (此參數可為null)
* @param isCloseConn
* 是否關閉連線
* @return 是否成功
*/
public boolean execute(String str_SQL, Object[] param, boolean isCloseConn) throws SQLException,NullPointerException {
if(isIsdebug()){
System.out.println("executeQuery:"+str_SQL);
if(param!=null)
for(int i=0;i<param.length;i++)
System.out.println("Parameter "+ i +":"+param[i]);
System.out.println("isCloseConn :"+isCloseConn);
}
boolean result = false;
if (con != null) {
try {
int i = 0;
if (param == null) {
stat = con.createStatement();
result = stat.execute(str_SQL);
} else {
pst = con.prepareStatement(str_SQL);
for (i = 0; i < param.length; i++) {
pst.setObject(i + 1, param[i]);
}
result = pst.execute();
pst.clearParameters();
}
} catch (SQLException e) {
System.err.println("execute Exception :" + e.toString());
throw e;
} finally {
CloseConn(isCloseConn);
}
} else {
System.err.println("Connection is null");
throw new NullPointerException("Connection is null");
}
return result;
}
/**
* 關閉連線
*/
public void CloseConn() {
CloseConn(true);
}

/**
* 關閉連線
* @param isCloseConn 是否關閉Connection
*/
public void CloseConn(boolean isCloseConn) {
if(isIsdebug()){
System.out.println("CloseConn:"+isCloseConn);
}
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stat != null) {
stat.close();
stat = null;
}
if (pst != null) {
pst.close();
pst = null;
}
} catch (SQLException e) {
//System.err.println("Close Exception :" + e.toString());
} finally {
try {
if (con != null && isCloseConn) {
con.close();
isClosed=true;
}
} catch (SQLException e) {
//System.err.println("Close Exception :" + e.toString());
}
}
}


int allpage=0;

public int getAllpage() {
return allpage;
}
public void setAllpage(int allpage) {
this.allpage = allpage;
}
/**
* 查詢分頁資料
* @param colunms
* @param from
* @param where
* @param group
* @param order
* @param fpage
* @param pages
* @return
* @throws SQLException
* @throws NullPointerException
*/
public ArrayList<Object[]> selectPage(String colunms,String from,String where,String group,String having,String order,int fpage,int pages) throws SQLException,NullPointerException{
return selectPage(colunms,from,where,group,having,order,fpage,pages,null);
}
/**
* 查詢分頁資料
* @param colunms
* @param from
* @param where
* @param group
* @param order
* @param fpage
* @param pages
* @param param
* @return
* @throws SQLException
* @throws NullPointerException
*/
public ArrayList<Object[]> selectPage(String colunms,String from,String where,String group,String having,String order,int fpage,int pages, Object[] param) throws SQLException,NullPointerException{
return selectPage(colunms,from,where,group,having,order,fpage,pages,param,true);
}
/**
* 查詢分頁資料
* @param colunms
* @param from
* @param where
* @param group
* @param order
* @param fpage
* @param pages
* @param param
* @param isCloseConn
* @return
* @throws SQLException
* @throws NullPointerException
*/
public ArrayList<Object[]> selectPage(String colunms,String from,String where,String group,String having,String order,int fpage,int pages, Object[] param, boolean isCloseConn) throws SQLException,NullPointerException{
return selectPage(colunms, from, where, group, having, order, fpage, pages, true, 0, param, isCloseConn);
}
/**
* 查詢分頁資料
* @param colunms
* @param from
* @param where
* @param group
* @param having
* @param order
* @param fpage
* @param pages
* @param isCountPage
* @param allpageCount
* @param param
* @param isCloseConn
* @return
* @throws SQLException
* @throws NullPointerException
*/
public ArrayList<Object[]> selectPage(String colunms,String from,String where,String group,String having,String order,int fpage,int pages,boolean isCountPage,int allpageCount, Object[] param, boolean isCloseConn) throws SQLException,NullPointerException{
ArrayList<Object[]> result=null;
try{
String sql;
if(!isCountPage){
allpage=allpageCount;
}else{
int intRowCount;
sql="select count(*) from (select ";
if(colunms!=null){
sql+="" + colunms + " \n ";
}else{
sql+="* \n ";
}
sql+="from " + from.toString() + " \n ";
if(where!=null){
sql+="where " + where + " \n ";
}
if(group!=null){
sql+="group by " + group + " \n ";
}
if(having!=null){
sql+="having " + group + " \n ";
}
sql+=") selectcount \n ";

Object temp=executescalar(sql,param,false);
if(temp==null){
return result;
}
intRowCount=Integer.parseInt(temp.toString());
allpage=(intRowCount+fpage-1) / fpage;
}
int spage;

if(pages > allpage ){// pages == 0){
pages = 1;
}
spage=(pages-1)*fpage;
//sql="select " + colunms + " \n from " + from + " \n where " + where + " \n order by " + order + " \n limit "+ spage +","+fpage ;
sql="select ";
if(colunms!=null){
sql+= colunms + " \n ";
}else{
sql+="* \n ";
}
sql+="from " + from + " \n ";
if(where!=null){
sql+="where " + where + " \n ";
}
if(group!=null){
sql+="group by " + group + " \n ";
}
if(having!=null){
sql+="having " + group + " \n ";
}
if(order!=null){
sql+="order by " + order + " \n ";
}
sql+="limit "+ spage +","+fpage;
result=executeQuery(sql, param, false);
} catch (NullPointerException e) {
System.err.println("NullPointerException Exception :" + e.toString());
throw e;
} catch (SQLException e) {
System.err.println("SQLException Exception :" + e.toString());
throw e;
} finally {
CloseConn(isCloseConn);
}
return result;
}

}

使用範例程式

package common.test;

import static org.junit.Assert.*;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import common.DBBaseIO;

public class DBBaseIOTest {

DBBaseIO dbio;
int m_id;

String m_name;
@Before
public void setUp() throws Exception {
dbio=new DBBaseIO(0);
m_id=20100111;
m_name="tester" + m_id;
}

@After
public void tearDown() throws Exception {
dbio.CloseConn();
dbio=null;
}

@Test
public void testExecuteString() {
String msg="\n# testExecuteString\n Drop and Create :\t TestTable" + m_id;
System.out.println(msg);//DROP TABLE IF EXISTS TestTable" + m_id + ";
String str_Sql = "CREATE TABLE TestTable" + m_id + " ( " +
"`test_id` int(10) unsigned NOT NULL auto_increment, " +
"`member_id` int(10) unsigned NOT NULL, "+
"`member_name` varchar(45) NOT NULL, " +
"PRIMARY KEY (`test_id`) "+
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; ";
try{
dbio.execute(str_Sql);

}catch(Exception e){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + e.getMessage());
}
}

@Test
public void testExecuteUpdateString() {
String msg="\n# testExecuteUpdateString\n Insert :\tID=" + m_id + "\tName=" + m_name;
System.out.println(msg);
String str_Sql="Insert into TestTable" + m_id + " (member_id,member_name) values('" + m_id + "','" + m_name + "')";
try{
int result=dbio.executeUpdate(str_Sql);
if(result<=0){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + result);
}
}catch(Exception e){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + e.getMessage());
}
}

@Test
public void testExecuteQueryString() {
String msg="\n# testExecuteQueryString\n Select ALL :";
System.out.println(msg);
String str_Sql="Select * from TestTable" + m_id + "";
try{
ArrayList<Object[]> result=dbio.executeQuery(str_Sql);
if(result.size()<=0){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + result);
}else{
for(int i=0;i<result.size();i++){
for(int j=0;j<result.get(i).length;j++){
System.out.print(result.get(i)[j] + "\t");
}
System.out.println();
}
}
}catch(Exception e){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + e.getMessage());
}
}

@Test
public void testexecuteQueryResultset() {
String msg="\n# testexecuteQueryResultset\n Select ALL :";
System.out.println(msg);
String str_Sql="Select * from TestTable" + m_id + "";
try{

ResultSet rs=dbio.executeQueryResultset(str_Sql);
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
while (rs.next()) {
for (int i = 1; i < numberOfColumns + 1; i++) {
System.out.print(rs.getObject(i) + "\t");
}
System.out.println();
}

dbio.CloseConn();
}catch(Exception e){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + e.getMessage());
}
}

@Test
public void testselectPage() {
String msg="\n# testselectPage\n Insert 98 User";
System.out.println(msg);
String str_Sql="";
try{

for(int i=1;i<=98;i++){
str_Sql="Insert into TestTable" + m_id + " (member_id,member_name) values('" + m_id + i + "','" + m_name + "_" + i + "')";
int result=dbio.executeUpdate(str_Sql,null,false);
if(result<=0){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + result);
}
}
str_Sql="";
ArrayList<Object[]> result=dbio.selectPage("member_id,member_name", "TestTable" + m_id, null, "member_id", 10, 10, null, true);
if(result.size()<=0){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + result);
}else{
for(int i=0;i<result.size();i++){
for(int j=0;j<result.get(i).length;j++){
System.out.print(result.get(i)[j] + "\t");
}
System.out.println();
}
}
}catch(Exception e){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + e.getMessage());
}


}


@Test
public void testExecuteUpdateStringObjectArray() {
m_name+="_Updateed";
String msg="\n# testExecuteUpdateStringObjectArray\n Update :\tID=" + m_id + "\tName=" + m_name;
System.out.println(msg);
String str_Sql="Update TestTable" + m_id + " Set member_name=? WHERE member_id=?";
Object[] param={m_name,m_id};
try{
int result=dbio.executeUpdate(str_Sql,param);
if(result<=0){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + result);
}
}catch(Exception e){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + e.getMessage());
}
}

@Test
public void testExecuteQueryStringObjectArray() {
String msg="\n# testExecuteQueryStringObjectArray\n Select :\tID=" + m_id;
System.out.println(msg);
String str_Sql="Select * from TestTable" + m_id + " where member_id=?";
Object[] param={m_id};
try{
ArrayList<Object[]> result=dbio.executeQuery(str_Sql,param);
if(result.size()<=0){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + result);
}else{
for(int i=0;i<result.size();i++){
for(int j=0;j<result.get(i).length;j++){
System.out.print(result.get(i)[j] + "\t");
}
System.out.println();
}
}
}catch(Exception e){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + e.getMessage());
}
}

@Test
public void testExecuteUpdateStringObjectArrayBoolean() {
String msg="\n# testExecuteUpdateStringObjectArrayBoolean\n Delete :\tID=" + m_id + "\tName=" + m_name;
System.out.println(msg);
String str_Sql="Delete from TestTable" + m_id + " WHERE member_id=?";
Object[] param={m_id};
try{
int result=dbio.executeUpdate(str_Sql,param,false);
if(result<=0){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + result);
}
}catch(Exception e){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + e.getMessage());
}finally{
dbio.CloseConn();
}
}

@Test
public void testExecuteQueryStringObjectArrayBoolean() {
String msg="\n# testExecuteQueryStringObjectArrayBoolean\n Select :\tID=" + m_id;
System.out.println(msg);
String str_Sql="Select * from TestTable" + m_id + " where member_id=?";
Object[] param={m_id};
try{
ArrayList<Object[]> result=dbio.executeQuery(str_Sql,param,false);
if(result.size()>0){
for(int i=0;i<result.size();i++){
for(int j=0;j<result.get(i).length;j++){
System.out.print(result.get(i)[j] + "\t");
}
System.out.println();
}
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + result);
}
}catch(Exception e){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + e.getMessage());
}finally{
dbio.CloseConn();
}
}





@Test
public void testExecuteStringObjectArrayBoolean() {
String msg="\n# testExecuteStringObjectArrayBoolean\n Drop :\t TestTable_" + m_id;
System.out.println(msg);
String str_Sql="DROP TABLE IF EXISTS TestTable" + m_id + ";" ;

try{
dbio.execute(str_Sql,null,false);

}catch(Exception e){
fail(msg + "\n\n" + str_Sql + "\n\nResult:" + e.getMessage());
}finally{
dbio.CloseConn();
}
}



}

總結

我在開發時採用ArrayList把資料給回傳回來,因此相反的就沒有辦法回傳欄位名稱(不過我想你在寫SQL時應該已經可以指定自己要用的欄位名稱了吧)。

相關文章

[JAAS] JBoss下動態網站使用資料庫設定認證授權

序言

雖說我以前一直很不想再Try Java Tool弄很久~因為開發速度還是微軟順手些,但看來我還是逃脫不了。
總之~這篇是最近我實作時的紀錄,主要是要用Java預設的安全機制,完成網站的認證授權(也就是登入登出、 Login and Logout)。
通常書到用時方恨少,這句話這時就應驗了,腦中有的資訊太少,所以這個東西花了我不少功夫。
如果有任何不標準的用法,請見諒,很多都是我從網路找來的資訊組合而成的,我很懶得去查官方的英文文件(官方文件有很多其實也沒寫的很詳細...)。
歡迎有更標準的用法的話,請留下文章、連結等資料指正我。

開發環境

JBoss設定

  1. 資料庫連接器:將資料庫連接器【mysql-connector-java-5.1.10-bin.jar】放在Jboss的伺服器函式庫資料夾,如【jboss\server\default\lib】
  2. 資料庫連線設定:連線設定檔是用xml定義的,可直接複製JBoss提供的範本(位於【jboss\docs\examples\jca\mysql-ds.xml】)到【jboss\server\default\deploy】,並將資料庫設為你的MySQL連線設定。(如果用其他資料庫的話,在範本資料夾裡大都有範例)
    這邊比較需要注意的是等下要用到的名稱
    • jndi-name:等下設定驗證方式時會用到的名稱,例如下面範例的【MySqlDS】
    <?xml version="1.0" encoding="UTF-8"?>

    <!-- See http://www.jboss.org/community/wiki/Multiple1PC for information about local-tx-datasource -->
    <!-- $Id: mysql-ds.xml 88948 2009-05-15 14:09:08Z jesper.pedersen $ -->
    <!-- Datasource config for MySQL using 3.0.9 available from:
    http://www.mysql.com/downloads/api-jdbc-stable.html
    -->

    <datasources>
    <local-tx-datasource>
    <jndi-name>MySqlDS</jndi-name>
    <connection-url>jdbc:mysql://mysql-hostname:3306/jbossdb</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>x</user-name>
    <password>y</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
    <!-- should only be used on drivers after 3.22.1 with "ping" support
    <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
    -->
    <!-- sql to call when connection is created
    <new-connection-sql>some arbitrary sql</new-connection-sql>
    -->
    <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
    <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
    -->

    <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
    <metadata>
    <type-mapping>mySQL</type-mapping>
    </metadata>
    </local-tx-datasource>
    </datasources>


    另外,如果覺得連線密碼這樣寫明碼太危險,可參考JBoss community的文件加密儲存
  3. 驗證方式設定:在JBoss的設定目錄下【jboss\server\default\conf】,有個【login-config.xml】,這個就是設定帳號驗證的方式(如果沒有這個檔也可以自己新增)
    裡面的參數
    • UserAA: 我設定的驗證來源名稱,這個在專案中會做對應的設定。
    • dsJndiName: 這邊就會對應到上一個設定的資料庫來源名稱MySqlDS
    • principalsQuery:這邊要寫查詢帳號密碼的SQL
    • rolesQuery:這邊要寫使用者角色的查詢SQL,角色會關係到後面網站權限的設定,第一個資料【roleName】所查詢出來的值我們這邊假設可能會有admin與guest,第二個資料是群組,我目前也不清楚是做什麼的,先依據官方範例給固定文字
    <?xml version='1.0'?>
    <policy>
    <application-policy name="UserAA">
    <authentication>
    <login-module code="org.jboss.security.auth.spi.DatabaseServerLoginModule" flag = "required">
    <module-option name = "dsJndiName">java:/MySqlDS</module-option>
    <module-option name = "principalsQuery">SELECT passwd FROM userdb.usersTab where userID=?</module-option>
    <module-option name = "rolesQuery">SELECT roleName,'Roles' FROM userdb.userRolesTab where userID=?</module-option>
    </login-module>
    </authentication>
    </application-policy>
    </policy>

MySQL

以下使用SQL script建立所需的資料庫與資料表,我將同時產生兩個預設的帳號分別屬於不同的權限角色,以下比較需要注意的部份是
  • 主要用到的資料來源是UsersTab資料表中的兩個欄位userID、passwd,還有UserRolesTab的兩個欄位userID、roleName,這部份不知道是不是有型態限制或是長度限制的問題,我用別的規格下去可能就無法認證。
  • 往下的Trigger是可有可無的,是我建立還方便管理資料庫用的
  • 最下面的insert是預設帳號與角色資料的建立
    • boss/0000:角色為admin
    • test/0000:角色為guest
CREATE DATABASE  IF NOT EXISTS `userdb` /*!40100 DEFAULT CHARACTER SET utf8 */;


DROP TABLE IF EXISTS `userdb`.`UsersTab`;
CREATE TABLE `userdb`.`UsersTab` (
`userID` varchar(64) NOT NULL,
`passwd` varchar(64) NOT NULL,
`createtime` datetime DEFAULT NULL,
`updatetime` datetime DEFAULT NULL,
PRIMARY KEY (`userID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


DROP TABLE IF EXISTS `userdb`.`UserRolesTab`;
CREATE TABLE `userdb`.`UserRolesTab` (
`userID` varchar(64) NOT NULL,
`roleName` varchar(32) NOT NULL DEFAULT 'guest',
`createtime` datetime DEFAULT NULL,
PRIMARY KEY (`userID`,`roleName`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


DELIMITER $$
drop TRIGGER IF EXISTS `userdb`.UsersTab_Insert$$
CREATE TRIGGER `userdb`.UsersTab_Insert BEFORE INSERT ON `userdb`.UsersTab FOR EACH ROW
BEGIN
if new.createtime is null then
Set new.createtime = now();
end if;
if new.updatetime is null then
Set new.updatetime = now();
end if;
END$$

drop TRIGGER IF EXISTS `userdb`.UsersTab_Update$$
CREATE TRIGGER `userdb`.UsersTab_Update BEFORE UPDATE ON `userdb`.UsersTab FOR EACH ROW
BEGIN
if new.updatetime is null or old.updatetime=new.updatetime then
Set new.updatetime = now();
end if;
END$$

drop TRIGGER IF EXISTS `userdb`.UsersTab_Delete$$
CREATE TRIGGER `userdb`.UsersTab_Delete AFTER DELETE ON `userdb`.UsersTab FOR EACH ROW
BEGIN
delete from `userdb`.UserRolesTab where userID=old.userID;
END$$

drop TRIGGER IF EXISTS `userdb`.UserRolesTab_Insert$$
CREATE TRIGGER `userdb`.UserRolesTab_Insert BEFORE INSERT ON `userdb`.UserRolesTab FOR EACH ROW
BEGIN
if new.createtime is null then
Set new.createtime = now();
end if;
END$$
DELIMITER ;

insert into userdb.UsersTab (userID,passwd) values('boss','0000');
insert into userdb.UserRolesTab (userID,roleName) values('boss','admin');

insert into userdb.UsersTab (userID,passwd) values('test','0000');
insert into userdb.UserRolesTab (userID,roleName) values('test','guest');

Dynmic Web Project

動態網站的設定主要是在【WEB-INF】資料夾中新增一個【jboss-web.xml】,內容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.3V2//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_3_2.dtd">

<jboss-web>
<security-domain>java:/jaas/UserAA</security-domain>
</jboss-web>
然後修改【web.xml】。這邊我採用Form認證的方式,也就是用網頁提供Login的介面。
  • security-constraint:這一段是設定哪些路徑可由哪些Role來存取,也就是權限設定。這部份有用到的角色一定要在下面的【security-role】有定義到。另外如果想設定成只有有登入的人才能存取,可將【role-name】設定成【*】。
  • login-config:這一段就是定義Login的方式與參照頁面(另依種方式設定為BASIC)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
...
<security-constraint>
<web-resource-collection>
<web-resource-name></web-resource-name>
<url-pattern>/test/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name></web-resource-name>
<url-pattern>/Login</url-pattern>
<url-pattern>/Logout</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name></realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/login.jsp?msg=Incorrect</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>admin</role-name>
</security-role>
<security-role>
<role-name>guest</role-name>
</security-role>
...
</web-app>

JSP & Servlet

再來我針對登入部份的網頁進行開發,主要會建立JSP:login.jsp與Servlet:Login、Logout。
實際上真正的登入網頁會是login.jsp,但因為如果使用者直接輸入login.jsp的網址後,進行登入成功後系統會不知該導到哪一頁(似乎也沒辦法設定預設導頁,網路上是有另一種作法如果有需要可自行參考)。
我這邊是自己想了個比較簡單的邏輯性解法,就是做一個Login的Servlet裡面直接會跳到首頁作為預設網頁,
然後將這個Login servlet設定必須登入才能存取(也就是上一步的web.xml中設定的),讓登入的連結以這個Servlet為主就會自動導自login.jsp。
login.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>Login</title>
</head>
<body>

<h2 align="center">Login</h2>
<table border="0" align="center">
<tr>
<td>
<form method="POST" action="j_security_check">
<div>User ID :
<input type="text"
name="j_username">
</div>
<div>Password :
<input type="password"
name="j_password">
</div>
<div><input type="submit" value="Log In"></div>

<div style="color: red;"><%=(request.getParameter("msg")!=null)?request.getParameter("msg"):"" %></div>

</form>
</td>
</tr>
</table>

</body>
</html>
Login servlet
package view;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Login
*/
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}

Logout servlet
package view;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Logout extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Logout() {
super();
}

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getSession().invalidate();
response.sendRedirect(request.getContextPath() + "/");
}
}

總結

以上設定完成後,就可以進到你的應用程式,然後存取Login servlet看看能不能正常登入登出了。
在這邊補充一些資訊,如果要在程式中取得登入者後的帳號,可以用【request.getUserPrincipal()】來取得Principal物件,如果不是null就可以呼叫該物件的getName()方法。
不過我個人比較喜歡用另外一個物件【org.jboss.security.SecurityAssociation】中的【getPrincipal()】也可以。
如果是要判斷使用者的角色,可使用【request.isUserInRole("admin")】方法來知道該使用者是否屬於admin這個角色。

參考資料

[分享]PHP版網頁原始碼處理函式

序言

這是我依VB.Net的網頁原始碼處理函式翻過來的PHP版程式,可以取得網頁後,再針對特定標簽進行切割處理。

函式原始碼

我將以下原始碼存在【myClass】目錄的【HTMLParser.php】檔
<?php
Class HTMLParser{
function getHTML($url,$method="GET",$param=NULL,$noCashe=False){
//$param = array("name" => 'tim',"content" => 'test');

if($noCashe){
$cashe="Cache-Control: no-cache\r\n";
}else{
$cashe="";
}
if($method=="GET" and $param!=NULL){
$data="";
foreach ($param as $k => $v) {
if($data!="") $data.="&";
$data.= "$k=".htmlentities($v);
}
$url.="?".$data;
}

if($method=="POST" and $param!=NULL){
$data = http_build_query($param);
$contentLength="Content-length:".strlen($data)."\r\n";
$opts = array(
'http'=>array(
'method'=>$method,
'header'=>"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\r\n"
. "Accept-language: zh-tw\r\n"
. "Content-Typ: application/x-www-form-urlencoded\r\n"
. $cashe . $contentLength,
'content' => $data
)
);
}else{
$opts = array(
'http'=>array(
'method'=>$method,
'header'=>"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\r\n"
. "Accept-language: zh-tw\r\n"
. "Content-Typ: application/x-www-form-urlencoded\r\n"
. $cashe
)
);

}
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
return file_get_contents($url, false, $context);
//return file_get_contents($url);
}
//取得網頁的Body區段
function getHTMLBody($url,$postValue=NULL,$noCashe= False){
$html=NULL;
$htmlBody=NULL;
if($postValue!=NULL){
$html = $this->getHTML($url,"POST",$postValue,$noCashe);
}else{
$html = $this->getHTML($url,"GET", $postValue,$noCashe);
}
If(html!=NULL) $this->getHTMLTagContain($html, "body", $htmlBody);

return $htmlBody;
}
//取得HTML中第一個符合的標籤內容的指標,並將取得的標籤內容寫入參數中
function getHTMLTagContain($html,$tag,&$contain=NULL,&$indexEnd= -1){
$tag = strtolower($tag);
$contain = NULL;
$indexBegin= -1;
$indexEnd = -1;
$indexbBegin= -1;
If($html !=NULL){
$indexBegin = strpos(strtolower($html),"<" . $tag);
if (gettype($indexBegin)!="integer") $indexBegin=-1;
If($indexBegin > -1){
$indexbBegin = strpos($html,">",$indexBegin);
if (gettype($indexbBegin)!="integer"){
$indexbBegin=-1;
}else{
$indexbBegin+=1;
}
$findTag=False;
$indexEnd=$indexbBegin;
$lastStart=$indexbBegin;
$stopLimit2 = 9999;
do{
$indexEnd=strpos(strtolower($html),"</" . $tag,$indexEnd);
if(gettype($indexEnd)!="integer") $indexEnd=-1;
If ($indexEnd > -1){
if(gettype(strpos(substr($html,$lastStart, $indexEnd - $lastStart),"<" . $tag))=="integer"){
$lastStart=$indexEnd;
$indexEnd=$indexEnd+strlen("</" . $tag);
$findTag=True;
}else{
$findTag=False;
}
}else{
$findTag=False;
}
$stopLimit2--;
}while($findTag AND $stopLimit2>0);
If ($indexEnd > -1){
$contain = substr($html,$indexbBegin, $indexEnd - $indexbBegin);

$indexEnd= strpos($html,">",$indexEnd);
if(gettype($indexEnd)!="integer"){
$indexEnd=-1;
}else{
$indexEnd+=1;
}
}
}
}
//echo $contain;
Return $indexbBegin;
}
//取得HTML中第一個符合的標籤屬性的指標,並將取得的標籤內容與該屬性內容寫入參數中
function getHTMLTagAtt($html,$tag,$attName,&$att=NULL,&$contain=NULL,&$indexEnd= -1){
$tag = strtolower($tag);
$attName = strtolower($attName);
$contain = NULL;
$att = NULL;
$indexEnd = -1;
$indexBegin= -1;
$indexaBegin= -1;
$indexbEnd = -1;
If($html !=NULL){
$indexBegin = 0;
$stopLimit1 = 9999;
do{
$indexbEnd = -1;
$indexBegin = strpos(strtolower($html),"<" . $tag,$indexBegin);
if (gettype($indexBegin)!="integer") $indexBegin=-1;
If($indexBegin > -1){
$indexbEnd= strpos($html,">",$indexBegin);
if (gettype($indexbEnd)!="integer") $indexbEnd=-1;
If($indexbEnd > -1){
$indexaBegin = strpos(strtolower(str_replace("\"", "'",substr($html,0, $indexbEnd))),$attName . "='",$indexBegin);
if(gettype($indexaBegin)!="integer") $indexaBegin=-1;
If($indexaBegin > -1){
$sign = substr($html,$indexaBegin + strlen($attName . "="), 1);
$indexaBegin+=strlen($attName . "='");
$indexaEnd= strpos(substr($html,0, $indexbEnd),$sign,$indexaBegin);
if(gettype($indexaEnd)!="integer") $indexaEnd=-1;
If($indexaEnd > -1){
$att = substr($html,$indexaBegin, $indexaEnd - $indexaBegin);
}
}else{
$indexBegin = $indexbEnd + 1;
continue;
}
$indexbEnd+=1;
}

$findTag=False;
$indexEnd=$indexbEnd;
$lastStart=$indexbEnd;
$stopLimit2 = 9999;
do{
$indexEnd=strpos(strtolower($html),"</" . $tag,$indexEnd);
if(gettype($indexEnd)!="integer") $indexEnd=-1;
If ($indexEnd > -1){
if(gettype(strpos(substr($html,$lastStart, $indexEnd - $lastStart),"<" . $tag))=="integer"){
$lastStart=$indexEnd;
$indexEnd=$indexEnd+strlen("</" . $tag);
$findTag=True;
}else{
$findTag=False;
}
}else{
$findTag=False;
}
$stopLimit2--;
}while($findTag==True AND $stopLimit2>0);
If ($indexEnd > -1){
$contain = substr($html,$indexbEnd, $indexEnd - $indexbEnd);
$indexEnd= strpos($html,">",$indexEnd);
if(gettype($indexEnd)!="integer"){
$indexEnd=-1;
}else{
$indexEnd+=1;
}
}
}else{
break;
}
$stopLimit1--;
}while($att == NULL AND $stopLimit1>0);
}
Return $indexbEnd;
}
}
?>

使用範例

<?php
    include 'myClass/HTMLParser.php';
    $htmlParser=new HTMLParser();
    $url = "http://allen080.blogspot.com/2009/05/vbnet20.html";
    //取得整個網頁的HTML
    $htmlAll = $htmlParser->getHTML($url);
    //取得網頁Body的部份
    $htmlBody = $htmlParser->getHTMLBody($url);
    If ($htmlBody!=NULL){
        $indexEnd = strpos($htmlBody,"<span class='item-control blog-admin'>");
        if(gettype($indexEnd)!="integer")    $indexEnd=-1;
        $valueAttribute = NULL;
        $valueContain = NULL;
        If($indexEnd > 0){
            $htmlBody = substr($htmlBody,$indexEnd);
            //取得a標籤1的內容
            $htmlParser->getHTMLTagContain($htmlBody, "span", $valueContain, $indexEnd);
            echo "span標籤的內容: " . $valueContain ."<BR>\n";
            //$htmlBody = substr($htmlBody,$indexEnd);
            //取得a標籤2的內容與href的屬性
            $htmlParser->getHTMLTagAtt($htmlBody, "a", "onclick", $valueAttribute, $valueContain, $indexEnd);
          echo "a標籤的內容與onclick的屬性: " . $valueAttribute ."\t" . ",內容:" . $valueContain;
        }
    }    
?>


執行結果

span標籤的內容:
<a class='quickedit' href='http://www.blogger.com/rearrange?blogID=2698062899592178296&widgetType=HTML&widgetId=HTML1&action=editWidget' onclick='return _WidgetManager._PopupConfig(document.getElementById("HTML1"));' target='configHTML1' title='編輯'>
<img alt='' height='18' src='http://img1.blogblog.com/img/icon18_wrench_allbkg.png' width='18'/>
</a>
<BR>
a標籤的內容與onclick的屬性: return _WidgetManager._PopupConfig(document.getElementById("HTML1"));    ,內容:
<img alt='' height='18' src='http://img1.blogblog.com/img/icon18_wrench_allbkg.png' width='18'/>


總結

這是我為了練習php時寫出的東西~
我個人是覺得滿好用的~
不過有部份邏輯滿複雜的~所以我也不敢保證完全正確或是適用於各種網頁~
如果有人用有發現更好的改進方式請跟我說。


[PHP]陣列、檔案讀寫

陣列

PHP的Array也是我覺得很特別的東西,感覺有點像VB中HashTable的機制。
從程式中可注意到,要取得Array中的元素個數可用【count($dataArr)】。
新增元素的方法也很多元,也能動態新增。→官方文件

<?php
    echo "\nArray 1<br>\n";
    $dataArr = array('abc','test');
    for($i=0;$i<count($dataArr);$i++){
        echo $i."\t".$dataArr[$i] . "<br>\n";
    }
    echo "\nArray 2<br>\n";
    $dataArr = array();
    $dataArr[]='abc';
    $dataArr[]='test';
    for($i=0;$i<count($dataArr);$i++){
        echo $i."\t".$dataArr[$i] . "<br>\n";
    }
    echo "\nArray 3<br>\n";
    $dataArr = array();
    $dataArr['name']='abc';
    $dataArr['content']='test';
    for($i=0;$i<count($dataArr);$i++){
        echo $i."\t".$dataArr[$i] . "<br>\n";
    }
    foreach($dataArr as $value){
        echo $value . "<br>\n";
    }
    foreach($dataArr as $key => $value){
        echo $key."\t".$value . "<br>\n";
    }
    echo "\nArray 4<br>\n";
    $dataArr = array('name' => 'abc','content' => 'test');
    foreach($dataArr as $key => $value){
        echo $key."\t".$value . "<br>\n";
    }
?>

結果:

Array 1<br>
0    abc<br>
1    test<br>

Array 2<br>
0    abc<br>
1    test<br>

Array 3<br>

0    <br>
1    <br>
abc<br>
test<br>
name    abc<br>
content    test<br>

Array 4<br>
name    abc<br>
content    test<br>

檔案存取

檔案存取是我常會用的功能,以下列出我的寫法與常用函式:官方文件

<?php
    //存文字檔,如果檔案不存在會建立檔案,如果存在會覆蓋檔案
    function saveText($filename,$fText){
        try{
            $fh = fopen($filename, "w");
            fwrite($fh, $fText);
            fclose($fh);
        }catch(Exception $e){
            echo 'Error addText: ' .$e->getMessage();
        }
    }
    //增加文字到文字檔最後面,如果檔案不存在會建立檔案
    function addText($filename,$fText){
        try{
            $fh = fopen($filename, "a");
            fwrite($fh, $fText);
            fclose($fh);
        }catch(Exception $e){
            echo 'Error addText: ' .$e->getMessage();
        }
    }
    //讀取文字檔內容
    function loadText($filename){
        $fText=NULL;
        try{
            if(file_exists($filename))    $fText=file_get_contents($filename);           
        }catch(Exception $e){
            echo 'Error loadText: ' .$e->getMessage();
        }
        return $fText;
    }
    //刪除檔案
    function deleteFile($filename){
        return unlink($filename);
    }
    //建立資料夾
    function createDir($dirname){
        return mkdir($dirname);
    }
    //刪除資料夾
    function deleteDir($dirname){
        return rmdir($dirname);
    }
?>

[初學]PHP入門心得

序言

雖然寫了許久的程式,但一直沒花時間去練習PHP。
最近花了點時間試試PHP,增長了些見聞,後續幾篇會一一記錄下我開發時注意到的事。

開發環境

  • Windows 2003
  • TWAMPs(Tiny Windows Apache MySQL PHP Portable Web Server)V1.3.3中文版
    我開發的時候雖然已經有V1.3.5版,但執行的時候不知道為什麼我的整個作業系統會出現很多衝突的現象,很多程式都受影響,所以就往回推用舊的版本。V1.3.4版無法下載,就改用V1.3.3。以下是這個免安裝的環境提供的版本:
    • Apache:2.2.11
    • PHP:5.2.10
    • MySQL:5.1.36-community
    • phpMyAdmin:3.2.0.1
    • Drupal:6.13
  • Notepad++ 5.4.4
    我沒有特地找PHP的編輯器,所以編輯起來比較費工,不過Notepad還是有用顏色標亮特定的語法,所以還是有點用的。

環境設定

  • 啟動前可以看一下根目錄的【README.txt】來找到對應的Port或其他值的更改地點,但有些部分【README.txt】沒有寫的很正確,所以可能還是要靠經驗與人工找找實際的目錄,如:
    •  Apache PORT更改,README.txt說【edit httpd.conf in TWAMP\Apache\conf】,但實際路徑會在【 TWAMPd\app\conf】下的httpd.conf,找到並更改【Listen 80】
    • php.ini的位置,README.txt說【Edit php.ini in TWAMPd\PHP】,但實際路徑會在【TWAMPd\app\bin】
    • 【TWAMPd\htdocs】目錄下會是所有網頁文件所在的地方。

Apache 目錄存取限制設定

【.htaccess】 是一個文字檔,可以自己建立後,放在要限制/保護的目錄下,Apache就會依裡面的設定內容對使用者的存取進行限制或保護。
例如我對目錄要完全不給人進來存取檔案,就在文字檔內寫入以下內容

order allow,deny
deny from all

基本上這只是最簡單的功能,但很常用。如果需要更多設定,其實這個檔也可以設定的很複雜,網路上還有人專門做出設定工具:htaccessEditor,在此就不詳述了。

PHP開發兩三事

  • 基本語法

    <?php echo "123"; ?>

    【<?php ... ?>】指的是裡面的code【...】是php,以副檔名為*.php的檔案放在【TWAMPd\htdocs】目錄下,就可以被編譯執行。【echo】會將後面帶的文字顯示在頁面這段程式所在的位置上。→官方文件-基本語法官方文件-echo
  • include

    <?php
    include 'myClass/HTMLParser.php';
    ?>

    include 的寫法,會將include的PHP程式如同嵌入在宣告的位置一樣,我想使用方法應該就如同C一樣,欲使用前必先宣告。→官方文件
  • 變數、單引號與雙引號

    <?php
        $value="123";
        echo "1: " . $value . "\r\n<br>";
        echo "2: " . "abc\t$valuedef\r\n<br>";
        echo "3: " . 'abc$valuedef\r\n<br>';
        echo "4: " . "abc\t$value def\r\n<br>";
        echo "5: " . 'abc$value def \r\n<br>';
        echo "6: " . 'abc' . $value . 'def\r\n<br>';
        echo "7: " . '"' . $value . "'\r\n<br>";
    ?>

    結果:

    1: 123
    <br>2: abc    
    <br>3: abc$valuedef\r\n<br>4: abc    123 def
    <br>5: abc$value def \r\n<br>6: abc123def\r\n<br>7: "123'
    <br>

    上面的程式中可以看到幾個現象:變數的前面用【$】來標示它是個變數,而且不用先行宣告,隨使用就會產生。以下逐行來看
    1. 字串的組合是用點【.】來標示兩字串要串聯起來。【雙引號】中的特殊文元【\】標示了後面帶了特殊字元【\r\n】,這都是換行符號,如果用VB來形容就是【vbCrLf】,這個特殊字元可以單獨使用,如【\n】。(PS:按「Enter」鍵實際產生兩個字元換行和回到開頭(ASCII編碼10:\n和13:\r))
    2. 【雙引號】中的變數被編譯成顯示,但因為英文字連再一起,所以編譯後是顯示【valuedef】變數的值。因為根本沒有宣告【valuedef】變數的值,所以最後產生的時候是空值,就什麼都沒有。
    3. 【單引號】直接輸出文字內容,所以特殊字元或變數都無效。
    4. 【雙引號】中的變數被編譯成顯示,此字串內容中,還有用到特殊字元【\t】來出現tab的字元。
    5. 【單引號】直接輸出文字內容,所以特殊字元或變數都無效。
    6. 【單引號】直接輸出文字內容,所以特殊字元或變數都無效。
    7. 雙引號內容中可以出現單引號,反過來說也行,如果雙引號內容中要出現雙引號,需要加上特殊字元\。
    PHP 會解譯【雙引號】內容中的變數,而【單引號】則視為純字串出,PHP 不會再處理單引號內的內容。所以單引號與雙引號在使用時會有時機上的差異。
  • 常數

    <?php
        define('myValue','1234');
        echo myValue;
    ?>

    常數的寫法與變數不同,定義後名稱前面不需要再加上【$】號。→官方文件
  • 脫逸 (Escape)字

    <?php
        //去除get_magic_quotes_gpc on 時 針對特殊字做脫逸 (Escape)的影響
        if (get_magic_quotes_gpc())
        {   
            $HTTP_GET_VARS = array_map('stripslashes', $HTTP_GET_VARS);
            $HTTP_POST_VARS = array_map('stripslashes', $HTTP_POST_VARS);
            $HTTP_COOKIE_VARS = array_map('stripslashes', $HTTP_COOKIE_VARS);
            $_GET = array_map('stripslashes', $_GET);
            $_POST = array_map('stripslashes', $_POST);
            $_COOKIE = array_map('stripslashes', $_COOKIE);
            $_REQUEST = array_map('stripslashes', $_REQUEST);
        }
    ?>

    這個部份是比較與執行環境設定有關的。在PHP.ini中有個設定【get_magic_quotes_gpc on】(官方文件),可以讓使用者輸入的字元中一些特殊的字元加上【\】,如單引號【'】、雙引號【"】、斜線【\】,以防程式處理時會造成意想不到的錯誤。但這樣的設定可能反而會讓我們在處理資料時更加棘手,所以這串程式能判斷當這個設定開啟時,讓這些特殊的字元不經過加上【\】的畜哩,直接取得使用者輸入的內容。
相關連結
  • TWAMP:免安裝的中英文伺服器執行環境。
  • PHP官方開發手冊:英文開發手冊。
  • W3School:簡體中文的網站開發程式語言文件,雖然有些不夠完整,不過很適合當工具書或初學參考(除了PHP,其他還有許多網頁語言)。

[程式]VB.Net2.0資料庫通用存取函式

序言

在VS2005建立VB專案(或Web網站)後,若要進行資料庫存取,我個人比較習慣將連線字串寫在設定檔app.config(或web.config)下。

不過VB專案預設是不會產生這個檔,而且在程式中若要取得連線字串的設定也要對專案的參考進行設定。

以下記錄下我做的設定,並把資料庫存取程式簡化為函式。

開發環境

  • VB.Net 2.0 (VS2005)
  • MSSQL2005 Express

專案設定

  • 建立專案後預設的專案屬性下,參考的元件如下圖:

  • 下面的程式中會用到【ConfigurationManager】這個物件來取得設定檔的連線字串,所以我們需要加入【System.Configuration】這個元件:

    點選加入,在.Net分頁下找到【System.Configuration】這個元件
  • 再來要在專案加入新項目【應用程式組態】,名稱用預設的【app.config】就可以了:

  • 編輯app.config,在【configuration】標籤內加入連線字串的設定如:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    ...
    <connectionStrings>
    <add name="MSSQLDB1"
    connectionString="Data Source=localhost;Initial Catalog=MyTestDB;Integrated Security=True"
    providerName="System.Data.SqlClient" />
    <add name="MSSQLDB2"
    connectionString="Data Source=127.0.0.1;Initial Catalog=MyTestDB;User ID=sa;Password=1234"
    providerName="System.Data.SqlClient" />
    </connectionStrings>
    ...

通用存取函式類別


Imports System.Data.SqlClient
Imports System.Configuration
Public Class DBAccessFunc
    ''' <summary>
    ''' 從ConfigurationManager中的ConnectionStrings找出Initial Catalog
    ''' </summary>
    ''' <param name="ConnName">ConnectionStrings名稱(String)</param>
    ''' <returns>回傳資料庫名稱</returns>
    ''' <remarks>從ConfigurationManager中的ConnectionStrings找出Initial Catalog</remarks>
    Public Shared Function getDBName(ByVal ConnName As String) As String
        Dim val As String = ""
        Dim ConnString As String = ConfigurationManager.ConnectionStrings(ConnName).ConnectionString
        Dim i As Integer = ConnString.IndexOf("Initial Catalog=")
        If i > -1 Then
            val = ConnString.Substring(i + "Initial Catalog=".Length)
            i = val.IndexOf(";")
            If i > -1 Then
                val = val.Substring(0, i)
            End If
        End If
        Return val
    End Function

    Public Shared Function getConnString(ByVal ConnName As String) As String
        Return ConfigurationManager.ConnectionStrings(ConnName).ConnectionString
    End Function


    Public Shared Function getConn(ByVal ConnName As String) As SqlConnection
        Return New SqlConnection(getConnString(ConnName))
    End Function
    ''' <summary>
    ''' 查詢資料庫
    ''' </summary>
    ''' <param name="cn">連線</param>
    ''' <param name="sql">查詢內容</param>
    ''' <returns>回傳查詢結果的DataTable</returns>
    ''' <remarks>查詢資料庫並回傳查詢結果的DataTable</remarks>
    Public Shared Function getTable(ByRef cn As SqlConnection, ByRef sql As String, Optional ByVal isColseConn As Boolean = True) As Data.DataTable
        Dim da As New SqlDataAdapter(sql, cn)
        Dim dt As New Data.DataTable("dt_xml")
        Try
            If cn.State = ConnectionState.Closed Then cn.Open()
            da.SelectCommand.CommandTimeout = 36000000
            da.Fill(dt)
        Catch ex As Exception
            If isColseConn And cn.State = ConnectionState.Open Then cn.Close()
            Throw ex
        End Try
        If isColseConn And cn.State = ConnectionState.Open Then cn.Close()
        Return dt
    End Function

    ''' <summary>
    ''' 執行命令
    ''' </summary>
    ''' <param name="cn">連線</param>
    ''' <param name="sql">查詢內容</param>
    ''' <returns>回傳受影響的資料筆數</returns>
    ''' <remarks>執行命令並回傳受影響的資料筆數</remarks>
    Public Shared Function doCmd(ByRef cn As SqlConnection, ByRef sql As String, Optional ByRef param() As SqlParameter = Nothing, Optional ByVal isColseConn As Boolean = True) As Integer
        Dim result As Integer = 0
        Dim cmd As New SqlCommand
        Try
            cmd.Connection = cn
            cmd.CommandText = sql
            If Not param Is Nothing Then
                cmd.Parameters.AddRange(param)
            End If
            cmd.CommandTimeout = 36000000
            If cn.State = ConnectionState.Closed Then cn.Open()
            result = cmd.ExecuteNonQuery()
        Catch ex As Exception
            cmd.Parameters.Clear()
            If isColseConn And cn.State = ConnectionState.Open Then cn.Close()
            Throw ex
        Finally
            cmd.Parameters.Clear()
            If isColseConn And cn.State = ConnectionState.Open Then cn.Close()
        End Try
        Return result
    End Function


    ''' <summary>
    ''' 取得單一資料
    ''' </summary>
    ''' <param name="cn">連線</param>
    ''' <param name="sql">查詢內容</param>
    ''' <returns>回傳受影響的資料筆數</returns>
    ''' <remarks>執行命令並回傳受影響的資料筆數</remarks>
    Public Shared Function doScalar(ByRef cn As SqlConnection, ByRef sql As String, Optional ByRef param() As SqlParameter = Nothing, Optional ByVal isColseConn As Boolean = True) As Object
        Dim result As Object = Nothing
        Dim cmd As New SqlCommand
        Try
            cmd.Connection = cn
            cmd.CommandText = sql
            If Not param Is Nothing Then
                cmd.Parameters.AddRange(param)
            End If
            cmd.CommandTimeout = 36000000
            If cn.State = ConnectionState.Closed Then cn.Open()
            result = cmd.ExecuteScalar()
        Catch ex As Exception
            cmd.Parameters.Clear()
            If isColseConn And cn.State = ConnectionState.Open Then cn.Close()
            Throw ex
        Finally
            cmd.Parameters.Clear()
            If isColseConn And cn.State = ConnectionState.Open Then cn.Close()
        End Try
        Return result
    End Function

    Public Function executeStoredProcedure(ByRef cn As SqlConnection, ByVal procedure As String, Optional ByRef param As SqlParameter() = Nothing, Optional ByRef output As SqlParameter() = Nothing, Optional ByVal isColseConn As Boolean = True) As Integer
        Dim result As Integer = 0
        Dim cmd As New SqlCommand(procedure, cn)
        Try

            cmd.CommandText = procedure
            cmd.CommandTimeout = 36000000
            cmd.CommandType = CommandType.StoredProcedure
            If Not param Is Nothing Then cmd.Parameters.AddRange(param)
            If Not output Is Nothing Then
                For Each o As SqlParameter In output
                    o.Direction = ParameterDirection.Output
                Next
                cmd.Parameters.AddRange(output)
            End If

            '開啟資料庫連線
            If cn.State = ConnectionState.Closed Then cn.Open()
            '設定變數儲存受影響資料列
            result = cmd.ExecuteNonQuery()
        Catch ex As Exception
            cmd.Parameters.Clear()
            If isColseConn And cn.State = ConnectionState.Open Then cn.Close()
            Throw ex
        Finally
            cmd.Parameters.Clear()
            If isColseConn And cn.State = ConnectionState.Open Then cn.Close()
        End Try
        Return result
    End Function
End Class


使用範例程式

Dim connStr1 As String = DBGenFunc.getConnStr("MSSQLDB1")
Dim sourceTable As String() = {"[" & DBGenFunc.getDBName("MSSQLDB1") & "].[dbo].[" & "MyTestTab" & "]", _
"[" & DBGenFunc.getDBName("MSSQLDB1") & "].[dbo].[" & "MyContentTab" & "]"}
Dim param() As SqlClient.SqlParameter = {New SqlClient.SqlParameter("P0", SqlDbType.VarChar)}
Dim sql As String

Dim title As String

Console.WriteLine("doCmdScalar Start:")
sql = "SELECT Title" & vbCrLf & _
" FROM " & sourceTable(0) & "" & vbCrLf & _
" WHERE SN=@P0"
param(0).Value = 1
title = DBGenFunc.doCmdScalar(connStr1, sql, param)
Console.WriteLine("doCmdScalar" & vbTab & title)

Console.WriteLine("getTable Start:")
sql = "SELECT Title" & vbCrLf & _
" FROM " & sourceTable(0) & "" & vbCrLf
Dim dt As DataTable = DBGenFunc.getTable(connStr1, sql)
For Each r As DataRow In dt.Rows
title = r.Item(0)
Console.WriteLine("getTable" & vbTab & title)
Next

Console.WriteLine("getTableToArray Start:")
Dim data As String() = DBGenFunc.dataTableToArray(Of String)(dt, 0)
For Each r As String In data
title = r
Console.WriteLine("getTable" & vbTab & title)
Next

Console.WriteLine("doCmd Start:")
sql = "INSERT INTO " & sourceTable(0) & "" & vbCrLf & _
"VALUES(@P0,@P1)"
param = New SqlClient.SqlParameter() {New SqlClient.SqlParameter("P0", SqlDbType.VarChar), _
New SqlClient.SqlParameter("P1", SqlDbType.VarChar)}
param(0).Value = "TNew"
param(1).Value = "SNew"
title = DBGenFunc.doCmd(connStr1, sql, param)
Console.WriteLine("doCmd" & vbTab & title)

Console.WriteLine("getReader Start:")
sql = "SELECT Title, Subject" & vbCrLf & _
" FROM " & sourceTable(0) & "" & vbCrLf
Dim dr As SqlClient.SqlDataReader = DBGenFunc.getReader(connStr1, sql)
While dr.Read()
title = dr.Item(0)
Console.WriteLine("getReader" & vbTab & title & vbTab & dr.Item(1))
End While
DBGenFunc.closeReader()

執行結果

doCmdScalar Start:
doCmdScalar T1
getTable Start:
getTable T1
getTable T2
getTable T3
getTable TNew
getTableToArray Start:
getTable T1
getTable T2
getTable T3
getTable TNew
doCmd Start:
doCmd 1
getReader Start:
getReader T1 S1
getReader T2 S2
getReader T3 S3
getReader TNew SNew
getReader TNew SNew

這裡是關於技術的手札~

也歡迎大家到

倫與貓的足跡



到噗浪來

關心一下我唷!
by 倫
 
Copyright 2009 倫倫3號Beta-Log All rights reserved.
Blogger Templates created by Deluxe Templates
Wordpress Theme by EZwpthemes