Monday, April 20, 2009

sql IN logical operation for java

Hi,
have you ever needed sql IN for java language ?
OK, in this post I'm going to use new java 5 feature (varargs method) to simulate sql IN:
public static boolean in(int i, int... ints) {
Arrays.sort(ints);
return Arrays.binarySearch(ints, i) >= 0;
}
as you see, our function gets 2 parameters, first one is your desired value and second one is your search list.
now, when you call the method:
public static void main(String[] args) {
System.out.println(in(1, 2, 3, 4, 5, 1));
}
it returns:
true
i hope you find this tip useful;

Labels:

Tuesday, January 15, 2008

Set JFreeChart data from database

A couple of days ago I needed to generate some charts using Java so I searched for a library which could do it. Soon I found JFreeChart library and downloaded it from here. But I was surprised when I saw the user guide wasn't free! Therefore I decided to write this post and describe how to obtain JFreeChart data from database (I'll use mysql database).

At first I copied jcommon-1.0.12.jar, jfreechart-1.0.9.jar and mysql-connector-java-5.0.7-bin.jar files to /WEB-INF/lib directory then I created a table in the database and filled it with below data:

type count
----------------------
invoice_detail 3273
----------------------
object_status 2819
----------------------
service_sales_... 1540
----------------------
call_function 1183
----------------------
contact 904
----------------------
invoice 775
----------------------
payment 596

Now I create DatabaseChart servlet for connecting to database and generating my chart

import org.jfree.data.jdbc.JDBCPieDataset;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;

public class DatabaseChart extends HttpServlet {

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver").
newInstance();
try {
connection = DriverManager.
getConnection("jdbc:mysql://localhost/
my_db?user=my_un&password=my_pass&
useUnicode=true&
characterEncoding=utf-8");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

JDBCPieDataset dataset =
new JDBCPieDataset(connection);
try {
dataset.executeQuery("Select `type`, `count`
From my_table order by count desc");
JFreeChart chart = ChartFactory.
createPieChart("Pie Chart", dataset,
true, true, false);
if (chart != null) {
response.setContentType("image/png");
OutputStream out = response.
getOutputStream();
ChartUtilities.writeChartAsPNG(out,
chart, 450, 400);
}
} catch (SQLException e) {
e.printStackTrace();
}
try {if(connection != null){connection.close();
}}
catch (SQLException e) {e.printStackTrace();}
}

}
at the end I add the servlet information to web.xml file
 <servlet>
<servlet-name>generate_chart</servlet-name>
<servlet-class>DatabaseChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>generate_chart</servlet-name>
<url-pattern>/generate_chart</url-pattern>
</servlet-mapping>

now if you run the project you will get a chart like this

Labels: , ,

Monday, January 7, 2008

How to search and sort primitive arrays in Java ?

Always I was keen on solution for sorting and searching primitive arrays. Today I found java.util.Arrays class that includes some useful static methods for sorting, searching, filling and ... For sorting you can use Arrays.sort(aPrimitiveArray) static method, look here :
String[] test = {"d", "z", "a"};
Arrays.sort(test);
System.out.println(Arrays.toString(test));
The result will be:
[a, d, z]
As you see we've used Arrays.toString(test) for printing sorted array.

And for searching you can use Arrays.binarySearch(aPrimitiveArray, key) static method:
Arrays.sort(test);
System.out.println(Arrays.toString(test));
System.out.println(Arrays.binarySearch(test, "a"));
The result will be:
[a, d, z]
1
1 is index of found key otherwise it will be a less than zero number.

Attention: that before using Arrays.binarySearch(aPrimitiveArray, key) method you have to sort array by Arrays.sort(aPrimitiveArray) method.

Labels: ,

Saturday, December 22, 2007

first wintery mountain climbing

1. My wife and I went to first wintery mountain climbing on Friday. The weather was cold (around -6 C) but it's sunny. At first we had some problems with our ice creepers but i found their temper ;)

Everything was ok but when we was returning my knee got pain. One or two month ago I went to doctor and he said "it's not important , but you should strengthen your knee" !

Anyhow that was fantastic for us. You can see some pictures about that below.






2. Today my intellij IDEA made me crazy, when I compiled and ran my j2ee App, appeared situation as coma after one or two click on links! I couldn't understand what's wrong. God, help me :(

3. I found a page that lists all irregular verbs in English language, enjoy it.

Labels: , ,