![]() |
Woodstock Dataprovider Entity Example |
The project was updated using NetBeans 6.5.1.
The code for the project can be found on Bitbucket here: WoodstockJPAApplication
![]() |
Woodstock Dataprovider Entity Example |
![]() |
Multiple Selection Table |
![]() |
Single Selection Table |
![]() |
Collapsible Group Table Example |
![]() |
Master-Detail Example |
Formatter
used with the java.util.logging.Logger
to generate a standard output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | package com.bluelotussoftware.logging; import java.text.SimpleDateFormat; import java.util.Date; import java.util.logging.Formatter; import java.util.logging.LogRecord; /** * <p> * A formatter that generates a standard message as shown below.</p> * <pre> * [17/10/2014 22:17:01.348 EDT] com.example.project.App INFO main This is informational message! * </pre> * * @author John Yeary <jyeary@bluelotussoftware.com> * @version 1.0 */ public class PragmaticFormatter extends Formatter { private static final String DATEFORMATPATTERN = "mm/dd/yyyy HH:mm:ss.SSS z" ; private SimpleDateFormat dateFormatter = new SimpleDateFormat(DATEFORMATPATTERN); /** * Default constructor. */ public PragmaticFormatter() { } /** * {@inheritDoc} */ public String format( final LogRecord record) { StringBuilder sb = new StringBuilder(); sb.append( "[" ).append(dateFormatter.format( new Date(record.getMillis()))).append( "] " ); sb.append(record.getSourceClassName()).append( " " ); sb.append(record.getLevel().getName()).append( " " ); sb.append(record.getSourceMethodName()).append( " " ); sb.append(record.getMessage()).append( "\n" ); return sb.toString(); } /** * This returns the {@link SimpleDateFormat} used by the {@link Formatter}. * * @return {@link SimpleDateFormat} used by the {@link Formatter}. */ public SimpleDateFormat getDateFormatter() { return dateFormatter; } /** * Sets the {@link SimpleDateFormat} used by the {@link Formatter} * * @param dateFormatter The {@link SimpleDateFormat} to be set. */ public void setDateFormatter( final SimpleDateFormat dateFormatter) { this .dateFormatter = dateFormatter; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.example.project; import com.bluelotussoftware.logging.PragmaticFormatter; import java.util.logging.ConsoleHandler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; public class App { private static final Logger LOGGER = Logger.getLogger(App. class .getName()); public App() { } public static void main(String[] args) { ConsoleHandler handler = new ConsoleHandler(); handler.setFormatter( new PragmaticFormatter()); LOGGER.addHandler(handler); LOGGER.log( new LogRecord(Level.INFO, "This is informational message!" )); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static String XMLEncode( final String text) { StringBuilder result = new StringBuilder(); for ( int i = 0 ; i < text.length(); i++) { char value = text.charAt(i); if (!((value >= 'a' && value <= 'z' ) || (value >= 'A' && value <= 'Z' ) || (value >= '0' && value <= '9' ))) { MessageFormat.format( "&#{0};" , ( int ) value); result.append(MessageFormat.format( "&#{0};" , ( int ) value)); } else { result.append(value); } } return result.toString(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | package com.bluelotussoftware.database.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * Creates an Image BLOB table on the target database. * * @author John Yeary * @version 1.0 */ public class CreateImagesTable { // The URL connection string to the MySQL Database // This can be modified to attach to another database // The user and password need to be modified accordingly // SQL string to create images table private final String SQL = "CREATE TABLE IMAGES (ID INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT," + "FILENAME VARCHAR(255) NOT NULL, " + "BINARYIMAGEDATA MEDIUMBLOB NOT NULL);" ; // This needs to be changed if you use another database driver private final String driver = "com.mysql.jdbc.Driver" ; private Connection con = null ; private Statement stmt = null ; /** * Creates a new instance of CreateImagesTable */ public CreateImagesTable() { } /** * Creates an Image table for BLOB images on the target database. */ public void create() { try { Class.forName(driver); con = DriverManager.getConnection(URL); stmt = con.createStatement(); stmt.execute(SQL); } catch (ClassNotFoundException e) { System.err.println(e.getMessage()); } catch (SQLException e) { System.err.println(e); } finally { try { if (con != null ) { con.close(); } } catch (SQLException ignored) { } } } public static void main(String args[]) { CreateImagesTable table = new CreateImagesTable(); table.create(); } } |
cat
the contents of a file in Java. I thought for a second and here is what I came up with. I thought I would just share it. Note: This is my 30s answer, and probably could be cleaned up.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.bluelotussoftware.utilities; import java.io.*; import java.text.MessageFormat; public class Cat { public static void cat(File named) { String line; try (RandomAccessFile input = new RandomAccessFile(named, "r" )) { while ((line = input.readLine()) != null ) { System.out.println(line); } } catch (FileNotFoundException e) { System.out.println(MessageFormat.format( "File: {0} not found." , named.getName())); } catch (IOException e) { System.out.println(e.getMessage()); } finally { } } public static void main(String[] args) { Cat.cat( new File(args[ 0 ])); } } |