Pages

Thursday, November 8, 2012

Developing a RESTful Java Web service with Jersey API in Eclipse Indigo using maven

Hi folks,
This article posted in another blog of mine gives step-by-step procedure of developing a RESTful java web service in eclipse using maven with Jersey reference implementation.

http://kausalmalladi.blogspot.in/2012/11/developing-restful-java-web-service-in.html

Hope it helps you. Please feel free to post any queries/comments.

Wednesday, September 5, 2012

Implementing LinkedList in Java!

Hi All,
Many people say that implementing LinkedList in Java is not possible. I tried to implement it and thought of sharing so that someone would find it useful.

You can try out the following code. Please feel free to post your ideas/questions w.r.t. this in comments section.



public class LinkedList{
Node start;
int count;
public void add(int data){
Node n=new Node();
n.data=data;
n.next=null;
if(start==null){
start=n;
}
else{
Node temp=new Node();
temp=start;
while(temp.next!=null){
temp=temp.next;
}
temp.next=n;
}
count++;
}
public void display(){
Node n=new Node();
if(start!=null){
n=start;
System.out.println(n.data);
while(n.next!=null){
n=n.next;
System.out.println(n.data);
}
}
}
}


public class LinkedListImpl{
public static void main(String[] args){
LinkedList ll=new LinkedList();
ll.add(10);
ll.add(20);
ll.add(30);
ll.add(40);
ll.display();
}
}

Yes, simple! As simpler as you can ever implement! I just implemented add method and this can be extended because the idea remains the same.

Thanks!

Wednesday, November 11, 2009

JDBC Connection through '.properties' File

Hi everyone,
The other day someone was asking me how to establish a JDBC Connection using a '.properties' file. I would like to share it with you. All you need is only a JRE on your machine and a DBMS along with drivers to establish connection, my example includes that of MySQL.
Open Notepad and write the following code with your own port number and password for MySQL and save it as some "jdbc.properties"


jdbc.properties

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:portnumber/databasename

username=root

password=password

Now open other text editor and write the following code and save it as some "JDBCConnection.java". Only thing is that you place the properties file in the same folder as this java file for my example to work, you can as well store it some other place but by giving a fully qualified file name in the statement 

File f=new File("fully qualified name");


JDBCConnection.java


import java.io.*;

import java.util.*;

import java.sql.*;

public class JDBCConnection

{

public static void main(String args[])

{

JDBCConnection j=new JDBCConnection();

j.getConnection();

}

public void getConnection()

{

try

{

File f=new File("./jdbc.properties");

if(f.exists())

{

Properties p=new Properties();

FileInputStream fis=new FileInputStream(f);

p.load(fis);

String driver=p.getProperty("driver");

System.out.println("Driver:"+driver);

String url=p.getProperty("url");

System.out.println("url:"+url);

String username=p.getProperty("username");

System.out.println("Username:"+username);

String password=p.getProperty("password");

System.out.println("Password:"+password);

try

{

Class.forName(driver);

Connection con=DriverManager.getConnection(url,username,password);

System.out.println("JDBC Connection Successfully established");

}

catch (Exception e)

{

e.printStackTrace();

}

}

else

{

System.out.println("No source found!!! Cannot establish JDBC Connection");

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

Thanks for going through the content, you can post any queries you have. I will try to solve them at the earliest.