Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Mike Wilson www.whisperingwind.co.uk

« Newer Snippets
Older Snippets »
Showing 11-17 of 17 total

Basic ant script with vim & jikes

Apache ant build XML. This will use jikes in place of javac. Any compiler error output is formatted so that vim can parse it.

ant is a modern alternative to make. The build script is an XML file. It works particularly well with java. Download it for free from the Apache ant site. Most people find ant a lot nicer to live with than make.

<?xml version="1.0"?>

<project name="Hello" default="compile" basedir=".">
  <property name="name" value="Hello"/>
  <property name="version" value="1.0"/>

  <!-- Project directories.
  -->

  <property name="build" value="build"/>
  <property name="dist" value="dist"/>
  <property name="src" value="src"/>

  <!-- Compiler directives.
  -->

  <property name="optimize" value="off"/>
  <property name="deprecation" value="on"/>
  <property name="debug" value="on"/>

  <property name="build.compiler" value="jikes"/>
  <property name="build.compiler.emacs" value="true"/>

  <target name="init">
    <tstamp/>
    <mkdir dir="${build}"/>
  </target>

  <!-- Compile all the .java files from the source directory into
       the build directory.
  -->

  <target name="compile" depends="init">
    <javac srcdir="${src}" destdir="${build}" includes="**/*.java"
      debug="${debug}" deprecation="${deprecation}" optimize="${optimize}">
    </javac>
  </target>

  <target name="clean" depends="init">
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>

  <target name="test" depends="compile">
    <java classname="HelloWorld" fork="yes">
      <classpath>
        <pathelement location="${build}"/>
      </classpath>
    </java>
  </target>
</project>

Open a JDBC connection

This opens a JDBC connection givine a driver class name, database URL, user name and password. It is possible this method is deprecated and some newer procedure is preferred. I need to look into that.

    /*
    ** driverClass is the JDBC driver class name as a String.
    */

    try
    {
	Class.forName (driverClass);
	connection = DriverManager.getConnection (url,
	    userName, password);
	connection.setAutoCommit (false);
    }
    catch (SQLException ex)
    {
	/*
	** "Connect error"...
	*/
    }
    catch (java.lang.ClassNotFoundException ex)
    {
	/*
	** "Driver error"...
	*/
    }

Execute arbitary SQL in JDBC & get column names etc from the meta data

This will execute an arbitary SQL string in JDBC and extract the column names. I extracted this code from a much larger module I wrote years ago, so it isn't complete and hasn't been tested. You have been warned.

    /*
    ** connection is a java.sql.Connection obtained in the usual way.
    */

    PreparedStatement statement =
	connection.prepareStatement (sqlString);
    statement.setMaxRows (configModel.getMaxRows ());

    if (statement.execute ())
    {
	ResultSet resultSet = statement.getResultSet ();
	ResultSetMetaData metaData = resultSet.getMetaData ();

	/*
	** Get the column names.
	*/

	for (int i = 0 ; i < metaData.getColumnCount () ; i++)
	{
	    int columnType = metaData.getColumnType (i + 1);
	    String columnName = metaData.getColumnLabel (i + 1);

	    /*
	    ** Do something with columnType & columnName.
	    */

	}

	/*
	** Fetch the rows.
	*/

	while (resultSet.next ())
	{
	    String value;

	    for (int i = 0 ; i < metaData.getColumnCount () ; i++)
		value = resultSet.getString (i + 1);
	}
    }
    else
    {
	/*
	** Query was probably update/insert/delete.
	*/

	int rowCount = statement.getUpdateCount ();
    }

    statement.close ();

bash aliases

For old duffers like me who were used to earlier shells when bash came along.

# These save typing, I use them a lot

alias sd='cd -' > /dev/null
alias cx='chmod +x'
alias which="type -path"

# This goes back to Unix v7, some time around 1985 I think.

alias lf='ls -CF'

# From my 'C' shell days. Those were almost 20 years ago as well. Sigh.

alias h='fc -l -20'
alias r='fc -s'

# Because real men use 'vi' and why use character mode if you have X?

alias vi='gvim'

.inputrc to make bash command-line editing like ksh

I was used to ksh and vi when bash came along, so I wanted the behaviour to remain the same. Some may think this odd.

set editing-mode vi
set keymap vi

vim configuration - graphics mode

This is my .gvim rc, the configuration file for gvim, vim's graphics mode. vim is "vi improved", a vi clone and so much more.

set guifont=Monospace\ 11

" Overrides settings in vim's configuration. I prefer minimal colour.

hi Normal			guifg=Black		guibg=White
hi NonText			guifg=Black		guibg=White
hi Comment	gui=none	guifg=DarkGray
hi Constant	gui=none	guifg=Magenta
hi Identifier	gui=none	guifg=DarkGreen
hi Statement	gui=none	guifg=Blue
hi PreProc	gui=none	guifg=Blue
hi Type		gui=none	guifg=Blue
hi Special	gui=none	guifg=DarkGreen
hi Ignore	gui=none	guifg=DarkGreen
hi Error	gui=none	guifg=DarkGreen
hi Todo		gui=none	guifg=DarkGreen
hi Cursor	gui=none	guifg=White		guibg=Black

autocmd GUIEnter * winpos 66 28
autocmd GUIEnter * winsize 80 45

" What is this menu for? No documentation, something to do with 'C'
" or C++ perhaps? Dunno, but I don't want it.

aunmenu Bicycle\ Repair\ Man

" If I have Python, create a dummy project menu and exececute the
" vimproject script.

if has ("python")
    if filereadable ("/home/mrw/bin/vimproject.py")
	amenu Project.Dummy dummy
	pyfile /home/mrw/bin/vimproject.py
    endif
endif

vim configuration

This is my .vimrc file -- configuration options for vim, the "vi improved" text editor for grown ups.

:autocmd!

set nohlsearch

" Press F2 to word-wrap a block of text. It's almost like using Word
" Star all over again.

map #2 !}fmt -65

" Personally, I wouldn't use a C++ keyword as a Java identifier, but
" if someone else does, I don't want an ugly display, so turn off
" flagging of this as an error:

let java_allow_cpp_keywords=1

set autoindent
set cmdheight=2

" Strewth, what a mess. Copied from the vim docs, if memory serves.

set comments=s:/*,mb:**,ex:*/,://,b:#,b:##,:%,:XCOMM,n:>,fb:-

set formatoptions=orc
set history=20
set incsearch
set ignorecase
set keywordprg=
set mouse=a
set mousehide
set mousemodel=popup_setpos
set nowrapscan
set path=.,/usr/include,/usr/local/include
set smartcase
set nosmartindent
set smarttab
set showmode
set textwidth=70
set viminfo='50,\"10000,n~/.viminfo
set wildchar=9
set wildignore+=*.class,*.pyc
set wildmenu

syntax on

:autocmd FileType *		set shiftwidth=4
:autocmd FileType xml,html	set shiftwidth=2
:autocmd FileType java,c,cc,cpp	set nocindent

set makeprg=ant

" Bleurgh! This makes sense of ant/jikes error messages so the ":make"
" command works. But, I ask you, how the f**k can anyone make sense of
" that mess?

set efm=\ %#[javac]\ %#%f:%l:%c:%*\\d:%*\\d:\ %t%[%^:]%#:%m,\%A\ %#[javac]\ %f:%l:\ %m,%-Z\ %#[javac]\ %p^,%-C%.%#

" For the style of comments I like in 'C', C++ and Java.

autocmd BufNewFile,BufRead *.java set comments=s:/*,mb:**,ex:*/
autocmd BufNewFile,BufRead *.c set comments=s:/*,mb:**,ex:*/
autocmd BufNewFile,BufRead *.cc set comments=s:/*,mb:**,ex:*/

« Newer Snippets
Older Snippets »
Showing 11-17 of 17 total