Sample Client - Server Chat Application in Java

Client Server ChatApplication

Wikipedia: A network socket is an endpoint of an inter-process communication flow across a computer network.

Difference between Server and Client
Server Socket is one that accept connection on specific port and can respond. Where client can send message to server on specified port and can get response from server.

We will send messages over network only. In this demo application I'm sending and receiving messages on localhost. To create full-fledged chat application you need to modify many things like...

  • Every system must have Socket Server running on local system to accept messages from another system.
  • Get list of all system which is running your application. 
  • If you are planning to create rich chat application, provide advance settings like Save chat log, Play sound when receives message, Send encrypted messages, etc... can be lot more.

Note: There will be plenty of code which is not explained in all source code which was generated by NetBeans IDE. NetBeans project code attached at the end of article.

Socket Server
It will accept connection and listen for messages and pass it to desired frame/user chat window. I create Server in form JFrame just to give better look.
/*
 * mainFrame.java
 * This program demonstarte client server message application.
 * mainFrame.java will act as a server program aimed to receive messages and respond to client.
 */
package com.javaquery.frames;

import java.awt.Frame;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.UIManager;
import org.json.JSONObject;

public class mainFrame extends javax.swing.JFrame {

    /* Inner class to create socket server */
    private class mainServer extends Thread {

        /* Create ServerSocket variable */
        private ServerSocket serverSocket;

        /* Constructor to initialize serverSocket */
        public mainServer() throws IOException {
            serverSocket = new ServerSocket(6666);
        }

        /* Implement run() for Thread */
        public void run() {
            /* Keep Thread running */
            while (true) {
                try {
                    /* Accept connection on server */
                    Socket server = serverSocket.accept();
                    /* DataInputStream to get message sent by client program */
                    DataInputStream in = new DataInputStream(server.getInputStream());
                    /* We are receiving message in JSON format from client. Parse String to JSONObject */
                    JSONObject clientMessage = new JSONObject(in.readUTF());
                    
                    /* Flag to check chat window is opened for user that sent message */
                    boolean flagChatWindowOpened = false;
                    /* Reading Message and Username from JSONObject */
                    String userName = clientMessage.get("Username").toString();
                    String message = clientMessage.getString("Message").toString();
                    
                    /* Get list of Frame/Windows opened by mainFrame.java */
                    for(Frame frame : Frame.getFrames()){
                        /* Check Frame/Window is opened for user */
                        if(frame.getTitle().equals(userName)){
                            /* Frame/ Window is already opened */
                            flagChatWindowOpened = true;
                            /* Get instance of ChatWindow */
                            ChatWindow chatWindow = (ChatWindow) frame;
                            /* Get previous messages from TextArea */
                            String previousMessage = chatWindow.getjTextArea1().getText();
                            /* Set message to TextArea with new message */
                            chatWindow.getjTextArea1().setText(previousMessage+"\n"+message);
                        }
                    }
                    
                    /* ChatWindow is not open for user sent message to server */
                    if(!flagChatWindowOpened){
                        /* Create an Object of ChatWindow */
                        ChatWindow chatWindow = new ChatWindow();
                        /**
                         * We are setting title of window to identify user for next message we gonna receive
                         * You can set hidden value in ChatWindow.java file.
                         */
                        chatWindow.setTitle(userName);
                        /* Set message to TextArea */
                        chatWindow.getjTextArea1().setText(message);
                        /* Make ChatWindow visible */
                        chatWindow.setVisible(true);
                    }
                    
                    /* Get DataOutputStream of client to repond */
                    DataOutputStream out = new DataOutputStream(server.getOutputStream());
                    /* Send response message to client */
                    out.writeUTF("Received from "+clientMessage.get("Username").toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * To start SocketServer when mainFrame.java loads
     */
    public void startServer() {
        try {
            /* Create thread of Inner class mainServer */
            Thread t = new mainServer();
            /* Start Thread */
            t.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** Creates new form mainFrame */
    public mainFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    //                           
    private void initComponents() {

        lblInformation = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Server");
        setAlwaysOnTop(true);

        lblInformation.setText("Client - Server Sample Application");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInformation)
                .addContainerGap(229, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInformation)
                .addContainerGap(275, Short.MAX_VALUE))
        );

        pack();
    }//                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                /* To set new look and feel */
                JFrame.setDefaultLookAndFeelDecorated(true);
                try {
                    /**
                     * Change look and feel of JFrame to Nimbus 
                     * For other look and feel check
                     * 
                     */
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception ex) {
                   ex.printStackTrace();
                }
                /* Create an Object of mainFrame */
                mainFrame mFrame = new mainFrame();
                /* make mainFrame visible */
                mFrame.setVisible(true);
                /* Call startServer method  */
                mFrame.startServer();
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JLabel lblInformation;
    // End of variables declaration                   
}

Chat Window Source Code
ChatWindow.java is common class which creates Chat window for user. We are identify particular user's chat window by its title. You can have your logic.
package com.javaquery.frames;

public class ChatWindow extends javax.swing.JFrame {

    /** Creates new form ChatWindow */
    public ChatWindow() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    //                           
    private void initComponents() {

        lblInfo = new javax.swing.JLabel();
        scrollPane = new javax.swing.JScrollPane();
        chatArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Message Box");

        lblInfo.setText("Messages");

        chatArea.setColumns(20);
        chatArea.setRows(5);
        scrollPane.setViewportView(chatArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(lblInfo))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }//                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ChatWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JTextArea chatArea;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JScrollPane scrollPane;
    // End of variables declaration                   

    /**
     * @return the chatArea
     */
    public javax.swing.JTextArea getjTextArea1() {
        return chatArea;
    }

    /**
     * @param chatArea the chatArea to set
     */
    public void setjTextArea1(javax.swing.JTextArea jTextArea1) {
        this.chatArea = jTextArea1;
    }
}

Client Source Code
This will send message to sever and get response. We are sending 3 messages to server. Two messages from same username.
/*
 * client.java
 * This program demonstarte client server message application.
 * client.java will act as a client program aimed to send messages and get server response
 */

package com.javaquery.network;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import org.json.JSONObject;

public class client {

    public static void main(String[] args) {
        client c = new client();
        /* Create JSON variable */
        JSONObject transmitJSON = new JSONObject();

        /**
         * This is sample program so we are sending three messages from same system with different username.
         * In your case you'll receive message from multiple systems.
         */
        /* Fill up JSON variable with message and username */
        transmitJSON.put("Message", "Hello JavaQuery!");
        transmitJSON.put("Username", "Vicky.Thakor");
        /* Send message to server */
        c.sendMessage("localhost", transmitJSON.toString());
        
        transmitJSON.put("Message", "Hello Apple!");
        transmitJSON.put("Username", "Steve.Jobs");
        /* Send message to server */
        c.sendMessage("localhost", transmitJSON.toString());
        
        transmitJSON.put("Message", "How are you?");
        transmitJSON.put("Username", "Steve.Jobs");
        /* Send message to server */
        c.sendMessage("localhost", transmitJSON.toString());
    }

    /**
     * @author javaQuery
     * @param host
     * @param message 
     */
    public void sendMessage(String host, String message) {
        try {
            /* Create new socket connection with server host using port 6666 (port can be anything) */
            Socket client = new Socket(host, 6666);
            /* Get server's OutputStream */
            OutputStream outToServer = client.getOutputStream();
            /* Get server's DataOutputStream to write/send message */
            DataOutputStream out = new DataOutputStream(outToServer);
            /* Write message to DataOutputStream */
            out.writeUTF(message);
            /* Get InputStream to get message from server */
            InputStream inFromServer = client.getInputStream();
            /* Get DataInputStream to read message of server */
            DataInputStream in = new DataInputStream(inFromServer);
            /* Print message received from server */
            System.out.println("Server says..." + in.readUTF());
            /* Close connection of client socket */
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Download Project

How to get list of opened JFrame in Java Swing?


Swing
Swing is built on base of Abstract Window Toolkit. Swing is used to create desktop application. Swing is using AWT directly or indirectly to provide you richer user interface. Swing is placed under javax.swing

JFrame can be accessible by its parent.



Understanding the Diagram
As you can see I created boundary around Parent JFrame, Child JFrame 1 and Child JFrame 2. It means you can access list of JFrame initiated by ParentJFrame. You can't access Standalone JFrame in Parent JFrame.


Frame.getFrames()
This method will return list of all Frame Object created by its parent and parent it self.


Parent JFrame Source Code
import java.awt.Frame;

public class ParentJFrame extends javax.swing.JFrame {

    /* Creates new form ParentJFrame */
    public ParentJFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        lblInfo = new javax.swing.JLabel();
        btnOpenChildJFrame = new javax.swing.JButton();
        btnListJFrames = new javax.swing.JButton();
        scrollPane = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Parent JFrame");

        lblInfo.setText("Parent JFrame");

        btnOpenChildJFrame.setText("Open Child JFrame");
        btnOpenChildJFrame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenChildJFrameActionPerformed(evt);
            }
        });

        btnListJFrames.setText("List JFrame");
        btnListJFrames.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListJFramesActionPerformed(evt);
            }
        });

        textArea.setColumns(20);
        textArea.setRows(5);
        scrollPane.setViewportView(textArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                    .addComponent(lblInfo)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnOpenChildJFrame)
                        .addGap(18, 18, 18)
                        .addComponent(btnListJFrames)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOpenChildJFrame)
                    .addComponent(btnListJFrames))
                .addGap(18, 18, 18)
                .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// //GEN-END:initComponents

    /**
     * Open Child JFrame button click event
     */
    private void btnOpenChildJFrameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenChildJFrameActionPerformed
        /* Create an object of childJFrame */
        childJFrame child = new childJFrame();
        /* Make childJFrame visible */
        child.setVisible(true);
    }//GEN-LAST:event_btnOpenChildJFrameActionPerformed

    /**
     * List JFrame button click event
     */
    private void btnListJFramesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListJFramesActionPerformed
        /* Get previous text of JFrame */
        String previousText = textArea.getText();
        String strNewFrames = "";
        /* It will give you list of JFrame created/Initiated by ParentJFrame */
        for(Frame frame: Frame.getFrames()){
            strNewFrames += frame.getTitle() + "\n";
        }
        /* Set text of JTextArea */
        textArea.setText(previousText+strNewFrames);
    }//GEN-LAST:event_btnListJFramesActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ParentJFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JButton btnListJFrames;
    private javax.swing.JButton btnOpenChildJFrame;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextArea textArea;
}

Child JFrame Source Code
public class childJFrame extends javax.swing.JFrame {

    /** Creates new form childJFrame */
    public childJFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        lblInfo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Child JFrame");

        lblInfo.setText("Child JFrame");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo)
                .addContainerGap(188, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo)
                .addContainerGap(91, Short.MAX_VALUE))
        );

        pack();
    }// //GEN-END:initComponents

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new childJFrame().setVisible(true);
            }
        });
    }
    private javax.swing.JLabel lblInfo;
}
I used NetBeans to create sample code. I explained code that relate the title of article. Major code done only in Parent JFrame. Child JFrame only supports example.

java.net.ConnectException: Connection timed out: connect

internet disconnected

This exception caused by several issues. Issue may be solved by ensuring following checklist.

- Connected to Internet / Local Area Network (LAN)
Make sure you are connected to internet or local area network.

- Behind Firewall
Firewall is common issue faced by many developer. We were facing same issue.
java.net.ConnectException: Connection timed out: connect
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
 at java.net.Socket.connect(Socket.java:529)
 at java.net.Socket.connect(Socket.java:478)
 at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
 at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
 at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
 at sun.net.www.http.HttpClient.(HttpClient.java:233)
 at sun.net.www.http.HttpClient.New(HttpClient.java:306)
 at sun.net.www.http.HttpClient.New(HttpClient.java:323)
 at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
 at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
 at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
 at ReadWebpage.main(ReadWebpage.java:24)

Scenario
We were writing an applet program in Java to read webpage from internet. We can't even make request to well know search engine www.google.com even if its allowed in firewall rule. We called upon our network admin he said you must pass your request through proxy server. We set proxy in our Java code for HttpURLConnection and all went good.

Code
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.202", 8080));
HttpURLConnection con = (HttpURLConnection) data.openConnection(proxy);
Use of Proxy
You need to take care for proxy connection and it depends upon whether you are write program for applet or desktop application. If you are writing code for applet then in production applet, you don't need proxy connection as server will have all access and will work without proxy. If you are working on desktop application that run within the Local Area Network then you must set proxy in case of firewall.

Sign Applet using code signing certificate and verify jar


Applet
An applet is Java program designed to be delivered over Internet/Network. The Java program executed automatically by Java compatible browser.

Code Signing Certificate
Like in real world we sign document, Code Signing Certificate does the same with code. Code signing certificate is used to preserve authenticity of code. You can buy code signing certificate through different vendor like verisign, Thawte, etc...

Note: I am using .jks (Java KeyStore) file to sign my applet. I haven't tried with other certificate as its not available for free. You can also contact your vendor for the same.


Step 1: Start command prompt.

Step 2: Execute below command by changing instructed values.
Note: I placed certificate and applet in same folder and pointing jarsigner.exe.
/**
 * Change jarsigner.exe path 
 * Change certificate name and path
 * Change jar file name and path
 */
cmd> "C:\Program Files\Java\jdk1.6.0_25\bin\jarsigner.exe" -keystore javaQueryThawte.jks applet.jar server

Step 3: Verifying jar you just signed.
/**
 * Change jarsigner.exe path 
 * Change jar file name and path
 */
cmd> "C:\Program Files\Java\jdk1.6.0_25\bin\jarsigner.exe" -verify -verbose -certs applet.jar

Lets say you required to sign same applet again and again, we'll act smart. Create executable batch file for windows system.
/**
 * Open notepad
 * Change jarsigner.exe location
 * Change certificate name
 * Change jar file name
 * Save below code with file extension .bat i.e: signApplet.bat
 */
@echo off
echo Applet Code Signing Console
"C:\Program Files\Java\jdk1.6.0_25\bin\jarsigner.exe" -keystore NextendersIndiaPvtLtd.jks POCDemo.jar server
"C:\Program Files\Java\jdk1.6.0_25\bin\jarsigner.exe" -verify -verbose -certs POCDemo.jar
pause

Place signApplet.bat, certificate and jar file in same folder. Now execute signApplet.bat and provide password of certificate. You are done with it.

Error Code: 1093. You can't specify target table 'user_master' for update in FROM clause

mysql_logo

You can't specify target table 'user_master' for update in FROM clause error comes up when you try to perform an update or delete operation while selecting data from table it self. This is something like How to update/delete self table in MySQL? We will produce same error and then will give you solution for the same.

Problem
/* UPDATING RECORDS */
UPDATE user_master
SET first_name = 'javaQuery'
WHERE id IN (SELECT id FROM user_master WHERE last_name = 'Jobs');

/* DELETING RECORDS */
DELETE 
FROM user_master
WHERE id IN (SELECT id FROM user_master WHERE user_name = 'javaQuery');
We are fetching all users whose Lastname is Jobs and replacing its Firstname in 1st query and deleting records in second query. This won't allow you to update or delete records.


Solution
/* UPDATING RECORDS */
UPDATE user_master
SET first_name = 'javaQuery'
WHERE id IN (SELECT * FROM (SELECT id FROM user_master WHERE user_name = 'javaQuery') AS userIDs);

/* DELETING RECORDS */
DELETE
FROM user_master
WHERE id IN (SELECT * FROM (SELECT id FROM user_master WHERE user_name = 'javaQuery') AS userIDs);

What we did?
  • We are selecting data from query SELECT id FROM user_master WHERE user_name = 'javaQuery' to temporary table called userIDs.
  • Select data from temporary table and use it in WHERE condition.

How to capture computer screen using Java?

screen capture macbook and desktop

Abstract Window Toolkit (AWT)
           AWT used to create and manage windows. It is used to create windows that run in a GUI environment. Swing is built on base of AWT. Swing is using AWT directly or indirectly to provide you richer user interface. AWT is placed under java.awt package.

We will use Abstract Window Toolkit to create screen capture program. For the sake of convenience, I’m executing example from main method. Following example will capture whole screen of your system.
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 * @author javaQuery
 */
public class ImageCapture {
    public static void main(String[] args) throws AWTException, IOException {
        /* File location is C:\Users\computerName\screenshot.png */
        File saveImage = new File(System.getProperty("user.home")+"\\screenshot.png");
        /* Define how much area you want to capture */
        Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); 
        /* Capture image */
        BufferedImage imageCapture = new Robot().createScreenCapture(rectangle);
        /* Write BufferedImage to file */
        ImageIO.write(imageCapture, "png", saveImage);
    }
}

Variations

Rectangle(int width, int height);
Rectangle rectangle = new Rectangle(100,100);
It will capture an image of 100 x 100 (WxH) from top left corner of screen.

Rectangle(Dimension(int width, int height));
Rectangle rectangle = new Rectangle(new Dimension(100, 100));
It will capture an image of 100 x 100 (WxH) as specified its dimension. It’s same as above specification. To use dimension you need to import java.awt.Dimension class in your code.

Rectangle(Point(int x, int y));
Rectangle rectangle = new Rectangle(new Point(20, 20));
/* setSize(int width, int height) */
rectangle.setSize(100, 100);
Point is used to specify PointX and PointY of screen. It capture image block of given size from PointX and PointY. In this case it will capture an image of 100 x 100 (WxH) from point x = 20 and point y = 20. To use Point you need to import java.awt.Point class in your code.

Rectangle(Point(int x, int y), Dimension(int width, int height))
Rectangle rectangle = new Rectangle(new Point(10,10), new Dimension(100, 100));
Using Point and Dimension at the same time. Capture 100 x 100 (WxH) image from point x = 10 and point y = 10. You need to import both class as follow java.awt.Point and java.awt.Dimension.

Rectangle(int x, int y, int width, int height)
Rectangle rectangle = new Rectangle(20, 20, 100, 100);
It’s same as specifing PointX and PointY of screen and its width - height. It will capture an image of 100 x 100 (WxH) from point x = 20 and point y = 20.

You can use this program for different purposes. One of them is Key Logger in Java.

Database ORDER BY on two columns

ORDER BY two column_Microsoft_SQL_Server_MySQL

ORDER BY 
ORDER BY is used in database to sort data in Ascending or Descending order. ORDER BY keyword is available in all across different database platforms. It comes with two option ASC and DESC. If you don't apply order pattern(ASC or DESC) then it'll take ASC as default pattern.

Microsoft SQL Server and MySQL (Ascending) : SELECT * FROM user_master ORDER BY user_name
Microsoft SQL Server and MySQL (Descending) : SELECT * FROM user_master ORDER BY user_name DESC

I tested ORDER BY on two columns in Microsoft SQL Server and MySQL. You can share for other database platform.

Microsoft SQL Server Start up scripts
/* MSSQL tables and Insert queries */
/****** Object:  Table [dbo].[post]    Script Date: 05/22/2014 17:25:18 ******/
CREATE TABLE [dbo].[post](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [post] [varchar](max) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[post] ([id], [post]) VALUES (1, N'Post 1')
INSERT [dbo].[post] ([id], [post]) VALUES (2, N'Post 2')

/****** Object:  Table [dbo].[comments]    Script Date: 05/22/2014 17:25:18 ******/
CREATE TABLE [dbo].[comments](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [comment] [varchar](max) NULL,
 [postID] [int] NOT NULL
) ON [PRIMARY]
GO
INSERT [dbo].[comments] ([id], [comment], [postID]) VALUES (1, N'comment 1 of 1', 1)
INSERT [dbo].[comments] ([id], [comment], [postID]) VALUES (2, N'comment 2 of 1', 1)
INSERT [dbo].[comments] ([id], [comment], [postID]) VALUES (3, N'comment 1 of 2', 2)
INSERT [dbo].[comments] ([id], [comment], [postID]) VALUES (4, N'comment 2 of 2', 2)
INSERT [dbo].[comments] ([id], [comment], [postID]) VALUES (5, N'comment 3 of 2', 2)

MySQL Start up scripts
/* MySQL tables and Insert queries */
/****** Object:  Table post    Script Date: 05/22/2014 17:25:18 ******/
CREATE TABLE `post` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `post` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT post (id, post) VALUES (1, 'Post 1');
INSERT post (id, post) VALUES (2, 'Post 2');

/****** Object:  Table comments    Script Date: 05/22/2014 17:25:18 ******/
CREATE TABLE `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `comment` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `postID` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT comments (id, comment, postID) VALUES (1, 'comment 1 of 1', 1);
INSERT comments (id, comment, postID) VALUES (2, 'comment 2 of 1', 1);
INSERT comments (id, comment, postID) VALUES (3, 'comment 1 of 2', 2);
INSERT comments (id, comment, postID) VALUES (4, 'comment 2 of 2', 2);
INSERT comments (id, comment, postID) VALUES (5, 'comment 3 of 2', 2);

Querying the above two tables.
SELECT *
FROM post p
LEFT JOIN comments c
ON p.id = c.postID
ORDER BY p.id DESC, c.id DESC
ORDER BY two column_Microsoft_SQL_Server_MySQL

  • You can't apply ORDER BY on two columns with same table. If you apply ORDER BY on same table. It'll take last column name and apply ORDER BY pattern on it.