当前位置:首页 » 数据仓库 » 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();-
}-
}-
}-