tag “java”
sql IN logical operation for java
by Hamid Reza Fahimi Madjd @ Apr 20, 2009
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;
java no comment
how to deploy war file into web root ?
by Hamid Reza Fahimi Madjd @ May 7, 2008
Some times we need deploy our peoject into root directory of your domain (not sub directory), to solve the problem:
- make war file (for example myproject.war)
- rename the war file to ROOT.war (attention the filename must be uppercase)
- deploy war file
now, you can see / in tomcat web application manager page that deployed in site root.
java no comment
Set JFreeChart data from database
by Hamid Reza Fahimi Madjd @ Jan 15, 2008
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, 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
java, jfreechart no comment
How to search and sort primitive arrays in Java ?
by Hamid Reza Fahimi Madjd @ Jan 8, 2008
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.
array, java no comment
deploy war file without lib in Tomcat
by Hamid Reza Fahimi Madjd @ Dec 12, 2007
At this post I'd talk about how to decrease size of big war files and make deployment faster.
I have a 15 MB war file and the size of lib (jar files) directory is 14.8 MB! So I need a solution that enables me to upload my project lib directory just for once and after that deploy my war file without lib directory.
At first I renamed my lib directory to myprojlib and then copied (for example using ftp) it to ${tomcat home}/common/lib folder on the server.
After that I modify catalina.properties file in ${tomcat home}/conf and add my myprojlib path to common.loader according below:
common.loader=${catalina.home}/common/classes, ${catalina.home}/common/i18n/*.jar, ${catalina.home}/common/endorsed/*.jar, ${catalina.home}/common/lib/*.jar, ${catalina.home}/common/lib/myprojlib/*.jar
After that I remove lib folder from my project and compile it without lib directory. Be careful don't package jar files in your project. My war file is 180KB now and I deploy it easily and I shouldn’t deploy 15 MB war file every time.
java, lib, tomcat, war file no comment
first wintery mountain climbing
by Hamid Reza Fahimi Madjd @ Dec 22, 2007
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 crampons 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.
intellij, mountain, java no comment
recent posts
- › how to use phing to build php projects
- › slice/paging large contents using php
- › how to log methods call in php ?
- › sql IN logical operation for java
- › backup from mysql database's routines
- › how to deploy war file into web root ?
- › how to get all oracle components version ?
- › temporary/memory tables in mysql
- › Set JFreeChart data from database
- › How to search and sort primitive arrays in Java ?
archive
- › 2011/06 (1)
- › 2010/11 (1)
- › 2010/10 (1)
- › 2009/04 (2)
- › 2008/05 (1)
- › 2008/03 (1)
- › 2008/01 (4)
- › 2007/12 (4)
last tweet
- ›