December 7, 2005

This pic explains why ruby on rails.

Posted in Java, Uncategorized at 9:31 am by Frank

I always choose python as my script language but ruby on rails is very hot these days, so many people turn their head to ruby, me included. Some guys said that ruby is more oo then python. After reading a few ruby docs, I found that ruby is kinda like basic, use end to mark the ending of a block, and its syntax seems very weird to me. So I didn’t go with it any deeper. But this picture seems very funny and persuadable, I can’t help looking through ruby docs again.

November 23, 2005

Hibernate results into JTable

Posted in Uncategorized at 6:02 am by Frank

We can retrieve a List of Objects from hibernate. To show the Objects in JTable, just inherited a class from AbstractTableModel and supply getRowCount() ,getColumnCount() and getValueAt() methods. And the getColumnName method is often need to be overrided to show the column name you want, not the A,B,C form.

I have a PERSON table in some database and its corresponding persistent class Person, it’s shown below, and the class PersonTableModel is the AbstractTableModel’s descendant here.
24
Source of PersonTableModel.java:

import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
import javax.swing.table.AbstractTableModel;

public class PersonTableModel extends AbstractTableModel
{
    private static final long serialVersionUID = 6105842825518764825L;
    private ArrayList<Person> PersonList;
    
    public PersonTableModel()
    {
        super();
        SessionFactory sf=new Configuration().configure().buildSessionFactory();
        Session session=sf.openSession();
        
        Query q=session.createQuery("from Person");
        PersonList=new ArrayList<Person>(q.list());
        
        session.close();
        sf.close();
    }

    public int getRowCount()
    {
        return PersonList.size();
    }

    public int getColumnCount()
    {
        return 5;
    }

    public Object getValueAt(int rowIndex, int columnIndex)
    {
        Person p=PersonList.get(rowIndex);
        Object[] values=new Object[]{p.getId(),p.getFirstname(),p.getLastname(),
                p.getAge(),p.getDescription()};
        return values[columnIndex];
    }

    @Override
    public String getColumnName(int column)
    {
        String[] columnNames=new String[]{"id","FirstName","LastName","Age","description"};
        return columnNames[column];
    }
}

Then create a PersonTableModel Object and set the JTable’s model to it, the data from hibernate will be represented in JTable, as below:
25

November 22, 2005

Streams in .net framework(1)

Posted in Uncategorized at 2:58 am by Frank

Basic streams of .net framework are defined in System.IO. .Net streams is much simpler than jdk streams. Class diagram of the System.IO is shown below, Including classes manipulate files and directories, and some IO exception classes.

15

Class Stream is the basic stream class in .net. It has 3 descents. (There are othere streams inherite from Stream such as SslStream, GzipStream, but they are not in the namespace System.IO).

FileStream: Associated to a file, supports random access, you can seek to any position of the file and read data from it or write data into it.
BufferedStream:It add buffering to another stream, so the read/write can be more efficient just as BufferedStream of jdk does.
MemoryStream:It indicates some data which is totally in memory. It’s often used as temporary buffer.

Writting to a stream

Stream and its descents can write byte array types. BinaryWrite and StreamWrite let you write various types of data, the former write as binary, the latter write as textual. The codes below demonstrate how to write to streams in the 3 ways.
//Create a file stream
FileStream fs=new FileStream(“data.dat”,FileMode.Create,FileAccess.ReadWrite);

//write some bytes in it.
byte[] arr=System.Text.Encoding.Default.GetBytes(“these are written as byte[]rn”);
fs.Write(arr,0,arr.Length);

//write binary data to file
BinaryWriter bw=new BinaryWriter(fs);
bw.Write(“this line is write as binary format.rn”);
bw.Close(); //the file is now closed

//open the file again and append text data into it
StreamWriter sw=new StreamWriter(“data.dat”,true);
sw.WriteLine(“this line is written as text format.”);
sw.Close();

After executing the codes above, the file “data.dat” will be as below:
these are written as byte[]
&this line is write as binary format.
this line is written as text format.

Reading from a stream

Corresponding to writing, Stream can read byte[] type data, BinaryReader and StreamReader read most of the basic types in binary or in textual respectively. Since reading and writing are symmetrical, I don’t give any code examples.

November 21, 2005

just try spaces

Posted in Uncategorized at 11:25 am by Frank

the words below has 4 spaces before
asdfasfdasfda

Follow

Get every new post delivered to your Inbox.