我想通过java程序将excel表数据插入数据库我没有得到实际的输出。 excel表不包含他们在行中给出的标题 我需要获取每一行并将其作为插入脚本。
数据应直接插入数据库
client page AcessDate ProcessTime bytes
12.34.56.9876545 hello.jsp 6/3/2014 5:52:16 PM 3 123
12.34.56.9876545 SignOut.jsp 6/3/2014 6:21:16 PM 0 1724
12.34.56.9876545 Login.jsp 6/3/2014 6:21:16 PM 0 4937
我认为输出为
insert into ClickStream(Client,Page,AccessDate,ProcessTime,Bytes)
value('"+12.34.56.9876545 +"','"+hello.jsp+"','"+ 6/3/2014 5:52:16 PM+"','"+3+"','"+1724+"'"
我正在使用我没有得到的代码,看看我的代码是否正常工作我无法获得正确的输出
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Vector;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class InsertExcelFileData {
public static void main( String [] args ) {
String fileName="F:\\book.xlsx";
Vector dataHolder=read(fileName);
saveToDatabase(dataHolder);
}
public static Vector read(String fileName) {
Vector cellVectorHolder = new Vector();
try{
FileInputStream myInput = new FileInputStream(fileName);
//POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
XSSFWorkbook myWorkBook = new XSSFWorkbook(myInput);
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator rowIter = mySheet.rowIterator();
while(rowIter.hasNext()){
XSSFRow myRow = (XSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
Vector cellStoreVector=new Vector();
while(cellIter.hasNext()){
XSSFCell myCell = (XSSFCell) cellIter.next();
cellStoreVector.addElement(myCell);
}
cellVectorHolder.addElement(cellStoreVector);
}
}catch (Exception e){e.printStackTrace(); }
return cellVectorHolder;
}
private static void saveToDatabase(Vector dataHolder) {
String Client="";
String Page="";
String AccessDate="";
String ProcessTime="";
String Bytes="";
for (int i=0;i<dataHolder.size(); i++){
Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
for (int j=0; j < cellStoreVector.size();j++){
XSSFCell myCell = (XSSFCell)cellStoreVector.elementAt(j);
String st = myCell.toString();
Client=st;
Page=st.substring(0);
System.out.print(st);
}
try{
Class.forName("com.jnetdirect.jsql.JSQLDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:JSQLConnect://12.33.44.55/database=Environment", "root", "root");
Statement stat=con.createStatement();
int k=stat.executeUpdate("insert into ClickStream(Client,Page,AccessDate,ProcessTime,Bytes) value('"+Client+"','"+Page+"','"+AccessDate+"','"+ProcessTime+"','"+Bytes+"'");
System.err.print(k);
System.out.println("Data is inserted");
stat.close();
con.close();
}
catch(Exception e){}
}
}
}
请帮助让代码正常运行
答案 0 :(得分:2)
实际上你的迭代几乎没有问题。我希望这段代码可能对你有所帮助
import java.io.FileInputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelToDb {
public static void main( String [] args ) {
String fileName="G:\\book.xlsx";
Vector dataHolder=read(fileName);
saveToDatabase(dataHolder);
}
public static Vector read(String fileName) {
Vector cellVectorHolder = new Vector();
try{
FileInputStream myInput = new FileInputStream(fileName);
//POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
XSSFWorkbook myWorkBook = new XSSFWorkbook(myInput);
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator rowIter = mySheet.rowIterator();
while(rowIter.hasNext()){
XSSFRow myRow = (XSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
//Vector cellStoreVector=new Vector();
List list = new ArrayList();
while(cellIter.hasNext()){
XSSFCell myCell = (XSSFCell) cellIter.next();
list.add(myCell);
}
cellVectorHolder.addElement(list);
}
}catch (Exception e){e.printStackTrace(); }
return cellVectorHolder;
}
private static void saveToDatabase(Vector dataHolder) {
String ClientAdd="";
String Page="";
String AccessDate="";
String ProcessTime="";
String Bytes="";
System.out.println(dataHolder);
for(Iterator iterator = dataHolder.iterator();iterator.hasNext();) {
List list = (List) iterator.next();
ClientAdd = list.get(0).toString();
Page = list.get(1).toString();
AccessDate = list.get(2).toString();
ProcessTime = list.get(3).toString();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "welcome");
System.out.println("connection made...");
PreparedStatement stmt=con.prepareStatement("INSERT INTO ClickStream(ClientAdd,Page,AccessDate,ProcessTime) VALUES(?,?,?,?)");
stmt.setString(1, ClientAdd);
stmt.setString(2, Page);
stmt.setString(3, AccessDate);
stmt.setString(4, ProcessTime);
stmt.executeUpdate();
System.out.println("Data is inserted");
stmt.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
答案 1 :(得分:-1)
为什么不将excel文件导出到csv文件中,然后用它将其加载到db?