|
Re: Why I don't like Java
|
Posted: Feb 15, 2007 7:53 AM
|
|
> Today I wrote a ten-line Perl script that reads a > comma-separated file and, after massaging the data, puts > that info into a database. Most of those ten lines had to > do with ODBC.
Here's how I'd write this in Java, covering all the exceptional cases and using my http://sqlfactory.dev.java.net projects support for managing JDBC reliably. I count 26 lines, and I didn't actually try to reduce the number of lines needed.
public static void main( String args[] ) throws IOException, SQLException { FileReader fr = new FileReader( args[0] ); try { BufferedReader rd = new BufferedReader( fr ); String ln; DatabaseManager mgr = SQLFactory.getConnection( "footprint", url, drvr, user, passwrd, conntime, transtime ); PreparedStatement ps = mgr.prepareStatement( "insert into tbl (f1,f2,f3,f4,f5) "+ "values (?,?,?,?,?)"; try { while( ( ln = rd.readLine() ) != null ) { String arr[] = ln.split(","); for( int i = 0; i < arr.length; ++i ) { ps.setObject( i+1, arr[i] ); } ps.executeUpdate(); } } finally { mgr.release(); } } finally { fr.close(); } }
I think that there's not a lot of pain here. Instead, there are issues that just take code to work through. I'm taking advantage of JDBCs auto field type conversion through the PreparedStatement.
|
|