Wednesday, September 28, 2011

My things of English

This is my new software that I'm developing now. That works with Hibernate so that controlling the data access to database of application. It's developed with Java. I've just powered by Google translator^^

Check it out!

Friday, September 23, 2011

Introduction to Hibernate - Part 1

Hibernate is a framework that helps to work with data access in databases and it improves the use of java objects persistence. The official page is http://www.hibernate.org/. Check out!

We have to look for our drive to the database that we're working. For example, I work with MySQL5 then I have to choose a Hibernate SQL dialect for that: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/session-configuration.html#configuration-optional-dialects


We need mapping our classes so that accessing to entities or tables of our working data base.
They exist two options to work in Hibernate for mapping:
  • Using annotations (It's my favorite)
  • Using XML file (I won't show it there. I don't like this)

So that Starting to work, we have to go to the page of downloads (http://www.hibernate.org/downloads) and  to download the last release pack. Inside of this we find a lot of libraries, but at this moment we need these: 
  • hibernate-core-[version number].jar (It's the heart of Hibernate)
  • hibernate-jpa-[version number]-api[version number].jar (this is for to use annotations)
  • slf4j-api-[version number].jar (it's a log for java framework: http://www.slf4j.org/ and hibernate uses a log)
  • slf4j-simple-[version number].jar
  • mysql-connector-java-[version number].jar (it's mysql driver for java: http://www.mysql.com/downloads/mirror.php?id=403046#mirrors)

I work with Eclipse IDE and I'll show the image captures there. (http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/indigor)

Let's go to start!
I always create a folder in my java projects to keep use libraries and I add them to build path.
Next, we create 4 packages: dao, model, utils and test.
Then we need to create a configuration file to Hibernate:



Now, we need to copy this in the hibernate.cfg.xml (it's the configuration file to Hibernate):

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


	
 	    
	    com.mysql.jdbc.Driver
	    jdbc:mysql://localhost/yourdatabase
	    youruser
	    yourpassword
	    
	    
	    1
	    
	    
	    org.hibernate.dialect.MySQL5Dialect
	    
	    
	    true
	    
	    
	    <mapping class="model.Client"/>
	    <mapping class="model.Product"/>
	





I'm going to explain some parts of this xml file: The first one, they're the connection parameters that you have to know:
	    com.mysql.jdbc.Driver
	    jdbc:mysql://localhost/yourdatabase
	    youruser
	    yourpassword
  • yourdatabase: it's the name of your database
  • youruser: it's the name of your user that you use to connect to the database
  • yourpassword: it's the password of your user

We're mapping our class so that adding java objects persistence here. Client and product classes are in model package:
	    <mapping class="model.Client"/>
	    <mapping class="model.Product"/>
Eventually, we have done Hibernate initial configuration. In the next part, I'll continue to explain how to access to the database very easy, because Hibernate help us!!!!

Have a nice week!

Welcome - How to copy files in Java

Welcome to my site. This is the first post and I want to write how to copy files in Java.


We need a source file and a destination file:
File source = new File("source.mp3");
File destination = new File("destination.mp3");
Next, we have to create an input stream and an output stream, reading the input file and writing in the output file. This is the code:
try {
 InputStream in = new FileInputStream(source);
 OutputStream out = new FileOutputStream(destination);

 // to read while data exist in the input stream
 byte[] buf = new byte[1024];
 int len;

 while ((len = in.read(buf)) > 0) {
  out.write(buf, 0, len);
 }

 // to close opened files
 in.close();
 out.close();

 } 
 catch (FileNotFoundException e1) {
  e1.printStackTrace();
 }
 catch (IOException e2) {
  e2.printStackTrace();
 }
}
I hope you like this and I've helped, Regards!