java - Control output file of a script -
in continuation this question.
i need in making tabletocsv (function converts .html table csv), render code database, rather .csv. created bufferedreader, converts .csv database, can't 2 connect. please make output file of tabletocsv go bufferedreader.
tabletocsv
* [tabletocsv.java] * * summary: extracts rows in csv tables csv form. extracts data tables in input. output in xxx.csv. * * copyright: (c) 2011-2014 roedy green, canadian mind products, http://mindprod.com * * licence: software may copied , used freely purpose military. * http://mindprod.com/contact/nonmil.html * * requires: jdk 1.6+ * * created with: jetbrains intellij idea ide http://www.jetbrains.com/idea/ * * version history: * 1.0 2011-01-23 initial version. * 1.1 2011-01-25 allow specify encoding */ package com.mindprod.csv; import com.mindprod.common11.misc; import com.mindprod.entities.deentifystrings; import com.mindprod.hunkio.hunkio; import java.io.bufferedoutputstream; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstreamwriter; import java.io.printwriter; import java.nio.charset.charset; import static java.lang.system.err; import static java.lang.system.out; /** * extracts rows in csv tables csv form. extracts data tables in input. output in xxx.csv. * <p/> * use: java.exe com.mindprod.tabletocsv xxxx.html * strips tags , converts entities utf-8 characters. * * @author roedy green, canadian mind products * @version 1.1 2011-01-25 allow specify encoding * @since 2011-01-23 */ public final class tabletocsv { // ------------------------------ constants ------------------------------ /** * how use command line */ private static final string usage = "tabletocsv needs name of html file on commandline, " + "nothing else. output in xxx.csv."; // -------------------------- public instance methods -------------------------- /** * constructor convert html table csv. strips out entities , tags. * * @param file csv file packed remove excess space , quotes. * @param separatorchar field separator character, ',' in north america, * ';' in europe , '\t' * tab output file. tab input file. * note 'char' not "string". * @param quotechar character used quote fields containing awkward chars. * @param commentchar character treat comments. * @param encoding encoding of input , output file. * * @throws java.io.ioexception if problems reading/writing file */ @suppresswarnings({ "weakeraccess" }) public tabletocsv( final file file, final char separatorchar, final char quotechar, final char commentchar, final charset encoding ) throws ioexception { string outfilename = misc.getcanorabspath( file ); outfilename = outfilename.substring( 0, outfilename.length() - 5 ) + ".csv"; final file outfile = new file( outfilename ); // writer, quotelevel, separatorchar, quotechar, commentchar, trim final printwriter pw = new printwriter( new outputstreamwriter( new bufferedoutputstream( new fileoutputstream( outfile ), 32768 ), encoding ) ); final csvwriter w = new csvwriter( pw, 0 /* minimal */, separatorchar, quotechar, commentchar, true ); // read entire html file ram. string big = hunkio.readentirefile( file, encoding ); int = 0; // our parser forgiving, works if </td> </tr> missing. while ( true ) { // find <tr final int trstart = big.indexof( "<tr", ); if ( trstart < 0 ) { break; } = trstart + 3; final int trend = big.indexof( '>', ); if ( trend < 0 ) { break; } while ( true ) { // search <td>...</td> final int tdstart = big.indexof( "<td", ); if ( tdstart < 0 ) { break; } = tdstart + 3; final int tdend = big.indexof( '>', ); if ( tdend < 0 ) { break; } = tdend + 1; final int startfield = tdend + 1; final int slashtdstart = big.indexof( "</td", ); final int lookaheadtd = big.indexof( "<td", ); final int lookaheadslashtr = big.indexof( "</tr", ); final int lookaheadtr = big.indexof( "<tr", ); int endfield = integer.max_value; if ( slashtdstart >= 0 && slashtdstart < endfield ) { endfield = slashtdstart; } if ( lookaheadtd >= 0 && lookaheadtd < endfield ) { endfield = lookaheadtd; } if ( lookaheadslashtr >= 0 && lookaheadslashtr < endfield ) { endfield = lookaheadslashtr; } if ( lookaheadtr >= 0 && lookaheadtr < endfield ) { endfield = lookaheadtr; } if ( endfield == integer.max_value ) { break; } = endfield + 3; final int slashtdend = big.indexof( '>', ); if ( slashtdend < 0 ) { break; } string field = big.substring( startfield, endfield ); field = deentifystrings.flattenhtml( field, ' ' ); w.put( field ); = slashtdend + 1; final int looktd = big.indexof( "<td", ); final int looktr = big.indexof( "<tr", ); if ( looktr >= 0 && looktr < looktd || looktd < 0 ) { break; } } w.nl(); } out.println( w.getlinecount() + " rows extracted table csv" ); w.close(); } // --------------------------- main() method --------------------------- /** * simple command line interface tabletocsv. converts 1 html file csv file, extracting tables, * entities stripped. * must have extension .html <br> use java com.mindprod.tabletocsv somefile.html . can use tabletocsv * constructor * in own programs. * * @param args name of csv file remove excess quotes , space */ public static void main( string[] args ) { if ( args.length != 1 ) { throw new illegalargumentexception( usage ); } string filename = args[ 0 ]; if ( !filename.endswith( ".html" ) ) { throw new illegalargumentexception( "bad extension. input must .html file.\n" + usage ); } final file file = new file( filename ); try { // file, separatorchar, quotechar, commentchar, encoding new tabletocsv( file, ',', '\"', '#', csv.utf8charset ); } catch ( ioexception e ) { err.println(); e.printstacktrace( err ); err.println( "csvtotable failed export" + misc.getcanorabspath( file ) ); err.println(); } }// end main }
and here bufferedreader
bufferedreader br=new bufferedreader(new filereader(newfile)); string line; while((line=br.readline())!=null) { string[]value = line.split(","); string sql = "insert main ( , ticket #, status, priority, department, account name) " + "values ('"+value[0]+"','"+value[1]+"','"+value[2]+"','"+value[3]+"','"+value[4]+"','"+value[5]+"')"; preparedstatement pst = databaseconnection.connectdb().preparestatement(sql); pst.executeupdate(); } br.close(); } catch(exception e) { joptionpane.showmessagedialog(null, e); } } } });
did test database codes? work? (hint: sql statement wrong). auto-commit on? if not, aren't suppose close() statement/connection?
i factor code , move sql statement out , creation of prepared statement out side of loop:
string sql = "insert main(\"ticket #\", \"status\", \"priority\", \"department\", \"account name\") values (?, ?, ?, ?, ?); preparedstatement pst = databaseconnection.connectdb().preparestatement(sql);
then inside while loop set the object before execute.
pst.setstring(1, value[0]); pst.setstring(2, value[1]); //...
and finally, don't forget close() statement / connection too!
pst.close(); databaseconnection.connectdb().close(); ???
Comments
Post a Comment