Logging JDBC Statements
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:
- Place the
log4jdbc4-1.2beta2.jarinto your classpath - If not already done, place the slf4j-api-*.jar and the slf4j-log4j12-*.jar (for Log4J logging) into your classpath
- Change your JDBC driver class to
net.sf.log4jdbc.DriverSpy - Change your JDBC url to jdbc:log4jdbc:…
- Configure Log4J (or your log framework) to log the category “jdbc.sqltiming” in INFO mode
Links:
- JDBC proxy driver: http://code.google.com/p/log4jdbc/
- Simple Logging Facade for Java: http://slf4j.org
- Apache Log4J: http://logging.apache.org/log4j/
- Forum discussion about how to log prepared statements: http://stackoverflow.com/questions/218113/logging-preparedstatements-in-java
Windows as NTP server
Want to use MS Windows as an NTP time server?
- 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
- Exeute the file
ActivateNTP.regas Administrator to change the registry informations - Open a command line: Start -> Run… -> cmd.exe
- Restart the time service:
net stop w32time net start w32time
- Be sure the firewall enables all clients to access the NTP service (UDP 123)
Links:
- How can I enable a Network Time Protocol (NTP) server: http://www.windowsitpro.com/article/time-synchronization/how-can-i-enable-a-network-time-protocol-ntp-server-.aspx
Uploading Files to a Server Using VBA
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:
- Server: fileupload.zip
Further links:
- Send binary data to URL: http://www.binarynow.com/pdf-conversion/simple-vbscript-vb-vbs-vba-converting-documents-doc-xls-ppt-eml-to-pdf-from-soap-web-service/
- Another possibility to send POST data: http://www.ms-office-forum.de/forum/showthread.php?t=208852
- How to work with binary files: http://www.motobit.com/tips/detpg_read-write-binary-files/
- MSDN XMLHTTPRequest reference: http://msdn.microsoft.com/en-us/library/ms759148%28v=VS.85%29.aspx
- Using the XMLHTTP object in VBA: http://scriptorium.serve-it.nl/view.php?sid=40
- Simple example how to do a HTTP post: http://stackoverflow.com/questions/158633/how-can-i-send-an-http-post-request-to-a-server-from-excel-using-vba
Check if XML is Well-Formed
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.
XSL and Line Breaks
To insert a line break in your XSL template, just use this line in your stylesheet:
<xsl:text> </xsl:text>
Links:
- Thanks to rabidrobot in this post: http://forum.umbraco.org/yaf_postst2991_How-to-output-line-breaks-in-XSLT.aspx
SUN/Oracle Java 6 Shipped with Buggy Xalan Libraries
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:
- Download the Xalan and/or Xerces libraries (Xalan is shipped with the matching Xerces libs!)
- Unpack the *.jar files from the archive
- Place them somewhere in your applications lib/ folder
- 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:
- Download this JAVA program: Xalanv.java
- Compile it:
javac Xalanv.java - Run it:
java Xalanv
Links:
- Infos about SUN Java integrated JAXP 1.3 implementation: http://download-llnw.oracle.com/javase/1.5.0/docs/guide/xml/jaxp/JAXP-Compatibility_150.html
- Find out the Xerces version you are using: http://cafe.elharo.com/xml/what-version-of-xerces-are-you-using/
- Apache Xalan Homepage: http://xalan.apache.org/index.html
- Apache Xerces-J Homepage: http://xerces.apache.org/xerces2-j/
- Jakarta release infos, including release dates of Xerces and Xalan: http://jakarta.apache.org/site/news/index.html
- Getting an idea what the
lib/ext/folder is for: http://mindprod.com/jgloss/ext.html
Dropping everything from an Oracle schema
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:
- Delete all tables in the OTN Discussion Forums: http://forums.oracle.com/forums/thread.jspa?threadID=614090
How to install SQL Plus with Instant Client setup (10.2.0.4)
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)
- oracle-instantclient-basiclite-10.2.0.4-1.i386.zip
- oracle-instantclient-sqlplus-10.2.0.4-1.i386.zip
- 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:
- SQL Plus installation guide: http://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ape.htm#sthref3926
- Download page: http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html
- Configuration Help: http://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ape.htm#CHDJCDFA
Howto handle large directories in Linux
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.
SUN Java not included in Ubuntu 10.04
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
- sudo add-apt-repository “deb http://archive.canonical.com/ lucid partner”
- sudo aptitude update
- sudo aptitude install sun-java6-jdk [or sun-java5-jdk; the JRE is available, too)
Use the JDK Java as the default Java runtime:
- sudo update-alternatives –install /usr/bin/java java /usr/lib/jvm/java-6-sun/bin/java 1
- sudo update-alternatives –set java /usr/lib/jvm/java-6-sun/bin/java
Use the JRE Java as the default Java runtime:
- 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