Archive for May, 2008

Java-XML in 3 minutes

20 May 2008

XML is a clear and concise way to describe data.

Let’s see an example:

<?xml version="1.0" encoding="UTF-8"?>
<message id="123">
    <from>Nikos</from>
    <to>Sofia</to>
    <text>Hello from XML!</text>
</message>

This simple document represents a “message”.

<message>
    ...
</message>

The message has an attribute called @id.

<message id="...">
    ...
</message>

It has also three children.

<from> ... </from>
<to> ... </to>
<text> ... </text>

Step 1. Download dom4j

To download dom4j go to dom4j.org → Download → Download the current release SourceForge → dom4j-1.6.1.jar

Note:

If you are using Java 1.5 download dom4j-1.5.2.jar, and
If you are using Java 1.4 download dom4j-1.4.zip; it has a dom4j-full.jar inside.

Step 2. Place the jar in the classpath

In Eclipse: Project → Java Build Path → Libraries → Add External JARs…

In Netbeans: File → ”Project” Properties → Libraries → Add JAR/Folder

In JDeveloper 11g: Tools → Project Properties… → Libraries and Classpath → Add JAR/Directory…

Step 3. Create the XML document

	// Create the document
	Document document = DocumentHelper.createDocument();
	// Add the root
	Element root = document.addElement("message").addAttribute("id", "123");
	// Add the "from" element
	root.addElement("from").addText("Nikos");
	// Add the "to" element
	root.addElement("to").addText("Sofia");
	// Add the "text" element
	root.addElement("text").addText("Hello from XML!");

That’s all!

Step 4. Save the XML document

// Make a pretty output
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		format.setTrimText(false);
// Save it
		XMLWriter writer = new XMLWriter(new FileWriter("C:/message.xml"), format);
		writer.write(document);
		writer.close();

Step 5. Read the XML document

// Locate the file
URL url = new File("C:/message.xml").toURI().toURL();
// Parse the document
Document document = new SAXReader().read(url);

Step 6. Get the information

// Get the root element
Element root = document.getRootElement();
// Get the id attribute
root.attributeValue("id");
// Get the from element
Element from = root.element("from");
if (from != null) {
   from.getText();
}

Step 7. Use some XPATH

// Get the root element
Element root = document.getRootElement();
// Get the id attribute
root.valueOf("@id");
// Get the from element
Node from = root.selectSingleNode("./from");
if (from != null) {
   from.getText();
}

A good XML tutorial can be found at w3schools.com.

Thank you.

Java-XML dalam 3 menit

20 May 2008

Selamat siang saudara2 dari Indonesia. XML adalah cara yang jelas untuk menggambarkan informasi.

Mari kita lihat contoh ini:

<?xml version="1.0" encoding="UTF-8"?>
<pesan nomor="123">
    <dari>Halim</dari>
    <kepada>Selvi</kepada>
    <isinya>Yo belajar yo!</isinya>
</pesan>

Dokumen sederhana ini menggambarkan sebuah pesan.

<pesan>
    ...
</pesan>

Pesan ini mempunyai satu attribute bernama @nomor.

<pesan nomor="...">
    ...
</pesan>

Pesan ini juga ada tiga anak.

<dari> ... </dari>
<kepada> ... </kepada>
<isinya> ... </isinya>

Langkah 1. Mendapatkan dom4j

Mendapatkan dom4j dari dom4j.org → Download → Download the current release SourceForge → dom4j-1.6.1.jar

Kalau sedang pakai Java 1.5 pilih dom4j-1.5.2.jar. Untuk Java 1.4 pakai dom4j-full.jar yang ada di dalam dom4j-1.4.zip

Langkah 2. Taruh jar di classpath

Untuk masukan jar di classpath di…

Eclipse: Project → Java Build Path → Libraries → Add External JARs…

Netbeans: File → ”Project” Properties → Libraries → Add JAR/Folder

JDeveloper 11g: Tools → Project Properties… → Libraries and Classpath → Add JAR/Directory…

Langkah 3. Membuat XML dokumen

	// Membuat dokumen
	Document dokumen = DocumentHelper.createDocument();

	// Tambah elemen pertama
	Element pertama = dokumen.addElement("pesan").addAttribute("nomor", "123");

	// Tambah elemen "dari"
	pertama.addElement("dari").addText("Halim");
	// Tambah elemen "kepada"
	pertama.addElement("kepada").addText("Selvi");
	// Tambah element "isinya"
	pertama.addElement("isinya").addText("Yo belajar yo!");

Sudah!

Langkah 4. Menyimpan XML dokumen

	// Pilih format yang rapi
	OutputFormat format = OutputFormat.createPrettyPrint();
	format.setEncoding("UTF-8");
	format.setTrimText(false);

	// Menyimpan
	XMLWriter writer = new XMLWriter(new FileWriter("C:/pesanku.xml"), format);
	writer.write(document);
	writer.close();

Langkah 5. Membaca XML dokumen

	// Menemukan file
	URL url = new File("C:/pesanku.xml").toURI().toURL();

	// Membaca XML dokumen yang ada di dalam
	Document dokumen = new SAXReader().read(url);

Langkah 6. Mengambil informasi

	// Ambil elemen pertama (pesan)
	Element pertama = document.getRootElement();

	// Ambil attribute "nomor"
	pertama.attributeValue("nomor");

	// Ambil elemen "dari"
	Element dari = pertama.element("dari");
	if (dari != null) {
		dari.getText();
	}

Langkah 7. Pakai sedikit XPATH

	// Ambil elemen pertama (pesan)
	Element pertama = document.getRootElement();

	// Ambil attribute "nomor"
	pertama.valueOf("@id");

	// Ambil elemen "dari"
	Node dari = pertama.selectSingleNode("./dari");
	if (dari != null) {
		dari.getText();
	}

Pelajaran yang bagus tentang XML ada di w3schools.com.

Terima kasih.

Java-XML σε 3 λεπτά

20 May 2008

Η XML είναι ένας σαφής και ξεκάθαρος τρόπος να περιγράφουμε δεδομένα.

Ας δούμε ένα παράδειγμα:

<?xml version="1.0" encoding="UTF-8"?>
<message id="123">
    <from>Νίκος</from>
    <to>Σοφία</to>
    <text>Καλημέρα!</text>
</message>

Αυτό το απλό κείμενο αναπαριστά ένα μήνυμα.

<message>
    ...
</message>

Παρατηρούμε ότι το μήνυμα έχει την ιδιότητα @id,

<message id="...">
    ...
</message>

και 3 παιδιά.

<from> ... </from>
<to> ... </to>
<text> ... </text>

Βήμα 1. Κατέβασε το dom4j

Πάρε το dom4j από το dom4j.org → Download → Download the current release SourceForge → dom4j-1.6.1.jar

Αν χρησιμοποιείς Java 1.5 κατέβασε το dom4j-1.5.2.jar. Για Java 1.4 κατέβασε το dom4j-1.4.zip: μέσα του είναι το dom4j-full.jar

Βήμα 2. Βάλε το jar στο classpath

Στο Eclipse: Project → Java Build Path → Libraries → Add External JARs…

Στο Netbeans: File → ”Project” Properties → Libraries → Add JAR/Folder

Στο JDeveloper 11g: Tools → Project Properties… → Libraries and Classpath → Add JAR/Directory…

Βήμα 3. Φτιάξε το XML

	// Φτιάξε το κείμενο
	Document document = DocumentHelper.createDocument();
	// Όρισε τον πατέρα
	Element root = document.addElement("message").addAttribute("id", "123");
	// Όρισε το "from"
	root.addElement("from").addText("Νίκος");
	// Όρισε το "to"
	root.addElement("to").addText("Σοφία");
	// Όρισε το "text"
	root.addElement("text").addText("Καλημέρα!");

Τόσο απλά!

Βήμα 4. Αποθήκευσε το XML

// Φρόντισε να έχει κωδικοποίηση UTF-8
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		format.setTrimText(false);
// Αποθήκευσέ το
		XMLWriter writer = new XMLWriter(new FileWriter("C:/message.xml"), format);
		writer.write(document);
		writer.close();

Βήμα 5. Διάβασε το XML

// Βρες το αρχείο
URL url = new File("C:/message.xml").toURI().toURL();
// Διάβασε το κείμενο
Document document = new SAXReader().read(url);

Βήμα 6. Πάρε την πληροφορία

// Πάρε τον πατέρα
Element root = document.getRootElement();
// Πάρε την ιδιότητα id
root.attributeValue("id");
// Πάρε το παιδί from
Element from = root.element("from");
if (from != null) {
   from.getText();
}

Step 7. Και λίγο XPATH

// Πάρε τον πατέρα
Element root = document.getRootElement();
// Πάρε την ιδιότητα id
root.valueOf("@id");
// Πάρε το παιδί from
Node from = root.selectSingleNode("./from");
if (from != null) {
   from.getText();
}

Ένα καλό μάθημα XML βρίσκεται στο w3schools.com.

Ευχαριστώ.

MySQL σε 3 λεπτά

17 May 2008

Αυτό το άρθρο είναι ένας γρήγορος οδηγός MySQL.

Βήμα 1. Κατέβασε την MySQL

Κατέβασε την MySQL από το mysql.com → downloads → MySQL Community Server → Windows ZIP/Setup.EXE (x86) → Pick a mirror → No thanks, just take me to the downloads → Greece [National Technical University of Athens] HTTP → mysql-5.0.51b-win32.zip

Βήμα 2. Εγκατέστησε την MySQL

Η ΜySQL εγκαθίσταται πολύ εύκολα. Απλά ακολούθησε της οδηγίες βήμα προς βήμα.

Αφού ολοκληρωθεί η εγκατάσταση, ανοίγει ένας οδηγός ρυθμίσεων: Στο παράθυρο με τις γλώσσες διάλεξε “Best Support For Multilingualism”. Επίσης καλό είναι σημειώσεις τον κωδικό του διαχειριστή (root password).

Βήμα 3. Ξεκίνησε την MySQL

Για να ξεκινήσεις την MySQL πήγαινε Start → Programs → MySQL → MySQL Server 5.0 → MySQL Command Line Client → και δώσε τον κωδικό του διαχειριστή.

Για να ξεκινήσεις τον server σε παλαιότερη έκδοση των Windows (π.χ. Millenium) πήγαινε Start → Run και γράψε "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld" --console. Έπειτα Start → Programs → MySQL → MySQL Server 5.0 → MySQL Command Line Client → και δώσε τον κωδικό του διαχειριστή.

Βήμα 4. Φτιάξε το σχήμα

Για να φτιάξεις μια βάση απλά γράψε create database hello;

Για επιβεβαίωση δώσε show databases; κι έπειτα use hello;

Βήμα 5. Φτιάξε έναν πίνακα

Αυτή η απλή βάση έχει να κάνει με άτομα. Για κάθε άτομο μας ενδιαφέρει 1. το όνομα, 2. η ηλικία και 3. ένας μοναδικός αριθμός (identifier = αναγνωριστικό = κλειδί).

Για να φτιάξεις έναν πίνακα γράψε CREATE TABLE persons (name VARCHAR(255), age SMALLINT, id BIGINT AUTO_INCREMENT, primary key (id));

Για επιβεβαίωση show tables; κι έπειτα describe persons;

Βήμα 6. Βάλε δεδομένα

Για να βάλεις δεδομένα στον πίνακα απλά γράψε INSERT INTO persons (name, age) VALUES ('Michael Jordan', 45);

Ας βάλουμε την Jennifer Lopez: INSERT INTO persons (name, age) VALUES ('JLo', 39);

Και βέβαια τον Al Pacino: INSERT INTO persons (name, age) VALUES ('Al Pacino', 68);

Βήμα 7. Πάρε δεδομένα

Για πάρεις όλα τα άτομα: SELECT * FROM persons;

Για να πάρεις όλα τα άτομα άνω των 50 ετών: SELECT * FROM persons WHERE age > 50;

Για να πάρεις όλους τους “Μιχάληδες”: SELECT * FROM persons WHERE name LIKE '%Michael%';

Βήμα 8. Άλλαξε μια εγγραφή

Η Jennifer Lopez ζήτησε προσωπικά να αλλάξουμε την ηλικία της. Πρώτα μαθαίνουμε το id SELECT id FROM persons WHERE name='JLo';
και μετά αλλάζουμε της ηλικία ως εξής UPDATE persons SET age=28 WHERE id=2;

Για να δούμε ότι όλα πήγαν καλά SELECT * FROM persons

Βήμα 9. Βγες από την MySQL

Για να βγεις απλά γράψε exit

Για να κλείσεις τον server σε παλαιότερη έκδοση των Windows (π.χ. ΜΕ) πήγαινε Start → Run
και γράψε "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqladmin" shutdown

Όλες οι εξελίξεις για την MySQL βρίσκονται στο mysql.com, πλήρεις οδηγίες στο Start → Programs → MySQL → MySQL Server 5.0 → MySQL Manual – Table Of Contents. Τέλος, ένα ωραίο μάθημα SQL είναι το w3schools.com.

Ευχαριστώ.

MySQL in 3 minutes

9 May 2008

This is a quick-start MySQL tutorial. You’ll create a simple database and do some useful read / write operations.

Step 1. Download MySQL

Download MySQL from mysql.com → downloads → Take me to the community downloads! → Scroll down to Windows downloads → Windows Essentials (x86) → Pick a mirror → No thanks, just take me to the downloads → Greece [National Technical University of Athens] HTTP → mysql-essential-5.1.31-win32.msi

Step 2. Install MySQL

Installing MySQL is very easy. Just follow the steps one-by-one. After installation completes, make sure to select the option “Configure the MySQL Server now” and click “Finish”.

Make sure the "Configure the MySQL Server now" is selected.

In the configuration wizard keep pressing next until you the dialog about character encoding. Here, you should choose “Best Support For Multilingualism”.

When defining default character set, make sure "Best Support for Multilingualism" is selected.

Press next; the last important setting is about security. Provide a root password and write it on a paper.

Step 3. Start MySQL

In order to start using MySQL go to Start → Programs → MySQL → MySQL Server 5.1 → MySQL Command Line Client → and give the root password from the previous step.

To start MySQL in an older version of Windows (e.g. ME) go to Start → Run and write "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld" --console. Then go to Start → Programs → MySQL → MySQL Server 5.1 → MySQL Command Line Client → and give the root password.

Step 4. Create a database

To create a database in MySQL just type:

create database hello;

For confirmation state

show databases;

and then:

use hello;

Step 5. Create a table

This simple database is about persons. For every person we are interested about 1. the name, 2. the age and 3. a unique number (identifier).

To create a table in MySQL write

CREATE TABLE persons (name VARCHAR(255), age SMALLINT,
             id BIGINT AUTO_INCREMENT, primary key (id));

To confirm write,

show tables;
Displaying the tables of a database

Displaying the tables of a database

and then:

describe persons;

Step 6. Store information (insert data)

To insert some data to your table simply say:

INSERT INTO persons (name, age) VALUES ('Michael Jordan', 45);

Let’s store Jennifer Lopez also:

INSERT INTO persons (name, age) VALUES ('JLo', 39);

How about Al Pacino?

INSERT INTO persons (name, age) VALUES ('Al Pacino', 68);

Step 7. Get information (select data)

To get all persons:

SELECT * FROM persons;
Selecting the data of a table

Selecting the data of a table

To get all persons that are older than 50 years:

SELECT * FROM persons WHERE age > 50;

To get all ‘Michaels’:

SELECT * FROM persons WHERE name LIKE '%Michael%';

Step 8. Change some info (update data)

We have the business requirement to change the age of Jennifer Lopez! To accomplish this task, we may use her id,

UPDATE persons SET age = 29 WHERE id = 2;

or directly her name:

UPDATE persons SET age = 29 WHERE name = 'JLo';

To confirm that her age is now proper:

SELECT * FROM persons;
Updating a value in the table

Updating a value in the table

Step 9. Exit MySQL

To exit MySQL simply type

exit

To shut down MySQL in older versions of Windows (e.g. ME) go to Start → Run
and write "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqladmin" shutdown

All the action about MySQL is at mysql.com. Finally, you may get more info about SQL at w3schools.com.