: Java




Convert Text To UniCode

July 1st, 2008 admin Posted in Java Comments Off

private static String convertToUniCode(String inputString) throws Exception {
	String uniCodeString  = "";
	String prefix = "\\u";
	String unicodebyte = new String(inputString.getBytes("UTF-8"),"UTF-8");
	char[] charArray = unicodebyte.toCharArray();
	StringBuffer sb = new StringBuffer();
	for(int i=0; i<charArray.length; i++) {
		//System.out.print("\\u"+Integer.toHexString(charArray[i]));
		String hex = Integer.toHexString(charArray[i]);
		if (hex.length()==2 ) {
			hex = "00" + hex;
		} else if (hex.length()==3) {
			hex = "0" + hex;
		}
		sb.append(prefix + hex);
	}
	uniCodeString = sb.toString();
	return uniCodeString;
}
AddThis Social Bookmark Button

Database Connection and SQL Query

July 1st, 2008 admin Posted in Java Comments Off

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DbUtils {

public static void main(String[] args) {
	try {
		executeQuery(”select * from persons”);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

public static void executeQuery(String sql) throws Exception {
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;
	try {
		conn = getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery(sql);
		while (rs.next()) {
			String oid = (String) rs.getObject(”oid”);
			System.out.println(”oid = ” + oid);
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		rs.close();
		stmt.close();
		conn.close();
	}
}

public static Connection getConnection() throws Exception {
	Connection conn = null;
	String driverName = “org.postgresql.Driver”;
	String databaseUrl = “jdbc:postgresql://mymachine/mydatabase”;
	String user = “user”;
	String pwd = “password”;
	Driver driver = (Driver) Class.forName(driverName).newInstance();
	DriverManager.registerDriver(driver);
	conn = DriverManager.getConnection(databaseUrl, user, pwd);
	return conn;
}	

}
AddThis Social Bookmark Button

Image Processing

July 1st, 2008 admin Posted in Java Comments Off


import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.math.BigDecimal;

import javax.imageio.ImageIO;

public class ImageTest01 {

public static void main(String[] args) {

	BufferedImage img = loadImage(”C:\\_Temp\\duke-real.gif”);
	float w = img.getWidth();
	float h = img.getHeight();

	System.out.println(”w = ” + w + “, h =” + h);

	int newH = 50;
	float scalingFactor = newH/h;
	//int newW = (new BigDecimal(w*scalingFactor)).round
	int newW = Math.round(w*scalingFactor);
	BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
	//ImageIO.read(new File(file));
	Graphics2D g = dimg.createGraphics();
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	g.drawImage(img, 0, 0, newW, newH, 0, 0, Math.round(w), Math.round(h), null);
	g.dispose();

	System.out.println(”newW = ” + newW + “, newH =” + newH);

	saveImage(dimg, “C:\\_Temp\\duke-new.gif”);

	System.out.println(”Done”);

}

public static BufferedImage loadImage(String file) {
	BufferedImage bimg = null;
	try {
		bimg = ImageIO.read(new File(file));
	} catch (Exception e) {
		e.printStackTrace();
	}
	return bimg;
}

public static void saveImage(BufferedImage img, String file) {
	try {
		File imageFile = new File(file);
		String format = imageFile.getName().split(”\\.”)[1];
		ImageIO.write(img, format, imageFile);
	} catch (Exception e) {
		e.printStackTrace();
	}
}	

}
AddThis Social Bookmark Button

Load Object Bytes from a URL

July 1st, 2008 admin Posted in Java Comments Off

private static byte[] loadFromURL(URL url) throws IOException {
	   ByteArrayOutputStream baos = new ByteArrayOutputStream();
	   InputStream is = url.openStream();
	   int r;
	   byte[] buffer = new byte[1000];

	   while ((r = is.read(buffer)) >= 0) {
	     if (r == 0)
	    	 continue;
	     baos.write(buffer, 0, r);
	   }
	   is.close();
	   baos.close();
	   return baos.toByteArray();
}
AddThis Social Bookmark Button

Parse XML into MessageElement

July 1st, 2008 admin Posted in Java Comments Off

import java.io.StringReader;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.axis.message.MessageElement;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;

public class XmlTest {
public static void main(String[] args) {
try {
MessageElement[] m = new MessageElement[1];
String xmlString = "<Results>" +
					"<Adjustments>" +
						"<Adjustment>" +
							"<RebuildAdjustmentID>16</RebuildAdjustmentID>" +
							"<IsBasicAdjustment>true</IsBasicAdjustment>" +
							"<AdjustmentType>stone</AdjustmentType>" +
							"<Title>External walls</Title>" +
						"</Adjustment>" +
					"</Adjustments>" +
					"</Results>";
Document XMLDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xmlString)));
//m[0] = (MessageElement) XMLDoc.getDocumentElement();
Element element = XMLDoc.getDocumentElement();
m[0] = new MessageElement(element);
System.out.println("Name = " + m[0].getName());
Iterator it = m[0].getChildElements();
while (it.hasNext()) {
	MessageElement me = (MessageElement) it.next();
	System.out.println("Name = " + me.getName());
	Iterator it2 = me.getChildElements();
	while (it2.hasNext()) {
		MessageElement me2 = (MessageElement) it2.next();
		System.out.println("Name = " + me2.getName());
	}
}

System.out.println("DONE");
} catch (Exception e) {
	e.printStackTrace();
}
}
}
AddThis Social Bookmark Button

Similar Posts