继续这个问题。
我需要帮助,使我的TableToCSV(函数转换。html表为csv),呈现代码到数据库,而不是。csv。我创建了一个BufferedReader,它将。csv转换为数据库,但我无法获得2连接。请将TableToCSV的输出文件放到我的bufferedreader中。
TableToCSV
* [TableToCSV.java]
*
* Summary: Extracts rows in CSV tables to CSV form. Extracts data from all tables in the input. Output in xxx.csv.
*
* Copyright: (c) 2011-2014 Roedy Green, Canadian Mind Products, http://mindprod.com
*
* Licence: This software may be copied and used freely for any purpose but 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 you to 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 to CSV form. Extracts data from all tables in the input. Output in xxx.csv.
* <p/>
* Use: java.exe com.mindprod.TableToCSV xxxx.html
* It also strips tags and converts entities back to UTF-8 characters.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.1 2011-01-25 allow you to specify encoding
* @since 2011-01-23
*/
public final class TableToCSV
{
// ------------------------------ CONSTANTS ------------------------------
/**
* how to use the command line
*/
private static final String USAGE = "TableToCSV needs the name of an HTML file on the commandline, " +
"nothing else. Output will be in xxx.csv.";
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* Constructor to convert an HTML table to CSV. Strips out entities and tags.
*
* @param file CSV file to be packed to remove excess space and quotes.
* @param separatorChar field separator character, usually ',' in North America,
* ';' in Europe and sometimes 't' for
* tab for the output file. It is tab for the input file.
* Note this is a 'char' not a "string".
* @param quoteChar character used to quote fields containing awkward chars.
* @param commentChar character to treat as comments.
* @param encoding encoding of the input and 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 the entire html file into RAM.
String big = HunkIO.readEntireFile( file, encoding );
int from = 0;
// our parser is forgiving, works even if </td> </tr> missing.
while ( true )
{
// find <tr
final int trStart = big.indexOf( "<tr", from );
if ( trStart < 0 )
{
break;
}
from = trStart + 3;
final int trEnd = big.indexOf( '>', from );
if ( trEnd < 0 )
{
break;
}
while ( true )
{
// search for <td>...</td>
final int tdStart = big.indexOf( "<td", from );
if ( tdStart < 0 )
{
break;
}
from = tdStart + 3;
final int tdEnd = big.indexOf( '>', from );
if ( tdEnd < 0 )
{
break;
}
from = tdEnd + 1;
final int startField = tdEnd + 1;
final int slashTdStart = big.indexOf( "</td", from );
final int lookaheadTd = big.indexOf( "<td", from );
final int lookaheadSlashTr = big.indexOf( "</tr", from );
final int lookaheadTr = big.indexOf( "<tr", from );
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;
}
from = endField + 3;
final int slashTdEnd = big.indexOf( '>', from );
if ( slashTdEnd < 0 )
{
break;
}
String field = big.substring( startField, endField );
field = DeEntifyStrings.flattenHTML( field, ' ' );
w.put( field );
from = slashTdEnd + 1;
final int lookTd = big.indexOf( "<td", from );
final int lookTr = big.indexOf( "<tr", from );
if ( lookTr >= 0 && lookTr < lookTd || lookTd < 0 )
{
break;
}
}
w.nl();
}
out.println( w.getLineCount() + " rows extracted from table to csv" );
w.close();
}
// --------------------------- main() method ---------------------------
/**
* Simple command line interface to TableToCSV. Converts one HTML file to a CSV file, extracting tables,
* with entities stripped.
* Must have extension .html <br> Use java com.mindprod.TableToCSV somefile.html . You can use TableToCSV
* constructor
* in your own programs.
*
* @param args name of csv file to remove excess quotes and 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 be a .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 to export" + Misc.getCanOrAbsPath( file ) );
err.println();
}
}// end main
}
这里是BufferedReader
BufferedReader br=new BufferedReader(new FileReader(newFile));
String line;
while((line=br.readLine())!=null)
{
String[]value = line.split(",");
String sql = "INSERT into 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);
}
}
}
});
您是否测试了您的数据库代码?它有用吗?(提示:你的sql语句是错误的)。是自动提交吗?如果不是,你不应该关闭()语句/连接吗?
我将对代码进行分解,将sql语句移出,并在循环之外创建预处理语句:
String sql = "INSERT INTO MAIN("Ticket #", "Status", "Priority", "Department", "Account Name") VALUES (?, ?, ?, ?, ?);
PreparedStatement pst = DatabaseConnection.ConnectDB().prepareStatement(sql);
然后在while循环中,在执行之前设置对象。
pst.setString(1, value[0]);
pst.setString(2, value[1]); //...
最后,不要忘记关闭()语句/连接!
pst.close();
DatabaseConnection.ConnectDB().close(); ???