Thursday, May 27, 2010

Finding Duplicates with SQL

Here's a query for finding duplicates in a table.
SELECT id_company,
  COUNT(id_company) AS occurrences
FROM company
GROUP BY id_company
HAVING ( COUNT(id_company) > 1 )

You could also use this technique to find rows that occur exactly once as bellow
SELECT id_company
FROM company
GROUP BY id_company
HAVING ( COUNT(id_company) = 1 )

Monday, May 24, 2010

How to Create Auto Increment Columns in Oracle

CREATE TABLE test_table
(
ID NUMBER NOT NULL,
ID_COMPANY VARCHAR2(20 BYTE)
)

Table created.

CREATE SEQUENCE test_sequence
START WITH 1
INCREMENT BY 1;

Sequence created.

CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT
ON test_table
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT test_sequence.nextval INTO :NEW.ID FROM dual;
END;
/

Trigger created.

Now, test adding sample data


SQL> INSERT INTO test (ID_COMPANY) VALUES ('ABC123');
1 row created.

ps : Oracle is sucks. MySQL is sexy!

Wednesday, May 5, 2010

Port Scanner

File list

1) Scar.jar
2) scan.sh (runner for linux)
3) scan.bat (runner for windows)
4) README (RTFM)

ps : This is very simple port scanner, does not support proxy and no error handler, but still can be used. I just spend less than an hour for this, if there is any bug, please let me know.

Download here

Ping Specific port in JAVA

This class is for testing the connectivity to a database. Very useful to avoid data missing during db server is down. Data can be save in file or store in another db server temporarily.

package com.blogspot.lxnx;

import java.io.*;
import java.net.*;


public class Pinger {
  public static void main(String args[]) {
    try {
      InetAddress addr = InetAddress.getByName("localhost"); 
      int port = 3306; 
      SocketAddress sockaddr = new InetSocketAddress(addr, port); 
      
      // Create an unbound socket 
      Socket sock = new Socket(); 
      // This method will block no more than timeoutMs. 
      // If the timeout occurs, SocketTimeoutException is thrown. 
      int timeoutMs = 2000; // 2 seconds 
      sock.connect(sockaddr, timeoutMs); 
      System.out.println("Alive!");
    } catch (UnknownHostException e) {
      System.err.println("Unable to lookup");
    } catch (SocketTimeoutException e) {
      System.err.println("Connection timeout");
    } catch (IOException e) { 
      System.err.println("Unable to reach");
    } 
  
   }
}


credit to http://www.exampledepot.com/egs/java.net/CreateClientSocket.html