Tutorial

How to read and write CLOB fields

The Character Large Object (or CLOB) is a commonly found in databases and used to store high quantity of characters.

At MySQL, for example, this field is called MEMO.

Writing CLOB field

The method setAsciiStream of PreparedStatement allow to pass data to a CLOB.

ps.setAsciiStream(bindPosition, inputStream, textLength);

bindPosition – Position in PreparedStatment’s CLOB field.
inputStream – Used to pass data.
textLength – Data (text) length.

Full code:

String sql = "INSERT INTO TABLE (text) VALUES(?)";
		try{
			String txt = readTxtFile();
			ByteArrayInputStream bais = new ByteArrayInputStream(txt.getBytes());

			PreparedStatement ps = conexao.prepareStatement(sql);
			//CLOB is '?' at first position
			ps.setAsciiStream(1, bais, txt.length());

			ps.execute();

			ps.close();
		}catch (Exception e) {
			e.printStackTrace();
		}

Retrieving CLOB data

We’re using SELECT clause, this clause returns a ResultSet to fetch data and, calling getClob method, passing column name or your position in the query, it give to you a Clob object.

rs.getClob("xml");

Full code:

String sql = "SELECT xml FROM TEST";
		try{
			PreparedStatement ps = conexao.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();

			while(rs.next()){
				Clob clob = rs.getClob("xml");
				BufferedReader reader = new BufferedReader(clob.getCharacterStream());
				StringBuffer strBuf = new StringBuffer();

				String linha = null;
				while((linha = reader.readLine()) != null){
					strBuf.append(linha);
//Character.LINE_SEPARATOR insert break line
					strBuf.append((char)Character.LINE_SEPARATOR);
				}

				System.out.println("=========== CLOB ===========");
				System.out.println(strBuf.toString());
			}

			rs.close();
			ps.close();
		}catch (Exception e) {
			e.printStackTrace();
		}

That’s it! Easy and painless ;)

Download full code here

To write a BLOB see this tutorial.


Fix for “JDK not found” error for Android SDK at Windows 7 x64

If you’re trying to install Android SDK on Windows 64 bits and is receiving the error message “JDK not found”, the solution found by http://www.eighthourlunch.com/node/161, basicaly insert some entries at Windows registry, after this, the Google Android SDK will found your JDK instalation.

The .REG file are available here .

Before run file, open it with notepad and change path “C:\\Program Files\\Java\\jdk1.6.0_23″ to your JDK directory, and the path “C:\\Program Files\\Java\\jre6″ to your JRE directory.

This fix has worked for me. ;)


Solving problems with removal of the external HD in Windows 7

Some times I had problems when disconnected my external HD using Windows’ s Removal Device Assistant, the most frequently was “This device is currently in use. Close any programs or windows that…”. This message appeared even with all programs closed.

After checked Windows settings, process and services, I noticed that the problem was in service “Windows Media Player Network Sharing Service”, forcing me to stop it before remove the device.

But still, this solution not is right, then I found the most right:

The service “Windows Media Player Network Sharing Service” is responsible to share your media files (musics and videos) in your network or in the internet (like your name sounded. :) ). This is default setting in Windows 7, to disable it…

Click in Start menu, type “Network and sharing center” and select the application. Like show in the image

Click “Choose homegroup and sharing options”

Clique na opção “Escolher opções de grupo doméstico e de compartilhamento”

Now click on “Choose media streaming options…”

Change option of item “Media programs on this PC and remote conections…” to “Blocked”.

Finish! From now you can remove your external hard drive or other removable storage devices without problems.

;)


Enabling Windows Media Player bar in Windows 7 taskbar

Unfortunately Windows Media Player 12,  already installed with Windows 7, has no support to minimize as toolbar in the taskbar. To enable this bar, just need copy a file from Windows Media Player for Windows Vista. Now I will show how do this:

1. You will need of a dll called wmpband.dll, that come with Windows Vista. If you not own it, download here (557) (this ZIP contains 32 and 64 bits versions);

2. Copy the file (32 or 64 bits, depends your Windows) to Windows Media Players installation directory, in my case “C:\Program Files\Windows Media Player“;

3. Now check if “Windows Media Player Network Sharing Service service is stopped. To do this, click in Start and, in the search box, type “Services” and click in the respective list item. Look for service and stop it, if running.

4. Open the Command Prompt with administrator privileges. Star > All Programs > Accessories > right click in “Command Prompt” and click “Run as Administrator”.

5. Type command regsvr32C:\Program Files\Windows Media Player\wmpband.dll“, replace the path of the directory where the dll is.

6. Open Windows Media Player and minimize it. Then right click in the taskbar > Toolbars > select Windows Media Player.

6. Abra o Windows Media Player e então o minimize-o. Depois clique com o botão direito na barra de tarefas > Barra de Ferramentas > Windows Media Player.

7. And voilá.

Microsoft should have kept this feature natively, I know people who use this resource, I inclusive.

natively


Setting up internet access through proxy server

Today I had a trouble in my workplace when a client application, running through a terminal (DOS prompt), not reach the server hosted in the internet, this is because the company where I work use proxy.

After some tries I found two solutions to solve my access problem: first was pass as Java parameters the proxy’s configurations.

$ java -Dhttp.proxyHost=serv -Dhttp.proxyPort=port -Dhttp.proxyUser=user -Dhttp.proxyPassword=pass  ClassJava

-Dhttp.proxyHost = IP or host name of server proxy
-Dhttp.proxyPort = Proxy port
-Dhttp.proxyUser = User
-Dhttp.proxyPassword = Password

This way the class ClassJava will have access to internet through proxy server.

Another way, was insert the settings in own class:

public static void main(String[] args){
...
System.getProperties().put("proxySet", "true");
System.getProperties().put("http.proxyHost", "serv");
System.getProperties().put("http.proxyPort", "port");
System.getProperties().put("http.proxyUser", "user");
System.getProperties().put("http.proxyPassword", "password");
...
}

proxySet = Connect, or not, through proxy server
http.proxyHost = IP or host name of server proxy
http.proxyPortt = Proxy port
http.proxyUse = User
http.proxyPassword = Password

Using this resource you can set your application to access the internet through proxy server.

I hope help you.  Bye! ;)


  • AdSense

  • Copyright © 1996-2010 André L. S.. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress