Skip to main content

jaxb example

Download <strong>eclipselink.jar</strong>,

 Create a java project and add a Person.java class in it.Now create a properties file jaxb.properties.

 <strong>jaxb.properties</strong>

 javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

 <strong>Person.java</strong>

 package pck;


 import javax.xml.bind.JAXBContext;

 import javax.xml.bind.Marshaller;

 import javax.xml.bind.annotation.XmlElement;

 import javax.xml.bind.annotation.XmlRootElement;

 import net.sf.json.JSONException;

 @XmlRootElement (name="Person")

 public class Person {

 @XmlElement (name="age")

 public int age;

 @XmlElement (name="name")

 public String name;

 public static void main(String[] args) throws JSONException, Exception {

 Person me = new Person();

 me.age = 27;

 me.name = "Biswajit";

 int PRETTY_PRINT_INDENT_FACTOR = 4;

 JAXBContext jc = JAXBContext.newInstance(Person.class);

 Marshaller marshaller = jc.createMarshaller();

 marshaller.setProperty("eclipselink.media-type", "application/json");

 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

 marshaller.marshal(me, System.out);

 }

 }


 run the java application.


 result


 {

 "Person" : {

 "age" : 27,

 "name" : "Biswajit"

 }

 }

Comments

Popular posts from this blog

jdbc mysql example in java

Here’s an example to show you how to connect to MySQL database via a JDBC driver.To connect to MySQL from Java, you have to use the JDBC driver from MySQL. You find the latest MySQL JDBC driver under the following URL:http://dev.mysql.com/downloads/connector/j.
Create a example database and named it test.Now create a a table name it user. Create table user ((userid INT NOT NULL AUTO_INCREMENT,
username VARCHAR(30) NOT NULL, PRIMARY KEY (userid)); Now add the record in your table; Now create a project in eclipse and mysql-connector jar in its classpath.package com.test;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; publicclass JavaJDBCExample { private Connection connect = null; private Statement statement = null; private ResultSet resultSet = null; publicvoid readDatabase() { try { // this will load the MySQL driver Class.forName("com.mysql.jdbc.Driver"); // setup the connection wi…

singleton design pattern

Java Singleton design pattern is one of the design pattern which suggest that only one instance of a Singleton object is created by the JVM. This is useful when exactly one object is needed to coordinate actions across the system Example: public class Singleton{ privatestatic Singleton singleton = null; private Singleton(){ } publicstaticsynchronized Singleton getInstance( ) { if(singleton == null) { singleton = new Singleton(); } returnsingleton; } publicvoid test( ) { System.out.println("test"); } @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub returnnew CloneNotSupportedException(); } } publicclass SingletonTest { publicstaticvoid main(String[] args){ Singleton singleton = Singleton.getInstance(); try{ Singleton

Tomcat configuration

1. How to deploy web application in tomcat 
 Create a war file and deploy it in tomcat->webapps folder.Restart tomcat and write localhost:8080/YOURWEBAPPNAME. 
 2. Add jndi in tomcat 
 <Context>
 <Resource name="jdbc/myDB" auth="Container"
 type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000"
 username="admin" password="admin"
 driverClassName="com.mysql.jdbc.Driver"
 url="jdbc:mysql://localhost:3306/javatest"/> 
 </Context>
 in web.xml 
 <resource-ref>
 <description>DB Connection</description>
 <res-ref-name>jdbc/myDB</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
 </resource-ref> 
 3. Add user with manager role in tomcat   Tomcat-users.xml 
 <tomcat-users>
 <role rolename="manager"/>
 <user username="admin"…