Skip to main content

Posts

How to convert a CSV file to XLSX

I wrote this utility for a friend of mine who wanted to convert large (~50MB) CSV files to XLSX. The CSV files had comma "," as a delimiter. I have used apache poi utility in this program. https://www.apache.org/dyn/closer.lua/poi/release/RELEASE-NOTES.txt This code uses SXSSFWorkbook class. The SXSSFWorkbook class uses "BigGridDemo" strategy, where only portions being processed are kept in memory. There are temporary files created which store the rest of the temporary data. setCompressTempFiles() method allows these temp files to be compressed. The size of these files can get quite large. The data to be converted was UTF-8 encoded data. So, we are using  OutputStreamWriter, where we specify the encoding of the data. If you do not need the encoding, just remove that parameter from the constructor call of OutputStreamWriter. The system where this ran, supported Java 7, so have not used features in java 8, such as try with exceptions. import java.io.*; im...

Interview Task: Java exercise to test basic Java skills

Here is a problem to test following basic Java skills, with increasing difficulty. Depending on what skills are expected from you. 1. OOPS concepts 2. Code Design 3.  Knowledge of Java classes 4. Collections 5. J2EE Interview Tasks Task 1 Create an application that maintains information of all students in a class. The information of each student includes Name, Age and Roll number. The application should be able to do the following, through a console: (No need for UI) 1. Add a new student information 2. Delete a student information 3. Edit Student information -- Roll number should not be editable 4. List all students in a class --While evaluating the solution, check what are the Interfaces, abstract classes are created and the reasons to do so. Which collection is used to store the list of students. How do you ensure a student is not added twice. Task 2 Now functionality that will allow users to be sorted alphabetically by Name. Also add another functi...

JDBC Drivers for relational databases

JDBC (Java Database Connectivity) is an API that allows Java programs to access the database to read or modify data in databases. Below given are various databases and their Type 4(thin) JDBC driver details For each database I have given the driver name and the URL from where you can download the jdbc driver jar file. Oracle Driver name:  oracle.jdbc.driver.OracleDriver URL:  http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html (Drivers for various Oracle versions including 8i, 9i,10g, 11g are available here) Microsoft SQL Server Driver name:  com.microsoft.sqlserver.jdbc.SQLServerDriver URL:  http://www.microsoft.com/download/en/details.aspx?id=21599 ( This driver can connect to SQL Server 2008 R2, SQL Server 2008, SQL Server 2005 and SQL Server 2000 versions )    IBM DB2 Driver name:  java com.ibm.db2.jcc.DB2Jcc URL:   http://www-01.ibm.com/support/docview.wss?uid=swg21363866 ( DB2 Ve...

How to detect browser closing events and warn user before closing

Recently I came across this problem.I was creating a web application where users were supposed to save their work before logging out.While testing, it turned out that many users closed their browsers instead of logging out. To solve this, I googled the problem and found out that this was a very common problem. I just used below java script code in my webpage's header. Q. How to detect browser closing event and save your data Ans: The onunload event gets fired when the browser is closed or refreshed(using F5 or the refresh button). So now It turns out that the data was getting saved unnecessarily on refreshing the page. For that I stumbled across a forum where I found the solution.(It works for IE only) Q.How to disable function key F5? Ans: document.attachEvent("onkeydown", my_onkeydown_handler); function my_onkeydown_handler() { switch (event.keyCode) { case 116 : // 'F5' event.retur...

Fully qualified table name for relational databases

Q: What is fully qualified table name pattern? Ans: A fully qualified table name is an unambiguous name that specifies a table in a database. It is advisable to use the fully qualified name of a table so that there are less chances of errors. Various databases have different patterns of fully qualified table names. Below given are the details of various databases and their fully qualified table name patterns Database Fully qualified table Pattern Oracle SCHEMA.TABLE DB2 CATALOG.TABLE MySQL SCHEMA.TABLE Microsoft SQL Server [CATALOG]..[TABLE] PostGre SQL "SCHEMA"."TABLE"

How to remotly debug java program using Eclipse

Eclipse provides a facility to debug your java programs remotely. To demonstrate how you can remotely debug java programs remotely, I will do the following: Create a .bat file that calls the java program Configure the arguments to the JVM so that the JVM will be capable of being remotely debugged Configure Eclipse to connect to the remote JVM 1. Create a bat file calling the java program I create a java class given below: public class RemoteDebugDemo { public static void main(String[] args) { System.out.println("STEP 1"); System.out.println("STEP 2"); System.out.println("STEP 3"); System.out.println("STEP 4"); System.out.println("STEP 5"); System.out.println("STEP 6"); } } Now I wish to run this program in batch mode.So I write a file called RemoteDebugDemo.bat file with following content. java -jar RemoteDebugDemo.jar I create a jar file named RemoteDebugDe...

How to create a jar file using eclipse in 3 easy steps

A jar file is a J ava AR chive file that contains many java .class files as well as other resource files in a compressed format. It is similar to a tar or zip format. Here are three simple steps with illustrations to create your jar file in Eclipse IDE. Step 1 : Create your java class(es) I have written a java class HelloWorld.java in Eclipse in a package called HelloWorld as shown below. Step 2 : Choose the export file format Now I want to put entire project in a jar file. Right-click on the package and select export. Now select under Java icon JAR as shown. Step 3 : Specify the jar file name and location I give the jar file name as C:\amol\HelloWorld.jar as shown I check 'Export generated class files and Resources' and 'compress the contents of jar file' checkboxes. Thats it. Now just click finish and your jar file will be created at the given location. CGTUXEU3CDMS