Register now and start sharing your code snippets.
How to parse an RSS or Atom feed with the ROME Java library
Java posted 2 months ago by christian
This is a simple example of how to use the ROME library to parse feeds:
1 import com.sun.syndication.io.*; 2 import com.sun.syndication.feed.synd.*; 3 import java.net.URL; 4 import java.util.*; 5 6 public class RomeParserTest { 7 8 public static void main(String args[]) { 9 try { 10 SyndFeedInput sfi = new SyndFeedInput(); 11 12 String urls[] = { 13 "...", 14 "..." 15 }; 16 17 for(String url:urls) { 18 SyndFeed feed = sfi.build(new XmlReader(new URL(url))); 19 20 List entries = feed.getEntries(); 21 22 System.out.println(feed.getTitle()); 23 System.out.println(entries.size()); 24 } 25 } catch (Exception ex) { 26 throw new RuntimeException(ex); 27 } 28 } 29 }
How to read UTF-8 data from an Oracle BLOB column with Java and JDBC
Java posted 11 months ago by christian
This example works with Oracle:
1 private String getBlobAsString(Blob blob) 2 { 3 StringBuffer result = new StringBuffer(); 4 5 if ( blob != null ) 6 { 7 int read = 0; 8 Reader reader = null; 9 char[] buffer = new char[1024]; 10 11 try 12 { 13 reader = new InputStreamReader(blob.getBinaryStream(), "UTF-8"); 14 15 while((read = reader.read(buffer)) != -1) 16 { 17 result.append(buffer, 0, read); 18 } 19 } 20 catch(SQLException ex) 21 { 22 throw new RuntimeException("Unable to read blob data.", ex); 23 } 24 catch(IOException ex) 25 { 26 throw new RuntimeException("Unable to read blob data.", ex); 27 } 28 finally 29 { 30 try { if(reader != null) reader.close(); } catch(Exception ex) {}; 31 } 32 } 33 34 return result.toString(); 35 }
Then use the method like this:
1 ResultSet resultSet = your JDBC result set; 2 3 String utf8 = getBlobAsString(resultSet.getBlob("xml")); 4
How to write UTF-8 data to an Oracle BLOB column with Java and JDBC
Java posted 11 months ago by christian
This example works with Oracle:
1 private Blob getBlob(Connection connection, String data) 2 { 3 BLOB blob = BLOB.createTemporary(connection, true, BLOB.DURATION_SESSION); 4 5 try 6 { 7 blob.open(BLOB.MODE_READWRITE); 8 blob.putBytes(1, data.getBytes("UTF-8")); // Consider streaming, if data size is unknown. Note that setBytes doesn't work 9 } 10 catch(UnsupportedEncodingException ex) 11 { 12 throw new RuntimeException("Unable to get a blob for '" + data + "'", ex); 13 } 14 catch(SQLException ex) 15 { 16 throw new RuntimeException("Unable to get a blob for '" + data + "'", ex); 17 } 18 finally 19 { 20 try { if(blob != null) blob.close(); } catch(Exception ex) {}; 21 } 22 }
Then use the method like this:
1 Connection connection = getConnection(); 2 PreparedStatement statement = getPreparedStatement(yer sequel); 3 4 statement.setBlob(1, getBlob(connection, <Mao's Little Red Book>));
Split a URL into protocol, domain, port and URI using regular expressions
Java posted 12 months ago by christian
1 // Split URL into protocol, domain, port and URI 2 Pattern pattern = Pattern.compile("(https?://)([^:^/]*)(:\\d*)?(.*)?"); 3 Matcher matcher = pattern.matcher(url); 4 5 matcher.find(); 6 7 String protocol = matcher.group(1); 8 String domain = matcher.group(2); 9 String port = matcher.group(3); 10 String uri = matcher.group(4);
Format currency by Locale in Java
Java posted about 1 year ago by marko
There are a few ways to format currency to your own locale. If you want the formatted result to appear without the currency sign then this is probably one of the better ways to do it. In the real world you would dig the locale from the user’s request or session and pass it as a parameter to the method.
1 public static String formatCurrency(String amount) { 2 NumberFormat decimalFormat = NumberFormat.getInstance(new Locale("fi", "FI", "")); 3 decimalFormat.setMinimumFractionDigits(2); 4 return decimalFormat.format(amount); 5 }