Skip to main content

Posts

Showing posts from 2009

Easy Start with JNA

JNA (Java Native Access) is the good and easy answer to the JNI(Java Native Interface). Those who find it cumbersome to to call C, C++ DLL functions using JNI, then JNA is for you. The project is hosted at https://jna.dev.java.net/ . Here is my first encounter(experiment) with the JNA. Problem: I want to call one DLL function in Java program Solution: The first thing that comes into my mind is JNI, coz I heard a lot about it whenever something "native" has to be called in Java. Just while surfing for the more information on basic JNI programming, I came across the JNA project/api and find it interesting to give it try. Here are the basic two things you need to have in order to use JNA 1) JNA api JAR file (jna.jar): Download it from here 2) DLL which you want to call in your Java program Now this may be the big thing for a guy who haven't worked on VC++, DLL, MFC etc things. So here is the quick link about how to create the DLL http://www.icynorth.com/development/createdl

How to know when FORM (bean) values changed by user or not

Many times, developer want to know whether user has changed the HTML FORM value or values so that at server side action they can take the necessary action if some values has been changed or not changed at all. For example, once someone has requirement regarding whether user changed the IP address fields in the UPDATE HTML FORM, because if that value has been changed by the user, then he need to make several heavy network calls, which he wants to avoid if user doesn't change that single value. Also, recently someone wants to know if the FORM has changed by single value or submitted as it without any change. For this above scenario, there are two (best) possible solutions: 1. If you want to observe the value change for particular bean property, then use dummy variable in bean and if you do not wish to create one, then pass the previous bean value in query string. How to make it: public class MyForm { private String name; private Integer number; // I like to know

Syntax Highlighter for Blogger

Update: Here is the simple two steps which will add "Syntax Highlighter" widget for blogger hosted blogs 1. Go to : http://fazibear.googlepages.com/blogger.html 2. And click to : --------------------------------------------------------------------------------------- Today, one of my JR mate asked me about How did I add that syntax highlighter code in blogger ? OK, I knew I installed it from some blog, but not remembering the name of that site, so I googled it and all the hits shows me the way to install it using CSS defined and uploaded at googlepages site. Here is the code project site for same from Google: http://code.google.com/p/syntaxhighlighter/ I tried this too, but with little success. Its not that easy. But, finally I found that blog and thus that post where author created the Blogger widget for syntax highlighter. Just add it and you can add code snippet in you blog. Here is that useful link: http://fazibear.blogspot.com/2007/09/blogger-syntax-higlighter.html

INTO OUTFILE query, How to modify & output null values ?

I tried one query (originally asked at JR) on MySQL which export the table data into a file, with a comma delimited field. The query is straight forward and replace the null column value by "\N" character, but when I want to replace that "\N" value by some other character like "0" or simple empty field like " ", its gets little tricky. Here is the base query SELECT Id, name, age FROM student INTO OUTFILE 'c:/result.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'; And it outputs like, 1, "sagar", 23 2, "anant", 24 Now suppose some 'name' fields are null, then it outputs like 1, "sagar", 23 2, \N, 24 Now to obtain my desired results, which replace this null (\N) values by empty string like, "", I tried out two solutions --1. Using CASE statement SELECT Id, CASE WHEN name IS NULL THEN '' ELSE name END AS NewName, age FROM s

Java Generics: Why I get "name clash" exception when I override methods with different "type" parameter ?

import java.util.*; class Parent { void foo(List<String> list) { System.out.println("parent"); } } class Child extends Parent { void foo(List<Integer> list) { System.out.println("child"); } } When you compile this code you will get an error like Parent.java:7: name clash: foo(java.util.List<java.lang.Integer>) in Child and foo(java.util.List<java.lang.String>) in Parent have the same erasure, yet neither overrides the other class Child extends Parent { ^ 1 error This "name clash" error thrown because of "type erasure" thing when "generics" code gets complied. Let’s see how, When we complied any generics code, the compiler removes the type from the code and treat it as a simple pre Java 1.5 code, like, in our case, compiler saw something like this, import java.util.*; class Parent { void foo(List list) { // oops, <String> get erased. System.out.println("parent"); } } class Child extend

Remove PK, FK constraint from SQL Server table

I'm weak in databases and consult Google every time if I'm new to the concept OR baffled somewhere. Some days back I got simple requirement which is nothing but to drop the PK constraint from a table, Initially I thought it was easy to do but got stuck and finally came up with solution.This is what straight forward thing I did -- Problem: Remove the PK constraint from the table 'test', -- Solution: -- 1. Get the table constraint using following query SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'test'; -- 2. It returns a column CONSTRAINT_NAME with the list of constraint, like, PK, FK, etc --|CONSTRAINT_NAME| ------------------- --|PK_test | -- 3. The PK constraint name generally starts with "PK_table_name[_xxx]" -- 4. Remove it using following query ALTER TABLE test DROP CONSTRAINT PK_test; -- 5. You just drop the PK constraint from table 'test', hurray !!! :) Hope this hel

VirtualHost in Tomcat (Make Tomcat a standalone web server)

Problem: I want to bind a registered domain name to my web application using Apache Tomcat as a standalone web server(means NO Apache web Server, HTTPD), simple :) Environment: Windows Server (64) 2003 Apache Tomcat 6.0.18 IIS 6 Still recently what were we doing to access the web application is, we are binding domain name "xyz.com" to the IIS which is running on port 80, and then this IIS redirects the page to local instance of tomcat, which is running on port 8080, as http://localhost:8080/WebApp So when someone hits www.xyz.com the server redirects to the URL as http://xyz.com:8080/WebApp. Now we need some changes into it 1. Remove port 8080 from URL (pretty easy) Open CATLINA_HOME/conf/server.xml and search for number 8080 and replace by 80, save it and you're done. But wait, if you restarted the tomcat, it will throw exception, like ‘JVM_bind: port 80 is a

Job storage in Quartz using Files, A FileJobStore Class

Quartz supports mainly two types of Job Storage for Scheduler information. • Memory (non persistent) storage The memory job storage facility for Quartz is provided by a class called org.quartz.simpl.RAMJobStore , The RAMJobStore is the default, i.e unless you change the configuration, and this is the JobStore that will be used for any Quartz application. Using this JobStore has some advantage 1. Easy to configure, because already configured by Scheduler as a default 2. It’s fast - a plain old simple memory access But, as a computer memory is volatile, when your Quartz application is stopped, it releases the memory back to the operating system. Of course, along with everything else stored in that released memory is the scheduling information. • Persistent storage Second is using database for Job storage which implements JobStoreSupport abstract class Quartz offers two different types of concrete persistent JobStores, each one designed for a specific database environment and co

Luck By Chance

No, no I’m not here to discuss the latest Farhan Akhtar starred film, Luck By Chance . This actually regarding the Best Java forum on earth, JavaRanch where mostly I’m engaged with my post. The forum is very friendly yet professional. Most of the time they have a book promotion, which is usually starts at Monday and ends at Friday, in a same week. This time they gave out the copies of “Eclipse Plugin” in 'IDEs, Version Control and other tools' forum section, they draw 4 lucky posters at randomly, and give out book to them, and this time, I won the book… Now what is the Luck By chance, because when you post only one answer to this forum and still you won, you need to be lucky and after all it’s my day, isn’t it? Many people (Not "really" many , but one) questioned about the correctness of that software, after saw my name who posted only one reply. It’s all your luck and for the matter of fact, I participated in many book promotions earlier, recent one is for 'Apac

How to fetch NULL data into C-ODBC program?

From last two months I’m working on C-ODBC project, despite my expertise in Java. This was I encountered while development. Problem : C-ODBC API does not provide any function which fetch NULL data without throwing a runtime error. Solution : Need to provide an indicator , which get set when row data contained NULL for that specific column. 1. Define the long typed variable for holding indication about NULL data: SQLINTEGER i_first_name, i_last_name, i_age; // i for indicator 2. Passed this indicator variable to SQLBindCol function: SQLBindCol(hstmt, 1, SQL_C_CHAR, first_name, 100, &i_first_name); SQLBindCol(hstmt, 2, SQL_C_CHAR, last_name, 100, &i_larst_name); SQLBindCol(hstmt, 3, SQL_C_CHAR, age, 100, &i_age); More Information: http://msdn.microsoft.com/en-us/library/ms711010.aspx 3. Check whether we fetched any NULL data into a column by checking, if(i_first_name == SQL_NULL_DATA) { // I put a “null” string into fist_name variable. strcpy(fi