blog.Ring.idv.tw

Articles

Apache Tomcat 8 + SQLite

很長一段時間沒有更新該Blog了,由於最近為了做一些side project,所以打算在Mac筆電上架個簡單的開發環境,初步就想說直接利用SQLite來當做資料庫,反正只要中間層的DAO設計好,未來要改個後端也不是什麼太大的負擔,所以更不需要動到HBase之類的儲存方案。所以本文就純粹當作Memo...

必要軟體

.Apache Tomcat 8.5.43

為什麼選擇Tomcat 8的理由是,由於筆電的Java版本為Java 7,加上沒有急著升級的必要,所以就維持著...

至於SQLite的話,直接下載SQLite JDBC Driver即可,目前最新版本為sqlite-jdbc-3.27.2.1.jar

接下來就一步步地做些必要的操作:

修改 /etc/hosts

127.0.0.1    helloworld

這步驟主要是用來方便在筆電端快速地來做測試而已,上述的【helloworld】你可以改成你任意喜歡的,之後要測試時就直接在網頁上輸入http://helloworld:8080即可。

建立必要的目錄及檔案

$TOMCAT_HOME/helloworld/ROOT

$TOMCAT_HOME/helloworld/ROOT/index.jsp

上述index.jsp檔案裡任意寫個Hello JSP之類的文字即可。

修改 $TOMCAT_HOME/conf/server.xml

<Host name="helloworld"  appBase="helloworld"  unpackWARs="true" autoDeploy="true">
	     <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="helloworld_access_log." suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />
</Host>

啟動Tomcat

在啟動之前,如果你也是在Unix-like系統下安裝的話,那你可以考慮刪除掉$TOMCAT_HOME/bin/目錄底下的所有.bat檔案,這樣在透過Tab鍵補字操作下會方便些。

$TOMCAT_HOME/bin/startup.sh

接著在網頁上輸入http://helloworld:8080,應該就可以work了,接下來處理SQLite資料庫的部份。

建立必要的目錄及檔案

$TOMCAT_HOME/helloworld/ROOT/META-INF/context.xml

$TOMCAT_HOME/helloworld/ROOT/WEB-INF/classes

$TOMCAT_HOME/helloworld/ROOT/WEB-INF/lib

P.S. 請將sqlite-jdbc-3.27.2.1.jar直接copy到$TOMCAT_HOME/helloworld/ROOT/WEB-INF/lib/目錄下,至於其它目錄及檔案,待會再說明。

修改 META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Resource name="jdbc/helloworld" auth="Container" type="javax.sql.DataSource" driverClassName="org.sqlite.JDBC"
            url="jdbc:sqlite:/Users/yourname/apache-tomcat-8.5.43/helloworld/ROOT/helloworld.db"
            factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory">
  </Resource>
</Context>

上述的【url】用來設定SQLite的檔案位置,接著會透過下面的Servlet程式來操作該資料庫。

建立一個 Servlet 程式

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.*;
import java.sql.*;

@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		
		PrintWriter out = resp.getWriter();
		Connection con = null;
		ResultSet rs = null;
		PreparedStatement ps = null;

		try
		{
			Context ctx = new InitialContext();
			DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/helloworld");
			con =  ds.getConnection();

			Statement stat = con.createStatement();
			stat.executeUpdate("drop table if exists member;");
			stat.executeUpdate("create table member (id, name);");
			ps = con.prepareStatement("insert into member values (?, ?);");

			ps.setInt(1, 1);
			ps.setString(2, "Tony Lee");
			ps.addBatch();
			ps.setInt(1, 2);
			ps.setString(2, "Jack Wang");
			ps.addBatch();
			ps.executeBatch();


			rs = stat.executeQuery("select * from member;");
			while (rs.next()) {
				out.println("ID=" + rs.getInt("id")+"<br/>");
				out.println("Name=" + rs.getString("name")+"<br/>");
			}

		}catch (SQLException e)
		{
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();

			try { rs.close(); } catch (SQLException e) { e.printStackTrace(); }
			try { ps.close(); } catch (SQLException e) { e.printStackTrace(); }
			try { con.close(); } catch (SQLException e) { e.printStackTrace(); }
		}
	}
}

最後將上述程式編譯後,將HelloServlet.class檔案copy至$TOMCAT_HOME/helloworld/ROOT/WEB-INF/classes/目錄下。

重新啟動Tomcat,輸入網址http://helloworld:8080/HelloServlet就可以看到下述結果:

ID=1
Name=Tony Lee
ID=2
Name=Jack Wang

2019-07-24 19:49:43 | Add Comment

QRCode 掃描器 - 怪獸版 (支援紙本電子發票)

最近寫了一個「QRCode 掃描器 - 怪獸版」,但其實目前市面上QRCode掃描器那麼多了,為何我還想做一個? 除了練功之外,那就是市面上所有的QRCode掃描器在設計上其實都很雷同,但這也沒啥不好的,只要功能有符合大眾的需求就好了! 的確如此,但除了這之外還能不能有些不一樣?我想,而這也就是為何會有「QRCode 掃描器 - 怪獸版」App的產生。

首先,先來談談設計好了... 為什麼我會選擇「大眼怪」來作為設計的主軸?其實答案很簡單,當我確定要做一個QRCode相關的App之後,腦海裡瞬間掃過N張影像,唯獨這張「大眼怪」讓我印象深刻,直覺就認為它和QRCode掃描器相當搭,可以直接將怪獸的眼睛作為掃描的路口,所以就在這以「怪獸」為主軸的大方向確定之後,接下來其它的設計部份也就比較容易去處理了。

接著從功能面上來談... 嗯~這麼說好了,當初在決定寫一個QRCode掃描器的同時,除了介面的設計之外,那就是目前絕大多數的QRCode掃描器都沒有支援「紙本電子發票」的功能(除了少數幾款之外),所以「嶄新的介面」+「支援紙本電子發票」這兩個因素下,讓我決定開始來開發這個App。

在開發的過程中,由於並不是第一次開發App,所以倒是沒有碰到什麼比較技術性難解的問題,反倒是之前在研究「Multipeer Connectivity」時還比較棘手一些,這部份有機會再寫篇文章好了,唯一可能會稍微卡關一下的可能就是文字解碼的部份吧! 根據「電子發票證明聯 - 二維條碼」的規格書,它總共支援了三種文字編碼格式,它們分別為UTF-8Big5Base64。不過在這測試的過程中,以Base64為編碼的紙本電子發票我倒還沒看過,大多都以UTF-8為編碼的較為廣泛些,另外有少數幾間會用Big5格式,例如:中國石油…,所以解決文字的解碼問題之後,剩下的就是一些瑣碎的小事了。

有人會問我,那這個App可以直接掃描對獎嗎?基本上要實現這個功能並沒有什麼難度,但現階段我並不想做這個功能,理由其一是個人認為「掃描對獎不會比人工對獎快」,二是「傳統人工對獎比較有fu」,所以這對獎功能還是暫時先保留吧!

至於關於這個App的目前成績,從7/23日上架後到本文完稿為止,目前這個App在App Store台灣區的工具程式類排行取得算還不錯的成績(個人認為),因為在沒有花任何一毛錢做行銷之下,iPad排行從一開始的119名,到目前最佳的41名,而iPhone排行更是從榜單外,衝進到榜單內的第88名,另外用「qrcode」這個關鍵字搜尋,也從上架第一天分別在iPad及iPhone排行第5和第6名,直到現在躍進到第3、4名,由於目前只有上架五天還不到...未來如果更多人知道的話,我想排名應該可以再更好!

P.S. 最後在此特別感謝曾經幫我在FB分享的親朋好友!

2014-07-28 12:46:41 | Add Comment

開始吧!

好久一陣子沒有上來寫文章了...Orz 自從菲律賓宿霧回來之後就都待在家陪陪親人,換個角度來說...過去這十年也就只有這半年「真正」在陪親人,所以上個月帶他們去體驗了一趟東京自由行! 沒錯...我的身份就是領隊... 從買機票、訂飯店、安排滑雪、迪士尼還有怎麼線上買「薯條三兄弟」都在我的責任範圍內...Orz 呵~ 雖然雜事很多,不過也蠻多收獲的,至少我的手機裡現在存有好幾個日本旅遊必備網站,因為難得經歷到東京大雪,所以當時我都在注意當地的天氣以安排行程,另一方面也要注意電車的狀況,因為真的讓我遇到電車停駛... 好在有及時掌握這些資訊,所以就算臨時變動行程也不至於會措手不及。

而現在...?其實我原先的打算是從東京回來之後...就準備再開始我的工作生涯,但是家人卻希望我留在中部,經過一些思考之後... 我暫時決定就先留在家中,當然我不可能每天無所事事地… 所以我決定先當個獨立開發者,花點時間玩玩自己想做的... 以後的事就再說吧! 畢竟年紀也不小了,還能有這樣的時間也不容易,就從這一篇文章開始出發吧!!

P.S. 想在網路上訂購「薯條三兄弟」的朋友,請到 Calbee官網 訂購,不僅有限量更要抓準時間搶 (日本快台灣一個小時)

2014-03-08 21:29:56 | Add Comment

Bohol island

About ten days ago, I went to Bohol island to see the famous Tarsiers and Chocolate Hills, and of course it was my first time to be there. In this short tour, I realised that the Tarsier is the smallest monkey in the world, and their size is just like the size of our palm. But unfortunately, as far as I know their life span is about twenty to twenty-five years only, and their main food is insect, especially spider. And the other famous spot is the Chocolate Hills. It's quite new to me because I have no idea totally how the hills were made.

Aside from that, I also experienced dolphin watching and snorkelling. Moreover, I went to the virgin island. I must say that if you want to experience snorkelling, you must go to Balicasag because you can see many tropical fishes there, even sea turtle. And please don't forget to put some sun block on your body. Otherwise, you will experience what I have experienced. :)

2013-06-11 19:44:19 | Comments (1)

What is next?

Recently, I went to Philippines alone to take a rest and had a vacation, because I'm not younger anymore, I need to take some time to think what is next. More precisely speaking, I have no idea what I want at the moment, but I know what I don't want clearly. As you see above, I chose to go to Philippines although there are many dispute between Taiwan and Philippines recently.

2013-05-27 22:56:52 | Comments (1)

Previous Posts
Copyright (C) Ching-Shen Chen. All rights reserved.

::: 搜尋 :::

::: 分類 :::

::: Ads :::

::: 最新文章 :::

::: 最新回應 :::

::: 訂閱 :::

Atom feed
Atom Comment