Skip to main content

Posts

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...