當前位置:首頁 » 數據倉庫 » jdbc導入excel資料庫
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

jdbc導入excel資料庫

發布時間: 2022-12-09 07:41:48

㈠ (高手進)JDBC 批量導入excel數據

去看poi的api吧 在servlet里用poi讀取excel數據,轉成json返回頁面處理顯示

㈡ 怎麼把JDBC中數據導出Excel文件

JDBC不會有任何數據,JDBC 只是Java 操作數據的規范而已,本身用於操作資料庫,它不具備格式轉換之類的功能。

關於Office 的操作可以看看 Apache POI 組件。它可以支持Excel 到導入導出,不限定平台。

㈢ 怎麼把JDBC中數據導出Excel文件

jdbc先導出為txt,然後再導入到excel文件是常規討論,你直接操作excel也行,但是小題大作。

㈣ 如何在Java中導入Excel表數據

1,加入依賴的罐子文件:
引用:
*mysql的jar文件
*Spring_HOME/lib/poi/*.jar

2,編寫資料庫鏈接類
package com.zzg.db;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbUtils {
private static Connection conn;

static {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","123456");
} catch (Exception e) {
e.printStackTrace();
}
}

public static Connection getConn() {
return conn;
}

public static void setConn(Connection conn) {
DbUtils.conn = conn;
}
}

3,編寫資料庫操作類

package com.zzg.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ExcuteData {
private PreparedStatement pstmt;
public boolean ExcuData(String sql) {
Connection conn = DbUtils.getConn();
boolean flag=false;
try {
pstmt = conn.prepareStatement(sql);
flag=pstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
}

4,編寫的Excel表格實體類

package com.zzg.model;
public class TableCell {
private String _name;
private String _value;
public String get_name() {
return _name;
}
public void set_name(String _name) {
this._name = _name;
}
public String get_value() {
return _value;
}
public void set_value(String _value) {
this._value = _value;
}
}

5,編寫主鍵生成方法

package com.zzg.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class GenericUtil {
public static String getPrimaryKey()
{
String primaryKey;
primaryKey = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
Random r = new Random();
primaryKey +=r.nextInt(100000)+100000;
return primaryKey;
}
}

6,編寫的Excel操作類

package com.zzg.deployData;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.zzg.db.ExcuteData;
import com.zzg.model.TableCell;
import com.zzg.util.GenericUtil;
public class OperExcel<T extends Serializable> {
private HSSFWorkbook workbook;
private String tableName;
private Class<T> type;
private String sheetName;

public OperExcel(File excelFile, String tableName, Class<T> type,
String sheetName) throws FileNotFoundException,
IOException {
workbook = new HSSFWorkbook(new FileInputStream(excelFile));
this.tableName = tableName;
this.type = type;
this.sheetName = sheetName;
InsertData();
}

// 向表中寫入數據
public void InsertData() {
System.out.println("yyy");
ExcuteData excuteData = new ExcuteData();
List<List> datas = getDatasInSheet(this.sheetName);
// 向表中添加數據之前先刪除表中數據
String strSql = "delete from " + this.tableName;
excuteData.ExcuData(strSql);
// 拼接sql語句
for (int i = 1; i < datas.size(); i++) {
strSql = "insert into " + this.tableName + "(";
List row = datas.get(i);
for (short n = 0; n < row.size(); n++) {
TableCell excel = (TableCell) row.get(n);
if (n != row.size() - 1)
strSql += excel.get_name() + ",";
else
strSql += excel.get_name() + ")";
}
strSql += " values (";
for (short n = 0; n < row.size(); n++) {
TableCell excel = (TableCell) row.get(n);
try {
if (n != row.size() - 1) {
strSql += getTypeChangeValue(excel) + ",";
} else
strSql += getTypeChangeValue(excel) + ")";
} catch (RuntimeException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//執行sql
excuteData.ExcuData(strSql);
}
}

/**
* 獲得表中的數據
* @param sheetName 表格索引(EXCEL 是多表文檔,所以需要輸入表索引號)
* @return 由LIST構成的行和表
*/
public List<List> getDatasInSheet(String sheetName) {
List<List> result = new ArrayList<List>();
// 獲得指定的表
HSSFSheet sheet = workbook.getSheet(sheetName);
// 獲得數據總行數
int rowCount = sheet.getLastRowNum();
if (rowCount < 1) {
return result;
}
// 逐行讀取數據
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
// 獲得行對象
HSSFRow row = sheet.getRow(rowIndex);
if (row != null) {
List<TableCell> rowData = new ArrayList<TableCell>();
// 獲得本行中單元格的個數
int columnCount = sheet.getRow(0).getLastCellNum();
// 獲得本行中各單元格中的數據
for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) {
HSSFCell cell = row.getCell(columnIndex);
// 獲得指定單元格中數據
Object cellStr = this.getCellString(cell);
TableCell TableCell = new TableCell();
TableCell.set_name(getCellString(
sheet.getRow(0).getCell(columnIndex)).toString());
TableCell.set_value(cellStr == null ? "" : cellStr
.toString());
rowData.add(TableCell);
}
result.add(rowData);
}
}
return result;
}

/**
* 獲得單元格中的內容
* @param cell
* @return result
*/
protected Object getCellString(HSSFCell cell) {
Object result = null;
if (cell != null) {
int cellType = cell.getCellType();
switch (cellType) {

case HSSFCell.CELL_TYPE_STRING:
result = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR:
result = null;
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = null;
break;
}
}
return result;
}

// 根據類型返回相應的值
@SuppressWarnings("unchecked")
public String getTypeChangeValue(TableCell excelElement)
throws RuntimeException, Exception {
String colName = excelElement.get_name();
String colValue = excelElement.get_value();
String retValue = "";
if (colName.equals("id")) {
retValue = "'" + GenericUtil.getPrimaryKey() + "'";
return retValue;
}
if (colName == null) {
retValue = null;
}
if (colName.equals("class_createuser")) {
retValue = "yaa101";
return "'" + retValue + "'";
}
retValue = "'" + colValue + "'";
return retValue;
}
}

7,編寫調用操作的Excel類的方法

package com.zzg.deployData;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class DeployData {
private File fileOut;
public void excute(String filepath) {
fileOut = new File(filepath);
this.deployUserInfoData();
}

public void deployUserInfoData() {
try {
new OperExcel(fileOut, "test", Object.class, "Sheet1");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

8,編寫客戶端
package com.zzg.client;
import com.zzg.deployData.DeployData;
public class DeployClient {
public static void main(String[] args) {
DeployData deployData = new DeployData();
deployData.excute("D://test.xls");
}
}

㈤ java 怎麼從excel文件導入到oracle資料庫中

java操作excel一般都使用poi來完成:

1、下載poi相關jar,maven的集成如下:(把${poi.version}替換成你要的版本)

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>

2、根據poi相關api讀取sheet、row、cell,獲得excel的數據:

封裝row的對象,即每一行數據為一個對象,每個cell為對象里的一個屬性,

整個sheet的數據裝進集合里;

3、處理數據,可以對數據進行驗證或其他操作;

4、寫資料庫操作。

㈥ 怎樣通過JSP頁面上傳個Excel文件,並實現Java讀取EXCEL存入資料庫

1。通過jsp頁面接收上傳的excel文件到後台,
2,後台使用java連接資料庫
3.將excel文件通過io文件控制流讀入,然後使用jdbc寫入到資料庫

㈦ 如何用JAVA將資料庫中的數據導入到excel表格

importjava.io.*;
importjava.text.SimpleDateFormat;
importjava.util.*;
importjava.sql.*;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Map;
importjxl.*;
publicclassSimUpdate{
privateStringfileName;
publicZfzSimUpdate(StringfileName){
this.fileName=fileName;
}
staticMaptNames;
static{
tNames=newHashMap();
}
/**
*用於產生資料庫的ID值,組成[年月日時分秒(100-999)]總共17位數.
*根據不同的表名,可保證同一秒內產生的ID號不重復
*/
privatestaticStringgetDtime(){
Stringrid;
Datend=newDate();
SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMddHHmmss");
rid=sdf.format(nd);
returnrid;
}

publicStringgetSeqNumber(StringtableName){
if(tableName==null||"".equals(tableName))
tableName="GENERY";
Integerit;
//
synchronized(tNames){
it=(Integer)tNames.get(tableName);
if(it==null){
it=newInteger(100);
tNames.put(tableName,it);
}else{
if(it.intValue()>998)
it=newInteger(100);
else
it=newInteger(1+it.intValue());
tNames.put(tableName,it);
}
}
returngetDtime()+String.valueOf(it);
}

privatevoipdateDb(){
try{
Connectionconn=DbPool.connectDB();
if(conn!=null){
Statementstmt=conn.createStatement();
/**********************************************/
jxl.Workbookrwb=null;
try{
//構建Workbook對象只讀Workbook對象
//直接從本地文件創建Workbook
//從輸入流創建Workbook
InputStreamis=newFileInputStream(fileName);
rwb=Workbook.getWorkbook(is);
//Sheet(術語:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
//Sheet的下標是從0開始的
//獲取第一張Sheet表
Sheetrs=rwb.getSheet(0);
//獲取Sheet表中所包含的總列數
intrsColumns=rs.getColumns();
//獲取Sheet表中所包含的總行數
intrsRows=rs.getRows();
//獲取指這下單元格的對象引用

StringsimNumber="",termSeqId="";
//指定SIM卡號及序列號
for(inti=0;i<rsRows;i++){
for(intj=0;j<rsColumns;j++){
Cellcell=rs.getCell(j,i);
if(j==0){
simNumber=cell.getContents();
}
termSeqId="633"+simNumber;
}
Stringsql="查詢SQL";
intisOk=stmt.executeUpdate(sql);
if(isOk==0&&!simNumber.equals("")){
StringtermId=getSeqNumber("termInf");
StringinsertSql="自定義INSERT";
intisAdd=stmt.executeUpdate(insertSql);
if(isAdd>0){
System.out.println("成功插入第"+i+"條數據");
}

}
//System.out.println("SIM卡號:"+simNumber+",序列號:"+termSeqId);
}

//以下代碼為寫入新的EXCEL,這里不使用,所以注釋
/*

//利用已經創建的Excel工作薄創建新的可寫入的Excel工作薄
jxl.write.WritableWorkbookwwb=Workbook.createWorkbook(newFile("D://Book2.xls"),rwb);
//讀取第一張工作表
jxl.write.WritableSheetws=wwb.getSheet(0);

//獲取第一個單元格對象
jxl.write.WritableCellwc=ws.getWritableCell(0,0);
//決斷單元格的類型,做出相應的轉化
if(wc.getType()==CellType.LABEL){
Labell=(Label)wc;
l.setString("Thevaluehasbeenmodified.");
}
//寫入Excel對象
wwb.write();
wwb.close();
*/
}catch(Exceptione){
e.printStackTrace();
}
finally{
//操作完成時,關閉對象,翻譯佔用的內存空間
rwb.close();

}
/*********************************************/

}
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(Stringargs[]){
DbPooldbPool=newDbPool("dbConn.cfg");//連接資料庫
SimUpdatesimUpdate=newSimUpdate("zfz_sim.xls");
simUpdate.updateDb();

}

}

我只用了讀取XLS,寫入沒試,應該沒問題吧,你把注釋了的拿來試一下吧

㈧ 我是用的JDBC連接sql server資料庫,現在要在頁面中實現數據的批量導入,我想用excel文件來進行批量導入,

網上一搜一大堆

㈨ 網站用戶提交的文件怎麼直接導入資料庫

導入數據方法如下:
首先對需求進行分析,按照傳統模式,可以採用POI+JDBC的方式來進行導入數據。但是這種方式比較繁瑣,同時插入效率在數據量很大時,相對來說還是不夠高。還有一種更方便快速的方式實現該功能,就是利用Clickhouse自有的插入數據功能,類似於Mysql的load data語法實現的快速導入大批量數據的功能。
資料庫(DataBase,DB)是按照數據結構來組織、存儲和管理數據的倉庫。隨著信息技術和市場的發展,特別是20世紀90年代以後,數據管理不再僅僅是存儲和管理數據,而轉變成用戶所需要的各種數據管理的方式。資料庫有很多種類型,從最簡單的存儲有各種數據的表格到能夠進行海量數據存儲的大型資料庫系統,在各個方面都得到了廣泛的應用。
資料庫是一個長期存儲在計算機內的、有組織的、有共享的、統一管理的數據集合。它是一個按數據結構來存儲和管理數據的計算機軟體系統。也就是說,資料庫包含有兩種含義:保管數據的「倉庫」,以及管理數據的方法和技術。

㈩ java如何把excel內容導入到mysql資料庫,資料庫的列名就是excel的列名

1、添加POI jar包到項目的lib目錄下-
2、Excel文件目錄:d://excel.xls-
3、資料庫欄位為:num1 num2 num3 num4 num5 num6-
4、資料庫名:blog-
5、表名:test-
6、編寫類:連接mysql的字元串方法、插入的方法、實體類--
import java.io.FileInputStream;-
import java.io.FileNotFoundException;-
import java.io.IOException;-
import org.apache.commons.logging.Log;-
import org.apache.commons.logging.LogFactory;-
import org.apache.poi.hssf.usermodel.HSSFCell;-
import org.apache.poi.hssf.usermodel.HSSFRow;-
import org.apache.poi.hssf.usermodel.HSSFSheet;-
import org.apache.poi.hssf.usermodel.HSSFWorkbook;-
public class TestExcel {-
//記錄類的輸出信息-
static Log log = LogFactory.getLog(TestExcel.class); -
//獲取Excel文檔的路徑-
public static String filePath = "D://excel.xls";-
public static void main(String[] args) {-
try {-
// 創建對Excel工作簿文件的引用-
HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(filePath));-
// 在Excel文檔中,第一張工作表的預設索引是0,-
// 其語句為:HSSFSheet sheet = workbook.getSheetAt(0);-
HSSFSheet sheet = wookbook.getSheet("Sheet1");-
//獲取到Excel文件中的所有行數-
int rows = sheet.getPhysicalNumberOfRows();-
//遍歷行-
for (int i = 0; i < rows; i++) {-
// 讀取左上端單元格-
HSSFRow row = sheet.getRow(i);-
// 行不為空-
if (row != null) {-
//獲取到Excel文件中的所有的列-
int cells = row.getPhysicalNumberOfCells();-
String value = ""; -
//遍歷列-
for (int j = 0; j < cells; j++) {-
//獲取到列的值-
HSSFCell cell = row.getCell(j);-
if (cell != null) {-
switch (cell.getCellType()) {-
case HSSFCell.CELL_TYPE_FORMULA:-
break;-
case HSSFCell.CELL_TYPE_NUMERIC:-
value += cell.getNumericCellValue() + ","; -
break; -
case HSSFCell.CELL_TYPE_STRING:-
value += cell.getStringCellValue() + ",";-
break;-
default:-
value += "0";-
break;-
}-
} -
}-
// 將數據插入到mysql資料庫中-
String[] val = value.split(",");-
TestEntity entity = new TestEntity();-
entity.setNum1(val[0]);-
entity.setNum2(val[1]);-
entity.setNum3(val[2]);-
entity.setNum4(val[3]);-
entity.setNum5(val[4]);-
entity.setNum6(val[5]);-
TestMethod method = new TestMethod();-
method.Add(entity);-
}-
}-
} catch (FileNotFoundException e) {-
e.printStackTrace();-
} catch (IOException e) {-
e.printStackTrace();-
}-
}-
}-