Java 将 Excel 数据插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24278477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert Excel Data to database
提问by user3751150
i suppose to insert the excel sheet data into database by a java program i am not getting the actual output. the excel sheet does not contain headings they given in the row i need to get each row and make it as insert script.
我想通过java程序将excel表数据插入数据库我没有得到实际输出。Excel 工作表不包含他们在行中给出的标题,我需要获取每一行并将其作为插入脚本。
the data should be insert directly in database
数据应该直接插入数据库中
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
i shoud get the output as
我应该得到输出
insert into ClickStream(Client,Page,AccessDate,ProcessTime,Bytes)
value('"+12.34.56.9876545 +"','"+hello.jsp+"','"+ 6/3/2014 5:52:16 PM+"','"+3+"','"+1724+"'"
i am useing the code i am not getting and see if my code working or not i cant able to get the right output
我正在使用我没有得到的代码,看看我的代码是否有效,我无法获得正确的输出
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){}
}
}
}
please help to get the code work properly
请帮助使代码正常工作
采纳答案by SparkOn
Actually there is little problem with your iteration. I hope this code may help you somewhat
实际上,您的迭代没有什么问题。我希望这段代码可以对您有所帮助
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();
}
}
}
}
回答by Juan Antonio Gomez Moriano
Why don't you export the excel file into a csv file and then use that to load it into the db?
为什么不将 excel 文件导出为 csv 文件,然后使用它将其加载到数据库中?