L8storm's IT Blog

Keeping my mind free for other stuff

Logging JDBC Statements

without comments

You can log nearly every SQL statement without modification of your application. This includes timing of SQL statements, prepared statements and so on…

This can be reached by a JDBC driver wrapper. I was successful using the “log4jdbc” driver, see: http://code.google.com/p/log4jdbc/

You have to follow these steps:

  1. Place the log4jdbc4-1.2beta2.jar into your classpath
  2. If not already done, place the slf4j-api-*.jar and the slf4j-log4j12-*.jar (for Log4J logging) into your classpath
  3. Change your JDBC driver class to net.sf.log4jdbc.DriverSpy
  4. Change your JDBC url to jdbc:log4jdbc:…
  5. Configure Log4J (or your log framework) to log the category “jdbc.sqltiming” in INFO mode

Links:

Written by l8storm

November 16th, 2010 at 3:41 pm

Posted in IT Stuff

Tagged with , , , , , , ,

Windows as NTP server

without comments

Want to use MS Windows as an NTP time server?

  1. Copy this into a file named ActivateNTP.reg:
    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters]
    "LocalNTP"=dword:00000001

  2. Exeute the file ActivateNTP.reg as Administrator to change the registry informations
  3. Open a command line: Start -> Run… -> cmd.exe
  4. Restart the time service:
    net stop w32time
    net start w32time

  5. Be sure the firewall enables all clients to access the NTP service (UDP 123)

Links:

Written by l8storm

September 27th, 2010 at 1:40 pm

Posted in IT Stuff

Tagged with , , , ,

Uploading Files to a Server Using VBA

without comments

Problem: You want to store files on a central server, not on a client

Solution: This can be done by a combination of a Apache Tomcat servlet container and a simple VBA Script. This solution uses a HTTP POST request to send a file. The server side stores the file in a given destination directory.

1. Set up Apache Tomcat and deploy the file “fileupload.war”.
2. Edit the VBA script and send files to the server

Private Function postFile (fileName, url, remoteFileName, remoteDir)
  ' Prepare
  Randomize Timer

  Dim boundary 'As String
  boundary = "---------------------------32203174618290" 

  Dim simpleFileName 'As String
  If InStrRev(fileName, "\") = 0 Then
    simpleFileName = fileName
  Else
    simpleFileName = Mid(fileName, InStrRev(fileName, "\") + 1)
  End If

  Dim payload 'As Variant

  'file object
  payload = "--" & boundary & vbCrLf
  payload = payload & "Content-Disposition: form-data; name=""File""; filename=""" _
             & simpleFileName & """" & vbCrLf
  payload = payload & "Content-Type: application/octet-stream" & vbCrLf
  payload = payload & vbCrLf
  payload = payload & BinaryToString(ReadBinaryFile(fileName)) & vbCrLf

  'form data
  payload = payload & "--" & boundary & vbCrLf
  payload = payload & "Content-Disposition: form-data; name=""remote_filename"" & vbCrLf
  payload = payload & vbCrLf
  payload = payload & remoteFileName & vbCrLf

  payload = payload & "--" & boundary & vbCrLf
  payload = payload & "Content-Disposition: form-data; name=""remote_dir"" & vbCrLf
  payload = payload & vbCrLf
  payload = payload & remoteDir & vbCrLf

  payload = payload & "--" & boundary & "--" & vbCrLf

  ' Execute POST request
  ' see: http://msdn.microsoft.com/en-us/library/ms760305%28v=VS.85%29.aspx
  Dim objHTTP 'As Variant
  Set objHTTP = WScript.CreateObject("MSXML2.ServerXMLHTTP")

  objHTTP.Open "POST", url, False
  objHTTP.setRequestHeader "User-Agent", _
                   "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.0)"
  objHTTP.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
  objHTTP.send (payload)

  postFile = objHTTP.status

  objHTTP.abort
End Function

' http://www.motobit.com/tips/detpg_read-write-binary-files/
Function ReadBinaryFile(FileName)
  Const adTypeBinary = 1

  Dim BinaryStream 'As Variant
  Set BinaryStream = CreateObject("ADODB.Stream")

  BinaryStream.Type = adTypeBinary
  BinaryStream.Open
  BinaryStream.LoadFromFile FileName

  ReadBinaryFile = BinaryStream.Read
End Function

Function BinaryToString(Binary)
    'Antonin Foller, http://www.motobit.com
    'Optimized version of a simple BinaryToString algorithm.

    Dim cl1, cl2, cl3, pl1, pl2, pl3
    Dim L
    cl1 = 1
    cl2 = 1
    cl3 = 1
    L = LenB(Binary)

    Do While cl1<=L
        pl3 = pl3 & Chr(AscB(MidB(Binary,cl1,1)))
        cl1 = cl1 + 1
        cl3 = cl3 + 1
        If cl3>300 Then
            pl2 = pl2 & pl3
            pl3 = ""
            cl3 = 1
            cl2 = cl2 + 1
            If cl2>200 Then
                pl1 = pl1 & pl2
                pl2 = ""
                cl2 = 1
            End If
        End If
    Loop
    BinaryToString = pl1 & pl2 & pl3
End Function

' Test it here:
Dim retCode
retCode = postFile ("D:\example.zip", "http://localhost:8080/fileupload/fileupload", "myexample.zip", "myfolder/mysubfolder/")

If retCode = 200 Then
  MsgBox "Upload was successful"

Else
  MsgBox "Upload was NOT successful, error code: " & retCode

End If

Files:

Further links:

Written by l8storm

September 21st, 2010 at 9:37 am

Check if XML is Well-Formed

without comments

On Linux, there is an easy way to check if an xml file is well formed:

xmllint <file>

The process will give you an error result if the XML is not well formed, otherwise prints the XML. You can also use xmllint as a code formatter for XML. See help for this.

Written by l8storm

September 15th, 2010 at 1:02 pm

Posted in IT Stuff

Tagged with ,

XSL and Line Breaks

without comments

To insert a line break in your XSL template, just use this line in your stylesheet:

<xsl:text>&#10;</xsl:text>

Links:

Written by l8storm

August 18th, 2010 at 8:59 am

Posted in IT Stuff

Tagged with , , ,

SUN/Oracle Java 6 Shipped with Buggy Xalan Libraries

without comments

Java has integrated XML functionality (Parser, DOM and XPath handling, …) starting with version 1.5. This is nice, because one does not need additional libraries to provide XML handling in the own code. The integrated XML functionality (compatible to JAXP 1.3) in Java 1.5 and 1.6 is provided by the Apache Xerces and Xalan libraries. Problematic is that SUN/Oracle uses quite old versions of these libraries:

  • Xerces: Xerces-J 2.6.2, released: 20.02.2004 (!)
  • Xalan: Xalan Java 2.6.0, released: 29.02.2004 (!)

This can lead to unexpected behavior of your code. For example, I am using an XSLT transformation in a software project. The code is correct and worked as expected – when running single threaded. But when starting in a multi threaded environment, even while taking care about the API comments to use one XML Transformer object for a thread, it sometimes worked and sometimes it does not. NullPointerExceptions were thrown by an internal (Xalan) class. Using more heap space delayed the problem, but did not solve it. The solution was an update of the much too old Xalan libraries included in Java 1.6. The problem disappeared after the upgrade.

The newest versions of Xerces an Xalan are currently:

  • Xerces: Xerces-J 2.10.0, released: 18.06.2010
  • Xalan: Xalan Java 2.7.1, released: November 2007

If you encounter problems using the SUN/Oracle Java built in implementation, you can replace the implementation by a new one this way:

  1. Download the Xalan and/or Xerces libraries (Xalan is shipped with the matching Xerces libs!)
  2. Unpack the *.jar files from the archive
  3. Place them somewhere in your applications lib/ folder
  4. Be sure that you call your application with the *.jar files on its classpath (java -cp xalan.jar:xercesimpl.jar:serializer.jar:xml-apis.jar MyApplication)

One may place the files in the $JAVA_HOME/jre/lib/ext/ folder instead. Why Java 1.6 is shipped with internal implementations of Xerces that are more that 6 years old is a question that only SUN/Oracle can answer. Even the newest Xalan version is three years old now, so one could expect that the libs are integrated in the JRE in the meantime…

How to find out the version of Xalan you are using:

  1. Download this JAVA program: Xalanv.java
  2. Compile it: javac Xalanv.java
  3. Run it: java Xalanv

Links:

Written by l8storm

August 15th, 2010 at 2:26 pm

Dropping everything from an Oracle schema

without comments

Sometimes you want to drop all objects from a schema created in Oracle without dropping the schema itself. For this, use the following SQL script:

Begin
for c in (select table_name from user_tables) loop
execute immediate ('drop table '||c.table_name||' cascade constraints');
end loop;
End;


Begin
for c in (select view_name from user_views) loop
execute immediate ('drop view '||c.view_name||' cascade constraints');
end loop;
End;


Begin
for c in (select distinct name from user_source where type='FUNCTION') loop
execute immediate ('drop function '||c.name);
end loop;
End;

Links:

Written by l8storm

August 3rd, 2010 at 10:58 am

Posted in IT Stuff

Tagged with , ,

How to install SQL Plus with Instant Client setup (10.2.0.4)

without comments

Normally, ou would install the complete Oracle Client to have a complete Oracle DB support. But if you only want to have the SQL Plus tool, it seems way too much efford to install the complete package, which is approx. 500 MB in the current version (10.2.0.4).

But there is a solution by Oracle that allows SQL Plus to run without using the big pack. You will need these files from the Oracle website (http://www.oracle.com/technology/software/products/sql/index.html):

(describes the 32bit installation of version 10.2.0.4 on RedHat / CentOS Linux 5.5; you will need to have an OTN account)

  1. oracle-instantclient-basiclite-10.2.0.4-1.i386.zip
  2. oracle-instantclient-sqlplus-10.2.0.4-1.i386.zip
  3. oracle-instantclient-jdbc-10.2.0.4-1.i386.zip (optional)

Installationsteps:

  • Unzip all files into a folder (e. g.: /opt/oracle/instantclient_10.2.0.4)
  • Extend the ~/.profile by this: export LD_LIBRARY_PATH=”/opt/oracle/instantclient_10.2.0.4 $LD_LIBRARY_PATH”

Pro:
- Slim installation

Con:
- There are some libraries missing in the windows version

Links:

Written by l8storm

Juli 7th, 2010 at 2:08 pm

Posted in IT Stuff

Tagged with , , , , ,

Howto handle large directories in Linux

without comments

Using the ls command in large directories will often lead your Linux shell to be unavailable for a couple of minutes or even hours. Also, using ls is then not sufficient to work within scripts.

Another possibility to cycle through a large directory or even subdirectories is a combination of the find and the read command:

find -maxdepth 1 -name '*.err' -print | while read f; do echo $f; mv $f ${f%err}rdy; done

for example will find all files ending with .err that are at most in the next directory level and renames them into .rdy.

Written by l8storm

Juni 18th, 2010 at 11:17 am

Posted in IT Stuff

Tagged with , , , , , , ,

SUN Java not included in Ubuntu 10.04

without comments

As a Linux user and a Java developer, it was important to know that Ubuntu does not include the SUN Java JRE and JDK in its “multiverse” repository anymore. Instead, the OpenJDK is used, which is not sufficient for all cases (e. g., the Maven plugin in Eclipse wants to have a JDK installed, but does not recognize OpenJDK as such an environment). See link for more info.

For me it was easiest to install SUN Java again. For doing this, open a console in Ubuntu and execute these commands:

Install the SUN Java JDK

  1. sudo add-apt-repository “deb http://archive.canonical.com/ lucid partner”
  2. sudo aptitude update
  3. sudo aptitude install sun-java6-jdk [or sun-java5-jdk; the JRE is available, too)

Use the JDK Java as the default Java runtime:

  1. sudo update-alternatives –install /usr/bin/java java /usr/lib/jvm/java-6-sun/bin/java 1
  2. sudo update-alternatives –set java /usr/lib/jvm/java-6-sun/bin/java

Use the JRE Java as the default Java runtime:

  1. sudo update-alternatives –set java /usr/lib/jvm/java-6-sun/jre/bin/java

Links:
Ubuntu 10.04 Release Notes: http://www.ubuntu.com/getubuntu/releasenotes/1004#Sun%20Java%20moved%20to%20the%20Partner%20repository

Written by l8storm

Mai 18th, 2010 at 6:19 pm

Posted in IT Stuff

Tagged with , , , , , , , ,