gpt4 book ai didi

java - 将 Excel 数据插入数据库

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:59 25 4
gpt4 key购买 nike

我想通过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){}
}
}
}

请帮助使代码正常工作

最佳答案

其实你的迭代没有什么问题。我希望这段代码可以对您有所帮助

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();
}
}



}
}

关于java - 将 Excel 数据插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24278477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com