Posts

How to pass a collection of data to SQL Server

Hi all. If you are faced with a situation where you wish to send an array of strings(say) to the Server, then you have stumbled to the correct page :-) To start off with, SQL server up to 2005 does not support Arrays. So, it becomes a bit difficult for Java,C#,C++ programmers who are very fond of arrays to get their job done. Fortunately there is a detour which enables us to get the job done. Instead of sending the list as an array, we can append a delimiter(here I used the comma) and send the complete string to the server side. At the server side, it splits the separate strings by the delimiter and inserts them into a table(her I used it as a single column table of the form tmpTable(_name varchar(50))) You can use the values inserted into this table and use the results similar to having an array. NOTE: It does not support the sophisticated array functions you are used to, but it will let you get the job done. /* PROCEDURE NAME : readCSV AUTHOR : Ruchira Randana PROCEDURE DESC...

Connect SQL Server Express 2005 with NetBeans IDE

Image
Hi, Most of you Java Folks may have encountered situations where you wish to code using Java but need to connect to a Microsoft SQL Server Database. To an experienced person, this may seem trivial, but for newbies, this can be a troublesome task. Through this post, I would briefly describe how to connect to Microsoft SQL Server 2005 Express Edition using NetBeans 6.0.1 IDE. STEP 1: Download the relevant JDBC driver from the microsoft site. (A search on the internet will more than suffice) STEP 2: Add the driver to NetBeans IDE. This can be done as follows. On the services tab, under Databases under Drivers, you can add a new driver as follows On the Window, click Add and select the necessary Jar file of the driver. Then click Find. It will search the class name and display it as follows Once the driver is installed, you can right click on it and click 'connect using' Then you will get the following screen Fill in your database name and host address. The port is usually 1433. Yo...

Sending Mail Using Java

Image
In this post, I would like to discuss how to send an e-mail using javax.mail library. I will discuss how to send an e-mail for the simplest case. ie: I present my example for a scenario without authorization and other related issues. Once you get this mail application up and running, you can extend it further. I would regard my example as a beginner's starting position. As I preffer to say, "Start from trivial" Dont't start your application with all complications and Network issues. You will get frustrated very soon. Once you get the trivial running, add the other requirements as features to your application. In order to get started, you need an SMTP server. (When I mean a server, it does not necessarily mean a seperate machine running as a server. You can simply run a server application on your own machine) An SMTP(Simple Mail Transfer Protocol) server is very similar to a post box. You pop a mail with the destination address stamped and, it takes care of delivering ...

Build your own Network sniffer

Image
Recently, I ve been developing a tool to monitor the Network Traffic as an assignment. Here, I wish to share my experience with all of you who are interested in building your own Sniffer. Prerequisites : Java Programming capability, Knowledge on OOP(Object Oriented Programming) basics My Environment: I used the libraries specified for Java which is widely available. I ran my application on Windows XP only. But the libraries can be easily integrated with Linux based systems. I used NetBeans 6.1 version for development activities. But any other java IDE would do In setting up the environment, first you need to download Jpcap jar found at http://sourceforge.net/projects/jpcap Once you download this, you have to add this to your NetBeans project. It can be done as follows. After adding the Jar file to the project, you need to download the WinPCap driver. It can be easily downloaded from http://www.winpcap.org/ It is required to get low level networking access regarding packets. As for ...

Run a programme by java application

You may have encountered situations where you wish to run a programme say Web Browser (FireFox) from your Java programme. It is very simple Find the location of the actual executable of the programme. As in the case of FireFox Web Browser, the actual executable file is placed at C:\Program Files\Mozilla Firefox\firefox.exe You can use the following code to run it from your Java application Runtime runtime = Runtime.getRuntime(); runtime.exec("K:/Program Files/Mozilla Firefox/firefox.exe"); (YOU MUST USE A TRY CATCH BLOCK. Just excluded them for clarity) ENJOY programming :-) Here is a great tip if you wish to launch the native applications for specific tasks such as sending a mail, opening the native web browser. public static void main(String[] args) { Desktop dt = Desktop.getDesktop(); try { try { dt.browse(new URI("http://yahoo.com")); } catch (URISyntaxException ex) { Lo...

Java Buffered Writer Issue

Using Buffered Writers is an efficient way to write the output to a file where hard disk accesses are costly. The main idea behind the Buffered Objects is(Here it is the buffered writer) having a buffer which is, writing the output to a memory location in the computer's main memory(which is faster than writing to hard disk). And then once the Buffer is filled with enough data, the whole buffer is written to the hard disk. However, this can bring up some issues which are difficult to find. One of such issues is my attempt to write to a text file using a buffered writer. I used the following code. FileWriter writer; BufferedWriter bWriter ; PrintWriter pwriter ; writer = new FileWriter (new File("./output. txt "); bWriter = new BufferedWriter (writer); pwriter =new PrintWriter ( bWriter ); int i=0; while(i pwriter . println (i); i++; ...

Tribute to a National hero

Image
Sincere condolences for the assassination of late Major General Janaka Perera who served our nation as a true hero. Major General Janaka Perera was one of the exceptional figures and one of the most successful military leaders in the history of Sri Lankan battlefield. During his military career, his leadership and endeavors to rescue the homeland from the terrorists are well known and praised by every peaceloving citizen. His leadership in rescuing the Jaffna peninsula at a crucial and decisive situation is one of his most successful and well known operations. Major General Janaka Perera also led the army in defeating the terrorist attacks of Weli Oya where his skills and intelligence were used effectively to overpower the terrorists. There were more than 300 terrorists killed and minimum damage to the army. A village was later named as Janakapura in Weli Oya for his contribution. He was awarded the medals of honour 'Rana Wickrama Padakkama','Rana Sura Padakkama', '...