Tutorial for Application Development



Monday 18 October 2010

Hibernate กับ วิธี Count Record แบบต่างๆ บน Hibernate

นั่งๆ Research วิธี Count Record ด้วย Hibernate ดูเลยเอามาฝากครับ
แบบ แรก แบบง่าย ๆ นะครับ อันนี้ไม่ต้องมี Mapping ก็ใช้ได้
BigDecimal count = (BigDecimal) session.createSQLQuery("select count(*) from TableName").uniqueResult();
System.out.println("Count "+count);

หรือแบบนี้ก็ได้ ครับถ้ามี Mapping อยู่แล้ว เอา Projections มาช่วยก็ง่ายดี น่ะ
Criteria crit = session.createCriteria(Table.class);
crit.setProjection(Projections.rowCount());
Object o = crit.uniqueResult();
System.out.println("Count "+o);

ลองเอาไปใช้กันดูนะครับ

Jquery Javascript Framework ตัวเล็กแต่แสบ

วันนี้ได้ลอง เข้าเว็บ jquery.com มาครับ เห็น บัก อำนาจ เคยเล่าให้ฟังว่ามันเจ๋ง ดี

กล่างถึง บัก อำนาจ หลายคนคงจะงงว่าเ ป็นใคร มันเป็นเพื่อนผมเองครับตอนนี้ทำงานที่ sanook.com (ใครเค้าอยากจะรู้ล่ะเนี่ย)



แรกๆ ก็ไม่ได้สนใจอะไรมากมายครับแต่วันนี้ว่างจัดเลยลองดูขำๆ เผื่อมีคนถามจะได้รู้ว่ามันคืออะไร ตอนแรกกะว่าจะเปิดอ่านๆ ดูเล่นๆ ว่าตกลงมันคือะไรกันแน่ ใครสนใจดูต่อได้ที่นี่เลยนะครับ jquery.com

อ่านไปซักพัก พอรู้ว่าเป็น javascript framework ตัวนึงก็ไม่ได้สนใจอะไรมากเ พราะคิดว่าคงเ ป็นเ รื่องยุ่งยากใ นการแ กะดูพอสมควรแ ต่ พอกดไปหน้า download เจะ ไฟล์ .js ไฟล์เดียวครับเลยเริ่มสนใจขึ้นมาทันที
อะไ รกันเนี่ย javascript แค่ไฟล์เดียวเองเนี่ยนะ .... รำพันกับตัวเองพักนึ่งก็ download ลงมาอย่างรวดเร็ว พร้อมกับลองทำตาม Tutorial อย่างรวดเร็ว และง่ายดาย

สิ่งที่เกิดขึ้นแทบไม่น่าเชื่อ เพราะแค่ผมลองทำตามแบบ พื้นๆ ง่ายๆ น้ำจิ้มๆ ก็ทำให้ผมอึ้งและเริ่มมองเห็นประโยชน์ของมันขึ้นมาทันที เดี๋ยว ยกตัวอย่างให้ดู ซักหน่อยครับ






THE BASICS


This is a basic tutorial, designed to help you get started using jQuery. If you don''t have a test page setup yet, start by creating a new HTML page with the following contents:
  <html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
// Your code goes here
</script>
</head>
<body>
<a href="http://jquery.com/">jQuery</a>
</body>
</html>

Edit the src attribute in the script tag to point to your copy of jquery.js. For example, if jquery.js is in the same directory as your HTML file, you can use:
 <script type="text/javascript" src="jquery.js"></script>

You can download your own copy of jQuery from the Downloading jQuery page.




Launching Code on Document Ready


The first thing that most Javascript programmers end up doing is adding some code to their program, similar to this:
 window.onload = function(){ alert("welcome"); }

Inside of which is the code that you want to run right when the page is loaded. Problematically, however, the Javascript code isn''t run until all images are finished downloading (this includes banner ads). The reason for using window.onload in the first place is due to the fact that the HTML ''document'' isn''t finished loading yet, when you first try to run your code.

To circumvent both problems, jQuery has a simple statement that checks the document and waits until it''s ready to be manipulated, known as the ready event:
 $(document).ready(function(){
// Your code here
});

The remaining jQuery examples will need to be placed inside the ready event so that they are executed when the document is ready to be worked on. Add the next section of code:
 $("a").click(function(event){
alert("Thanks for visiting!");
});

Save your HTML file and reload the test page in your browser. Clicking the link on the page should make a browser''s alert pop-up, before leaving to go to the main jQuery page.

For click and most other events, you can prevent the default behaviour - here, following the link to jquery.com - by calling event.preventDefault() in the event handler:
 $("a").click(function(event){
alert("As you can see, the link no longer took you to jquery.com");
event.preventDefault();
});



Adding a CSS Class


Another common task is adding (or removing) a CSS (Cascading Style Sheet) class.
First, if you were to add some style information into the header of your script, like this:
 <style type="text/css">
a.test { font-weight: bold; }
</style>

and then added the addClass call -
  $("a").addClass("test");

all your a elements would now be bold. To remove the class, you''d use removeClass
 $("a").removeClass("test");


  • (CSS allows multiple classes to be added to an element.)






อันนี้ Copy มาทั้งดุ้นเลยนะครับ เล่าให้ฟังนิดนึง แค่ผมลองทำไปตาม Tutorial เรื่อย ๆ แค่ไล่ลงมาุถึง ส่วนของ CSS เท่านั้นผมก็เริ่มเห็นว่ามันสุดยอดแล้วล่ะครับ แค่นี้ก็จะเห็นว่า จะช่วยใ้ห้ทำงานได้ง่ายขึ้นไปอีกเยอะ
แ ละคิดว่านี่มันแค่น้ำจิ้มๆ และคงจะมีอะไรที่ น่าทึ่งรออยู่ ผมคงติดตามต่อไปจนจบครับ เดี๋ยวจะมาเล่าให้ฟังเรื่อยๆ เพื่อนๆคนใหนลองแล้วจะมา Update กันให้รู้ด้วยก็ดีนะครับ

Java ความรู้ Java การติดต่อฐานข้อมูลผ่าน JDBC

Note ไว้กันลืมครับ

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class ConnectDB {
public Connection getConnection(){
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver://host;DatabaseName=DB","dbuser","dbpass");
return con;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main (String args[]){
ConnectDB con=new ConnectDB();
System.out.println(con.getConnection());
}
}
ข้างต้นเป็นตัวอย่าง การ ติดต่อ ฐานข้อมูล ผ่าน JDBC ของ MSSQL นะครับ โดยใช้ Driver ของ JTDS ต้อง Download Libraly ของ JTDS เ พิ่มก่อนน่ะ

Java Script Select Box กับ Properties selectedIndex

อันนี้ผม Note ไว้จำของผมเองน่ะครับ ส่วนตัวชอบมีปัญหาในการเขียน Javascript
จัดการกับ Select box เลย Note ไว้เป็นตัวอย่างเวลาลืมครับ

<script language=javascript>
function sevent()
{
var xx = document.xx.sbox;
document.xx.sbox.selectedIndex = 1;
}
</script>

<form name=xx>
<select name=sbox onMouseOver="sevent()">
<option value="option 1">option 1</option>
<option value="option 2">option 2</option>
</select>
</form>

ใ ครจะเอาไปใช้ก็ไม่ได้ว่ากันนะครับ ถ้าอยากรู้ว่ามันสำคัญแค่ใหนสำหรับผมก็ถามมาได้น่ะ เดี๋ยวเข้ามาตอบ แต่ตอนนี้ขอแค่ Note ไว้ก่อนน่ะ

[ad]

Monday 4 October 2010

Java การรับค่าจากแป้นพิมพ์ คำสั่งเจ้าปัญหาแต่ว่าเป็นพื้นฐานที่้ต้องรู้

เป็นคำถามที่ถามกันอย่างแพร่หลายสำหรับการรับค่าจากแป้นพิมพ์โดยเฉพาะในหมู่น้องๆนักศึกษาทั้งหลายที่ต้องเขียนโปรแกรมส่งอาจารย์เห็นผ่านตาเลยเก็บตกมาฝากครับ

import java.io.*;

class TestSum{
public static void main(String[] args) throws IOException{
System.out.print("Enture Number 1 : ");

InputStreamReader in1 = new InputStreamReader(System.in);
BufferedReader buffer1 = new BufferedReader(in1);

String one = buffer1.readLine();
int number1 = Integer.parseInt(one);
System.out.print("Enture Number 2 : ");

InputStreamReader in2 = new InputStreamReader(System.in);

BufferedReader buffer2 = new BufferedReader(in2);

String two = buffer2.readLine();

int number2 = Integer.parseInt(two);
System.out.print("Enture Number 3 : ");

InputStreamReader in3 = new InputStreamReader(System.in);

BufferedReader buffer3 = new BufferedReader(in3);

String three = buffer3.readLine();
double number3 = Double.parseDouble(three);
System.out.print("Enture Number 4 : ");

InputStreamReader in4 = new InputStreamReader(System.in);
BufferedReader buffer4 = new BufferedReader(in4);

String four = buffer4.readLine();

double number4 = Double.parseDouble(four);

double sum = (number1 + number2 + number3 + number4);
System.out.println("number1 + number2 + number3 + number4 = "+sum);
}
}
อันนี้เป็นตัวอย่างโปรแกรม บวกลขน่ะครับ ดูให้ดีในส่วนของ

InputStreamReader กับ BufferedReader นะครับ

ตรงนั้นคือจุดสำคัญที่ โปรแกรมนี้ต้องการเน้น

MySQL String function ช่วยให้ทำงานได้ง่ายขึ้นเยอะเลยนะ

วันนี้ นำความรู้เกี่ยวกับ Function String ของ MySQL มาฝากครับ เห็นว่าน่าจะเป็นประโยชน์ ครับผมใช้แล้วชอบมากๆ
Name Description
ASCII() Return numeric value of left-most character
BIN() Return a string representation of the argument
BIT_LENGTH() Return length of argument in bits
CHAR_LENGTH() Return number of characters in argument
CHAR() Return the character for each integer passed
CHARACTER_LENGTH() A synonym for CHAR_LENGTH()
CONCAT_WS() Return concatenate with separator
CONCAT() Return concatenated string
ELT() Return string at index number
EXPORT_SET() Return a string such that for every bit set in the value bits, you get an on string and for every unset bit, you get an off string
FIELD() Return the index (position) of the first argument in the subsequent arguments
FIND_IN_SET() Return the index position of the first argument within the second argument
FORMAT() Return a number formatted to specified number of decimal places
HEX() Return a hexadecimal representation of a decimal or string value
INSERT() Insert a substring at the specified position up to the specified number of characters
INSTR() Return the index of the first occurrence of substring
LCASE() Synonym for LOWER()
LEFT() Return the leftmost number of characters as specified
LENGTH() Return the length of a string in bytes
LIKE Simple pattern matching
LOAD_FILE() Load the named file
LOCATE() Return the position of the first occurrence of substring
LOWER() Return the argument in lowercase
LPAD() Return the string argument, left-padded with the specified string
LTRIM() Remove leading spaces
MAKE_SET() Return a set of comma-separated strings that have the corresponding bit in bits set
MATCH Perform full-text search
MID() Return a substring starting from the specified position
NOT LIKE Negation of simple pattern matching
NOT REGEXP Negation of REGEXP
OCTET_LENGTH() A synonym for LENGTH()
ORD() Return character code for leftmost character of the argument
POSITION() A synonym for LOCATE()
QUOTE() Escape the argument for use in an SQL statement
REGEXP Pattern matching using regular expressions
REPEAT() Repeat a string the specified number of times
REPLACE() Replace occurrences of a specified string
REVERSE() Reverse the characters in a string
RIGHT() Return the specified rightmost number of characters
RLIKE Synonym for REGEXP
RPAD() Append string the specified number of times
RTRIM() Remove trailing spaces
SOUNDEX() Return a soundex string
SOUNDS LIKE(v4.1.0) Compare sounds
SPACE() Return a string of the specified number of spaces
STRCMP() Compare two strings
SUBSTR() Return the substring as specified
SUBSTRING_INDEX() Return a substring from a string before the specified number of occurrences of the delimiter
SUBSTRING() Return the substring as specified
TRIM() Remove leading and trailing spaces
UCASE() Synonym for UPPER()
UNHEX()(v4.1.2) Convert each pair of hexadecimal digits to a character
UPPER() Convert to uppercase

คิดๆไปแล้ว MySQL Free Database ตัวนี้ผมว่าความสามารถไม่ใช่น้อยเลยนะครับ หลังๆ มี Function ต่างๆให้ใช้งานเพียบเลยว่างๆ จะหามาฝากอีกนะ

Java reflect มุมกลับแห่งชีวิตกับการสร้างและเรียกใช้แบบ Dynamic

แหะๆ ตั้งชื่อหัวข้อซะเวอร์ จริงๆไม่มีอะไรหรอกครับวันนี้แค่จะพูดถึงการใช้งานเกี่ยวกับ java.lang.reflect หลายท่านที่เป็นโปรอยู่แล้วคงคิดว่ามันธรรมดา ๆ แต่สำหรับผมแล้ว มันน่าทึ่งและนำไปใช้ประโยชน์ได้มากเลยครับ ไม่พูดมากไปดูตัวอย่างกันเลยละกัน

SampleClass
public class SampleClass {
private String prop1;
private String prop2;
public SampleClass(String prop1,String prop2){
this.prop1=prop1;
this.prop2=prop2;
}
public String getProp1() {
return prop1;
}
public void setProp1(String prop1) {
this.prop1 = prop1;
}
public String getProp2() {
return prop2;
}
public void setProp2(String prop2) {
this.prop2 = prop2;
}
}

Create Instance and Invoke Method by Name
Require Import
java.lang.reflect.Constructor , java.lang.reflect.Method
public void callByName(){
try{
//Create Instance from Constructor
Class c=SampleClass.class;
Constructor cons=c.getConstructor(new Class[]{String.class,String.class});
Object obj=cons.newInstance(new Object[]{"Java",""});

//invoke method getProp1 by send methodName
Method m=c.getMethod("getProp1", new Class[]{});
String value=(String)m.invoke(obj, new Object[]{});
System.out.println("Prop1 "+value);

//invoke method setProp2(String prop2) by send methodName
m=c.getMethod("setProp2", new Class[]{String.class});
m.invoke(obj, new Object[]{"PHP"});

m=c.getMethod("getProp2", new Class[]{});
value=(String)m.invoke(obj, new Object[]{});
System.out.println("Prop2 "+value);

//This Object is the same normal way

SampleClass sc=(SampleClass)obj;
System.out.println("Norm Prop1 "+sc.getProp1());
System.out.println("Norm Prop2 "+sc.getProp2());


}catch(Exception e){}
}

Result Value

Prop1 Java


Prop2 PHP


Norm Prop1 Java



Norm Prop2 PHP



เห็นมั้ยครับจะว่าไปมันก็คือการ Execute หรือ Create instance ผ่านทาง String input ธรรมดาๆ นี่แหละ แต่ถ้ารู้จักประยุกต์ใช้ให้ดีๆ จะเป็นประโยชน์มาก ลองดูครับ
หรือหากสงสัยหรือไ ม่รู้จะเอาไปใช้ทำอะไรก็ Comment ไว้ตรงนี้ได้น่ะ ผมเข้ามา Update ทุกวัน

Java Number Format ตัวอย่าง การจัด Format เบื้องต้นด้วย Java

เริ่มต้น โพสแรกไม่รู้จะหาอะไรมาฝาก เอาง่ายๆ พื้นๆละกันไปก่อน แต่ไม่ต้องห่วงถ้าประยุกต์ใช้ดีๆล่ะก็จะมีประโยชน์มากๆ
Java เบื้องต้นกับการจัด Format ตัวเลข และ วันที่ด้วย Java คงไม่ต้องพูดมากนะครับเอาตัวอย่างไปดูเลยละกัน

public void testFormat(){
try{
DecimalFormat dc=new DecimalFormat();
dc.applyPattern("###,###.00");
System.out.println("Format Number "+dc.format(150000));
System.out.println("Parse Number "+dc.parse("150,000.00"));

DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
System.out.println(df.format(new Date()));
Date date=df.parse("1/10/2008");
System.out.print(df.format(date));



}catch(Exception e){}

}


Result
Format Number 150,000.00

Parse Number 150000


1/7/2551



1/10/2008

ความหมายของ Parameter ต่าง ๆ ก็ดูตามนี้เลย น้า

  • SHORT is completely numeric, such as 12.13.52 or 3:30pm

  • MEDIUM is longer, such as Jan 12, 1952

  • LONG is longer, such as January 12, 1952 or 3:30:32pm

  • FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.

คำสั่ง Java Script ในการ Set Link Location เช่น Parent,selft หรือแม้แต่ ลง Frame

คำสั่ง Java Script ในการ Set Link Location target เช่น Parent,selft หรือแม้แต่ ลง Frame
บางครั้ง เรามีความจำเป็นที่จะต้อง ส่ง Link ด้วย Javascript ด้วยคำสั่ง
location = "url" ;
แต่การ ส่ง Link นั้นโดยมากเรา จะกำหนด target ว่าให้ส่ง Link แบบใด


- เปิดหน้าต่างใหม่ _blank
- อยู่หน้าตัวเอง _parent , _selft
- ส่งเข้า Frame โดยใส่ชื่อ Frame เข้าไป

ที่ผมสนใจคือส่งเข้า Frame ครับ มันสามารถ ประยุกต์ใช้ได้หลายแบบดี
ผมเลยจะขอนำเสนอแนวทางในการจัดการจะครับ

top.location.href = "page.htm";
parent.location.href = "page.htm";
self.location.href = "page.htm";

แบบนี้แหละครับ เราก็สามารถ ใช้ Java Scrip ส่ง Link โดย ที่ ใส่ ค่า target ไปได้ด้วย
หวังว่า เกร็ดเล็กๆ น้อยๆ เหล่านี้จะเป็นประโชยน์ให้กับเพื่อนๆ ได้ไม่มากก็น้อยนะคร้าบ