Class DatabaseWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public class DatabaseWriter
    extends java.io.Writer
    Writes tab-separated Strings to a database table
    Author:
    Andrew Varley
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.sql.Connection con  
      protected int fields  
      protected java.lang.StringBuffer sb  
      protected java.lang.String table  
      • Fields inherited from class java.io.Writer

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      DatabaseWriter()
      Construct an empty DatabaseWriter for testing purposes
      DatabaseWriter​(java.sql.Connection con, java.lang.String table)
      Construct a DatabaseWriter
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close this Writer
      protected java.lang.String createSQLStatement​(java.lang.String table, java.lang.String row)
      Creates an SQL String suitable for initialising a PreparedStatement
      void flush()
      Flush completed rows to the database
      void write​(char[] cbuff, int off, int len)
      Write a portion of an array to the database
      • Methods inherited from class java.io.Writer

        append, append, append, nullWriter, write, write, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • con

        protected java.sql.Connection con
      • table

        protected java.lang.String table
      • sb

        protected java.lang.StringBuffer sb
      • fields

        protected int fields
    • Constructor Detail

      • DatabaseWriter

        public DatabaseWriter()
        Construct an empty DatabaseWriter for testing purposes
      • DatabaseWriter

        public DatabaseWriter​(java.sql.Connection con,
                              java.lang.String table)
        Construct a DatabaseWriter
        Parameters:
        con - a database Connection
        table - the table to write to
        Throws:
        java.lang.NullPointerException - if either con or table are null
    • Method Detail

      • write

        public void write​(char[] cbuff,
                          int off,
                          int len)
                   throws java.io.IOException
        Write a portion of an array to the database
        Specified by:
        write in class java.io.Writer
        Parameters:
        cbuff - the array of characters
        off - the start point
        len - the number of characters to write
        Throws:
        java.io.IOException - if an error occurs when writing to the underlying database
      • flush

        public void flush()
        Flush completed rows to the database
        Specified by:
        flush in interface java.io.Flushable
        Specified by:
        flush in class java.io.Writer
      • close

        public void close()
        Close this Writer
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Writer
      • createSQLStatement

        protected java.lang.String createSQLStatement​(java.lang.String table,
                                                      java.lang.String row)
        Creates an SQL String suitable for initialising a PreparedStatement
        Parameters:
        table - the table to insert in
        row - an example row
        Returns:
        the SQL String
        Throws:
        java.lang.NullPointerException - if any arguments are null