Tuesday, April 15, 2014

Different Ways to Generate Unique ID

We had a requirement to generate 32 character alpha numeric unique IDs. Many options came up - here is the summary of options we considered.

Java UUID:

With Java 1.5 UUID class was introduced. This would generate universally unique identifier (UUID). A UUID represents a 128-bit value. randomUUID() method in UUID class will generate 128-bit universally unique value every time the method is called. This is hash-based values separated by dashes. Java API available here: http://docs.oracle.com/javase/6/docs/api/index.html?java/util/UUID.html

Database Hash:

If system design and architecture allows the unique ID generation on database side, then database hash can be used to create unique IDs.

Composite Key in Database:

If there is a way to combine various non unique keys to form a primary key on database side, this might work as an option as well.

Apache Commons:

randomAlphanumeric(int) in RandomStringUtils class takes length as an arguments and generates alpha numeric values. API documentation is here: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/RandomStringUtils.html

Conclusion:

What worked for us in first phase of implementation was an approach where we used the system provided columns to generate a composite key.

Later the requirement changed to use alphanumeric values at which point in time we used RandomStringUtils provided by Apache Commons. However, it came with an overhead to check the generated ID against the database. However, this didn't have impact on performance given the benchmarks. 

Friday, April 4, 2014

One click export from Excel to CSV

Many a times we want to modify and frequently export the content from Excel to CSV.

Here is a quick macro that anyone can use to export the data from Excel. 

The CSV file will be generated without any prompts. All the data on the current worksheet will be exported. The CSV will be generated in the directory where your excel file is placed. The name of the CSV combines the Excel File Name, the Worksheet Name and current date and time.

Here is the code:

Sub SaveAsCSV() 
    Dim fs As Object, a As Object, i As Integer, s As String, t As String, l As String, mn As String 
    Set fs = CreateObject("Scripting.FileSystemObject") 
    Set a = fs.CreateTextFile(Application.ActiveWorkbook.Path + "\" + Replace(ActiveWorkbook.Name, ".xlsm", "") + "_" + ActiveSheet.Name + "_" + Format(Now(), "yyyy-MM-dd-hh-mm-ss") + ".txt", True) 
      
    For r = 1 To Range("A65536").End(xlUp).Row 
        s = "" 
        c = 1 
        While Not IsEmpty(Cells(r, c)) 
            s = s & Cells(r, c) & "," 
            c = c + 1 
        Wend 
        a.writeline s 'write line 
    Next r 
    
End Sub


Hope this helps.

Tuesday, February 23, 2010

Java still not supported (officially!) on Windows Vista

It may be a world known fact but I haven't known it yet that Java officially doesn't (officially) support Windows Vista yet

We have been working on a thick client implementation in Java Swing and RMI and while unit testing we figured out a JFileChooser cannot be invoked. It throws following error. This happened with JRE version 1.5.0_08-b03 and seems to have been correctted in JRE build version 1.5.0_18-b02

Further details are available on Sun's / Oracle's bug site: http://bugs.sun.com/view_bug.do?bug_id=6449933

An error stack trace is given below which might help you identify this issue precisely...
Hope this helps ...

******************* LAUNCHING APPLICATION **************
java version "1.5.0_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_08-b03)
Java HotSpot(TM) Client VM (build 1.5.0_08-b03, mixed mode, sharing)
Starting Application...
16:42:45,076 SYSTEM - Application Started on port 52305...
7038 [Thread-2] INFO fasttrack - LOGCAT VERSION
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3184
at sun.awt.shell.Win32ShellFolder2.getFileChooserIcon(Win32ShellFolder2.java:748)
at sun.awt.shell.Win32ShellFolderManager2.get(Win32ShellFolderManager2.java:248)
at sun.awt.shell.ShellFolder.get(ShellFolder.java:221)
at com.sun.java.swing.plaf.windows.WindowsLookAndFeel$LazyWindowsIcon.createValue(WindowsLookAndFeel.java:1865)
at javax.swing.UIDefaults.getFromHashtable(UIDefaults.java:185)
at javax.swing.UIDefaults.get(UIDefaults.java:130)
at javax.swing.MultiUIDefaults.get(MultiUIDefaults.java:44)
at javax.swing.UIDefaults.getIcon(UIDefaults.java:411)
at javax.swing.UIManager.getIcon(UIManager.java:613)
at javax.swing.plaf.basic.BasicFileChooserUI.installIcons(BasicFileChooserUI.java:237)
at javax.swing.plaf.basic.BasicFileChooserUI.installDefaults(BasicFileChooserUI.java:219)
at javax.swing.plaf.basic.BasicFileChooserUI.installUI(BasicFileChooserUI.java:135)
at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installUI(WindowsFileChooserUI.java:126)
at javax.swing.JComponent.setUI(JComponent.java:652)
at javax.swing.JFileChooser.updateUI(JFileChooser.java:1755)
at javax.swing.JFileChooser.setup(JFileChooser.java:366)
at javax.swing.JFileChooser.(JFileChooser.java:341)
at javax.swing.JFileChooser.(JFileChooser.java:300)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1778)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Sunday, February 21, 2010

Resetting password for System user in Oracle Express Edition

If you are running Oracle XE for your own development purpose and by any chance forgotten the password of the "system" user here are the simple steps to resolve the problem

Linux:
export ORACLE_SID=XE
export ORACLE_HOME=
sqlplus / as sysdba
SQL> alter user system identified by ;

Windows:
set ORACLE_SID=XE
set ORACLE_HOME= (E.g. This will look something like - "D:\oraclexe\app\oracle\product\10.2.0\server")
sqlplus / as sysdba
SQL> alter user system identified by ;

Hope this helps...

Wednesday, December 9, 2009

Ora-02429 cannot drop index used for enforcement of unique/primary key

There are multiple reasons why Ora-02429 error may happen. I encountered this problem while deleting tablespace dedicated to indexes in the product we are using.

One may want to try following options:

  1. Use "cascade constraints" e.g. drop tablespace [name of ts] cascade constraints;
  2. check what objects are in this tablespace and move these to some where else and drop the tablespace.
  3. alter index [indexname] rebuild [tablespacename]; (this is because, rowid's will change when a table is moved/re-org)
  4. If you are trying to delete tablespace which is still referenced by any user; you need to make sure either users are removed (recursively) and that there are no data related to any user in there. Delete the user (Schema) with all the objects and it should free up any references.

For me the last option worked!

Tuesday, December 8, 2009

Helpful SQL Scripts

There may come a time for database developers to perform bulk operations e.g. compile all the stored procedures or rebuild all the indices. This might be required esp. when you are supporting a product and have to upgrade it.

I had to do this with help of oracle XE instance with only a web interface and here is the solution I developed.

Recompiling All Procedures:

begin
declare
cursor c_proc is
select object_name from user_objects where object_type = 'PROCEDURE';
v_sql varchar2(512);

begin
for c_proc_rec in c_proc LOOP
v_sql := 'ALTER PROCEDURE 'c_proc_rec.object_name' COMPILE';
execute immediate v_sql;
END LOOP;

end;
end;


Rebuilding all indexes:
This might be required after inserting thousands of rows as part of product upgrade. Here is the script that I used.


begin
begin
declare
cursor c_idx is
select index_name from user_indexes where index_type = 'NORMAL' and uniqueness = 'NONUNIQUE';
v_sql varchar2(512);

begin
for c_idx_rec in c_idx LOOP
v_sql := 'Alter index 'c_idx_rec.index_name' rebuild';

execute immediate v_sql;
END LOOP;
end;
end;


Hope this helps!

Monday, December 7, 2009

Restarting a Windows XP when connected over Remote Desktop

If you ever have to connect to a windows machine over remote desktop and have to shut it down; it might not be straight forward to shutdown / restart the remote machine.

Here is a simple work around that I could find. Please note that the remote desktop program by design disables the shutdown option when connected.

"Shutdown" is the utility and can be invoked from command prompt. If you have access to command prompt - run the following command and you should see a dialoguebox displaying countdown to shutdown.

C:\>shutdown -s

please note that if you don't pass any arguments; the command will list all available parameters that "shutdown" utility can take as arguments.

Following command will restart the machine.

C:\>shutdown -r

Hope this helps...