Pages

Enable Remote access to Mysql Server [Ubuntu]

When your installing mysql from apt get you would face this problem. Actually its not a problem but a security for our local mysql server to prevent access from others. :)

If you want enable the remote access here are the steps. Just 3 steps

Step 1
Edit the mysql configuration file
 sudo gedit /etc/mysql/my.cnf  

Comment the following line
 bind-address = 127.0.0.1  
  • To comment add # 
Save the my.cnf file.

Step 2
Restart the mysql server
 sudo /etc/init.d/mysql restart  

Step 3
Grant database access to particular IP address
 GRANT ALL ON foo.* TO bar@'202.54.10.20' IDENTIFIED BY 'PASSWORD';  

That's IT. You just unsecure your database :P

Advance JAXB - XML into Java Map

In this article I will show you how to decode XML into a java Hashmap. So always there are multiple ways. But I will show you how you can do this in a simple and more responsible way.

JAXB is referred as Java Architecture for XML Binding. In this tutorial we are only use annotation for that. We don't require any third party libraries for this because java provide all the additional JAXB annotation for us.

Here is some good article about JAXB

OK first thing first.
Lets create a XML file.
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
 <bookStore>  
   <books>  
     <book bookId="1">  
       <name>book1</name>  
       <author>author1</author>  
     </book>  
     <book bookId="2">  
       <name>book2</name>  
       <author>author2</author>  
     </book>  
   </books>  
 </bookStore>  

Here is the sample and most popular tutorial XML :P

So after this we will write three domain classes for this and a adapter class to get the map.
Yes we need to have 3 domain java classes -> BookStore, Books and Book
and BookAdapter java class to create the hashmap.

Here is the BookStore class.
 @XmlRootElement(name = "bookStore")  
 public class BookStore {  
   private HashMap<String,Book> books;  
   @XmlElement  
   @XmlJavaTypeAdapter(BookAdapter.class)  
   public HashMap<String, Book> getBooks() {  
     return books;  
   }  
   public void setBooks(HashMap<String, Book> books) {  
     this.books = books;  
   }  
 }  

Some basic JAXB annotation are there with @XmlJavaTypeAdapter. We use this adapter class to marshal and unmarshal the xml.
So here is the Books java class
 public class Books {  
   private ArrayList<Book> books;  
   @XmlElement(name = "book")  
   public ArrayList<Book> getBooks() {  
     return books;  
   }  
   public void setBooks(ArrayList<Book> books) {  
     this.books = books;  
   }  
 }  

And here is the Book java class
 public class Book {  
   private String bookId;  
   private String name;  
   private String author;  
   @XmlAttribute  
   public String getBookId() {  
     return bookId;  
   }  
   public void setBookId(String bookId) {  
     this.bookId = bookId;  
   }  
   @XmlElement  
   public String getName() {  
     return name;  
   }  
   public void setName(String name) {  
     this.name = name;  
   }  
   @XmlElement  
   public String getAuthor() {  
     return author;  
   }  
   public void setAuthor(String author) {  
     this.author = author;  
   }  
 }  

So the basic are done. Now we have to write the adapter class to do the magic(actually there is no magic at all.)

Here is the BookAdapter java class
 public class BookAdapter extends XmlAdapter<Books,Map<String,Book>> {  
   // we only going to change this method. Because we are only doing un marshaling  
   // xml into map  
   @Override  
   public Map<String, Book> unmarshal(Books v) throws Exception {  
     Map<String, Book> map = new HashMap<String, Book>();  
     for(Book book : v.getBooks()){  
       map.put(book.getBookId(),book);  
     }  
     return map;  
   }  
   @Override  
   public Books marshal(Map<String, Book> v) throws Exception {  
     return null;  
   }  
 }  

So in here we only consider about unmarshal because our objective is to decode xml into hashmap.

And finally here is the App class with main method
 public class App {  
   public static void main(String[] args) {  
     File file = new File("book-store.xml");  
     try {  
       JAXBContext jaxbContext = JAXBContext.newInstance(BookStore.class);  
       Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();  
       BookStore bookStore = (BookStore) unmarshaller.unmarshal(file);  
       HashMap<String, Book> bookMap = bookStore.getBooks();  
       for (Map.Entry<String, Book> entry : bookMap.entrySet()) {  
         System.out.println("Key = " + entry.getKey());  
         System.out.println("value = " + entry.getValue().getAuthor() + " " + entry.getValue().getName());  
         System.out.println("=========================");  
       }  
     } catch (JAXBException e) {  
       e.printStackTrace();  
     }  
   }  
 }  

Full Project : https://github.com/rajithd/jaxb-sample