Tuesday, March 22, 2011

JDBC සමඟ MySQL එකට කලතා...

JDBC කියන්නේ මොකද්ද?

JDBC Technology කියන්නේ API එකක්. මේක J2SE වල වගේම J2EE වලත් ඇතුලත්.මේකෙන් Database වලට connection එක ලබාදීම සිදු කරනවා.

එතකොට MySQL කියන්නේ?

MySQL කියන්නේ Open Source Database Software එකක්.වර්තමාන ලෝකයේ ජනප්‍රියතම Open Source Database Software එක කියලා හැඳින්නුවත් වැරදි නැහැ.

Database වල ඇති data manipulate කරගන්න පුළුවන් java programs ලියන්න මේ JDBC නිසා පුළුවන්කම ලැබෙනවා.

මෙම JDBC Driver එක MySQL වෙබ් සයිට් එකෙන් download කරගන්න පුලුවන්.මෙන්න ලින්ක් එක..
http://www.mysql.com/products/connector/

දැනට භාවිත වන Connector version එක වන්නේ
mysql-connector-java-5.1.13-bin යන්නයි.

මෙම Connector එක අපේ NetBeans Project එකට add කරගන්නේ කොහොමද?

NetBeans Project එකේ Libraries යන්න right click කරන්න.ඉන් ලැබෙන menu එකෙන් add jar file යන්න select කරන්න.එවිට ලැබෙන dialog box එකෙන් Connector file එක ඇති තැන Browse කර add කරන්න.



Database එකක් manipulate කරන්නට JDBC භාවිතා කිරීමේදී ප්‍රධාන පියවර 5 ක් තියෙනවා.

1.JDBC driver classes load කරගැනීම සහ register කරගැනීම (භාවිතා කිරීමට බලාපොරොත්තු වන database server එක සඳහා )
2.database server එකට ඇති connection එක represent කිරීම සඳහා connection object එකක් ලබාගැනීම
3.Statement objects එකක් හෝ කීපයක් ලබාගැනීම
4.Statement objects භාවිතයෙන් database එක manipulate කිරීම
5.Database connection එක close කිරීම


පියවර 1.....

1.JDBC driver classes load කරගැනීම සහ register කරගැනීම සිදු කරන්නේ කොහොමද?

පහත code එක මඟින් සිදු කරන්නේ MySQL driver classes java program එක සමඟ register කර ගැනීමයි.මෙමඟින් MySQL server එකේ ඇති data, manipulate කිරීමේ හැකියාව program එකට ලබාදෙයි.

Class.forName("com.mysql.jdbc.Driver");

දෙවනුව database server එකේ URL එක ලබාදිය යුතුය.

String url= "jdbc:mysql://localhost:3306/myDB";

MySQL database engine එක network එකේ server එකක් ලෙස හැසිරෙයි. එමනිසා මේවා හඳුනා ගැනෙන්නේ URL එකක් මගිනි.

මෙහි 3306 ලෙස සඳහන් වන්නේ default port number එකයි.

myDB නම් database එක residing වී ඇත්තේ localhost එකේය.


පියවර 2....

Connection object එක ලබා ගැනීම....

Connection con= DriverManager.getConnection(url,"root","123");

මෙහිදී අදාල URL එකේ ඇති database සඳහා Connection එකක් ලබාගැනීම සිදු කරයි.

මෙහි root යනු username එකයි.
123 යනු password එකයි.


root යනු default adminstrator user වේ.

(administrator privileges ඇත.)

පියවර 3....

Statement objects ලබාගැනීම

මෙය සිදු කරන්නේ Connection Interface එකේ ඇති createStatement එක මඟිනි.

Statement stmp = con.createStatement();

මෙහි con යනු Connection object එකයි.

(Connection object එකෙන් සිදු කරන්නේ අදාල database එක සමඟ connection එකක් (session)නිර්මාණය කර ගැනීමයි.

Classes and Objects in Java

class: a set of same type of objects
a blueprint

ex: class Family
class Drama
class Student
class Button
class Connection
class HttpServlet

objects: instances of a class
attributes/variables : properties of those objects
methods: functions/ activities
static variable: common attribute to all objects

ex1)
public class Family {
float height;
String name;
static String surname ="perera";


public static void main(String[] args) {
Family father = new Family();
father.height = 6.0f;
father.name = "sunil";
System.out.println(father.height);
System.out.println(father.name+" "+surname);
System.out.println(surname);
father.sing();

Family son = new Family();
son.name="gayan";
System.out.println(son.surname);
System.out.println("full name: "+son.name+" "+surname);
}

public void sing(){
System.out.println("hehe");
}

}

class: Family
objects:father, son
variables/attributes: name, height
static variable: surname
method:sing


output
sunil perera
perera
hehe
perera
full name: gayan perera

Monday, March 14, 2011

Decorator Design Pattern

මෙහිදී සිදු කරන්නේ ප්‍රධාන වශයෙන් වන එක object එකක් නිර්මාණය කර, එයට වෙනස් වෙනස් දේ එක්කර modify කරන අවස්ථා විදහා පෑම සඳහා "decorator" objects නිර්මාණය කිරීමයි.

උදාහරණයක් ලෙස කොත්තුවක් පිලියෙල කිරීමේ අවස්ථාව සලකා බලමු.

මෙහිදී රොටී කොත්තුවක් පිලියෙල කිරීම මූලික පියවරයි.

එයට එක්කර "decorate" කලහැකි දේවල් ලෙස chicken, fish, cheese,egg සඳහන් කල හැක.

cheese සහ chicken යෙදූ රොටී කොත්තුවක මිල ගණනය කිරීම සඳහා java program එකක් ලිවීමට අවශ්‍ය යැයි සිතමු.

මුලින්ම රොටී object එකක් සාදා ගත යුතුය.

ඕනෑම කෑම වර්ගයක් සඳහා වන Meal class එක සාදා ගනිමු.

public abstract class Meal {
String Description="unknown meal";
public String getDescription(){
return Description;
}
public abstract double cost();
}


එහි sub class එකක් ලෙස RottyKottu class එක සාදා ගනිමු.

public class RottyKottu extends Meal{
public RottyKottu(){
Description="RottyKottu";
}
public double cost(){
return 100;
}
}

ඊලඟ පියවර වන්නේ decorators (cheese සහ chicken objects) සාදා ගැනීමයි. මේ සඳහා අපට මුලින්ම CondimentDecorator abstract class එක ලියා ගැනීමට සිදුවේ.


public abstract class CondimentDecorator extends Meal {
public abstract String getDescription();
}


දැන් chicken සහ cheese decorator objects සාදා ගනිමු.


1. public class Chicken extends CondimentDecorator {
Meal meal;
public Chicken (Meal meal){
this.meal=meal;
}
public String getDescription(){
return meal.getDescription()+",Chicken";}
public double cost(){
return 25.00+ meal.cost();
}
}

2. public class Cheese extends CondimentDecorator{
Meal meal;
public Cheese (Meal meal){
this.meal=meal;
}
public String getDescription(){
return meal.getDescription()+",Cheese";}
public double cost(){
return 35.00+ meal.cost();
}
}

දැන් ඇත්තේ අවසාන පියවරයි. එනම් cheese සහ chicken යෙදූ කොත්තුවක මිල ගණනය කිරීම සඳහා program එක ලිවීමයි.

public class kottu1 {
public static void main(String[] args) {
Meal m1 = new RottyKottu();
System.out.println(m1.getDescription()+"Rs"+m1.cost());
m1=new Cheese(m1);    //m1 gets decorated by cheese
m1=new Chicken(m1);  //m1 gets decorated by chiken
System.out.println(m1.getDescription()+"Rs"+m1.cost());

}
}

MVC Architecture

මේ design pattern එකෙන් කරන්නේ web application එකක ඇති components කොටස් තුනකට වෙන් කිරීමයි.
Model, View සහ Controller යනුවෙන්.

මේ component එක එකට පැවරුනු වගකීම් තියෙනවා.මේවා එකිනෙකින් ස්වායත්තයි. එක component එකක වෙනස් කිරීමකින් අනිත් component එකට බලපෑමක් නැත.(ඇත්නම් ඒ ඉතා සුළු වශයෙනි.)

මේ එක එක component වලට පැවරෙන වගකීම් මොනවාද?

Model

මෙය වගකියනු ලබන්නේ database එකෙන් data ලබාදීමට සහ database එකට data, save කිරීමටයි. සියළු business logic implement කරන්නේ මෙය තුලයි.

.User විසින් View එක හරහා ඇහුල් කරන data,db එකට save කිරීමට පෙර Model කොටසේදී check කිරීම කරනු ලබයි.Model එකට අයිති කොටස් වන්නේ

Data access,
Data Validation සහ
Data saving logic

යන ඒවායි.

View

application එකේ user ට පෙනෙන කොටස නිරූපනය කරන්නේ view මඟිනි.

-user ගෙන් input ලබාගැනීම
-controller එක වෙත requests, send කිරීම
-controller එක මඟින් responses ලබාගැනීම
-user ට output එක display කිරීම

මෙහි කාර්ය භාරය වෙයි.

View Component එකට අයිති වෙන්නේ..

-HTML
-JSP
-Custom tag libraries සහ
-Resourse files ආදියයි.

Controller


Model සහ View අතර අතරමැදියා ලෙස කටයුතු කරන්නේ Controller එකයි.

Client ගෙන් request එක view එක හරහා ලැබුනු විට, model එකේ අදාල business logic එක execute කරවා, ලැබෙන output එක view හරහා user ට ලබාදීමේ වගකීම පැවරෙන්නේ controller එකටයි.


Controller component එකට අයිති වන්නේ
-Action
-Action Servlet
-Action Form සහ
-Struts-config.xml

ආදියයි.

Saturday, March 5, 2011

Final භාවිතය

Final

final කියන දෙයින් අදහස් කරන්නේ අවසානය (end) හෝ වෙනස් කල නොහැකි බව (immutability)

final යෙදිය හැකි තැන්: classes, methods, member variables, method local variables

final classes

final classes, extend කල නොහැක.
final class යන්නෙන් අදහස් කරන්නේ, එම class එක inheritance hierarchy එකේ අවසානය බවයි.

final class එකක abstract methods තිබිය නොහැක.

අනිත් ඕනෑම වර්ගයක methods තිබිය හැක.

ex) class A{}
final class B extends A{}

class B තව දුරටත් extend කල නොහැක.

final methods

-static හෝ non-static (instance) methods වලට final යෙදිය හැක.
-final methods, override කල නොහැක.
-නමුත් final methods, sub classes වලට inherit කල හැක.

final variables