In Java you can do sorting by implementing 2 interfaces
1) Comparable
2) Comparator
Here we will see sorting example using Comparable Interface. As an example we will be sorting objects of Employee class.
When using comaprable interface make sure the object class
which you are sorting implements comaprable interface and override compareTo method correctly.
As I was more used to Java 1.4, I didnt followed any Java Generics implemetation.
SortTest is main class that will actually creaate an array list and fill it with some random Employee objects.
Employee is class that will implement compareTo method. I have added simple if-else control structure that will decide what to return on the basis of employee id.
less : -1 equal : 0 greater : 1
Here is the code for classes:
Read more…
Java comparable, comparator, Java, java generics, sorting
Here I’m going to post a class that will extract all valid urls from a web page. My class uses “URLConnectionReader” provided by Sun Tutorial
Class defines 2 constructors.
- One by default returns you the vector containing only text/html url objects from page.
- For the other you can specify the type of urls you want from a page. This is helpful when you want to get all images, videos or any other media urls.
The class also considers relative urls. It returns relative urls with http and host name prefixed.
E.g. If you have urls like “/about.php”, then class will return “http://hostname.domain/about.php”
Read more…
Java crawler, Java, url
In my last post I mentioned “Config Class”couple of times.. here you will see actual class and its usage. The class is very simple. We just need to provide proper path to config file we going to use.
The Config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| import java.util.*;
import java.util.Properties;
public class Config
{
Properties configFile;
public Config()
{
configFile = new java.util.Properties();
try {
configFile.load(this.getClass().getClassLoader().
getResourceAsStream("myapp/config.cfg"));
}catch(Exception eta){
eta.printStackTrace();
}
}
public String getProperty(String key)
{
String value = this.configFile.getProperty(key);
return value;
}
} |
You can get any property/settings from config with method ‘getProperty’
Here is my config file
#This is comment
mDbUser = myuser
mDbHost = myserver
mDbPwds = mypwd
mDbName = mydb
Usage:
1
2
| cfg = new Config();
dbname = cfg.getProperty("mDbUser"); |
Hope you find this useful..
Java config, Java
It’s been too long, I didnt get time to post anything OpenSource.
So here I am again with one simple class Database, to connect to MySQL in Java.
You can use this class in 2 ways -
- Specifying user,pwd and database name to connect
- Specifying connection ID and class will load rest of the variables from CONFIG file
Here I will post the Database class only, class to read config file and loading properties will be posted in different post.
Read more…
Java Java, MySQL
I googled quiet some time on using custom links for your site on windows using XAMPP. All of them suggested me to uncomment line for ‘mod_rewrite’, set AllowOverride to All. I tried this but it still faced same issue, a 404 error page.
Then I came to one more solution that I tried an bingooo!, it worked like charm. Just to keep note and save some time of other developers , I am putting all steps here . First 2, 3 steps are commonly suggested.
Read more…
apache htaccess, mod rewrite, xampp