jsp上传excel到mysql_通过java把excel内容上传到mysql

mysql 表列名 num1,num2,num3,num4,num5,num6  表名Excle

上传的方法

package com.web.connection;

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;

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 TestExcel {

//记录类的输出信息­

static Log log = LogFactory.getLog(TestExcel.class);

//获取Excel文档的路径­

//.xlsx文件用XSSFWorkbook .xlx 用HSSFWorkbook

public static String filePath = "D://demoExcel.xlsx";

public static void main(String[] args) {

try {

// 创建对Excel工作簿文件的引用­

XSSFWorkbook wookbook = new XSSFWorkbook(new FileInputStream(filePath));

// 在Excel文档中,第一张工作表的缺省索引是0

// 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);­

XSSFSheet sheet = wookbook.getSheet("Sheet1");

//获取到Excel文件中的所有行数­

int rows = sheet.getPhysicalNumberOfRows();

//遍历行

for (int i = 0; i < rows; i++) {

// 读取左上端单元格

XSSFRow row = sheet.getRow(i);

// 行不为空­

if (row != null) {

//获取到Excel文件中的所有的列­

int cells = row.getPhysicalNumberOfCells();

String value = "";

//遍历列­

for (int j = 0; j < cells; j++) {

//获取到列的值­

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

int a=method.add(entity);

if(a>0){

System.out.println("插入成功");

}else{

System.out.println("插入失败");

}

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

其中 TestEntity  为用存放从excel表中查询到的数据的实体类

package com.web.connection;

public class TestEntity {

private String num1;

private String num2;

private String num3;

private String num4;

private String num5;

private String num6;

public TestEntity(){}

public String getNum1() {

return num1;

}

public void setNum1(String num1) {

this.num1 = num1;

}

public String getNum2() {

return num2;

}

public void setNum2(String num2) {

this.num2 = num2;

}

public String getNum3() {

return num3;

}

public void setNum3(String num3) {

this.num3 = num3;

}

public String getNum4() {

return num4;

}

public void setNum4(String num4) {

this.num4 = num4;

}

public String getNum5() {

return num5;

}

public void setNum5(String num5) {

this.num5 = num5;

}

public String getNum6() {

return num6;

}

public void setNum6(String num6) {

this.num6 = num6;

}

}

TestMethod  为往mysql表中插入数据  的sql语句

package com.web.connection;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class TestMethod {

public int add(TestEntity te){

Connection con = DBconnection.getConnection();

PreparedStatement pstmt = null;

int count = 0;

String sql = " insert into Excle(num1,num2,num3,num4,num5,num6) values(?,?,?,?,?,?)";

try {

pstmt = con.prepareStatement(sql);

pstmt.setString(1, te.getNum1());

pstmt.setString(2, te.getNum2());

pstmt.setString(3, te.getNum3());

pstmt.setString(4, te.getNum4());

pstmt.setString(5, te.getNum5());

pstmt.setString(6, te.getNum6());

count = pstmt.executeUpdate();

/*

* if(count==0){ throw new DataAlreadyExistException(); }

*/

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

pstmt.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

DBconnection.closeConnection();

}

return count;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Excel导入MySQL的一种方法是使用JSP和JDBC(Java Database Connectivity)。您可以按照以下步骤操作: 1. 创建一个JSP页面,并在其中添加一个表单,以便用户可以选择要上传Excel文件。 2. 在JSP页面中添加Java代码来读取Excel文件。您可以使用Apache POI库来读取Excel文件。该库提供了一组API,可用于读取和写入Microsoft Office格式的文档。 3. 将Excel数据存储到Java对象中,并使用JDBC将数据插入MySQL数据库中。您可以使用Java中提供的JDBC API来连接和操作MySQL数据库。 以下是示例代码: ``` <%@ page import="java.sql.*" %> <%@ page import="org.apache.poi.ss.usermodel.*" %> <%@ page import="org.apache.poi.xssf.usermodel.XSSFWorkbook" %> <% String fileName = request.getParameter("fileName"); String jdbcURL = "jdbc:mysql://localhost:3306/mydb"; String username = "root"; String password = "password"; try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(jdbcURL, username, password); Statement stmt = con.createStatement(); FileInputStream file = new FileInputStream(fileName); Workbook workbook = new XSSFWorkbook(file); Sheet sheet = workbook.getSheetAt(0); for (int i=1; i<=sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); String name = row.getCell(0).getStringCellValue(); int age = (int) row.getCell(1).getNumericCellValue(); String email = row.getCell(2).getStringCellValue(); String query = "INSERT INTO users(name, age, email) VALUES('" + name + "', " + age + ", '" + email + "')"; stmt.executeUpdate(query); } stmt.close(); con.close(); file.close(); out.println("Data uploaded successfully!"); } catch (Exception e) { out.println("Error: " + e.getMessage()); } %> ``` 在此示例中,假设您已经安装并配置了Apache POI和MySQL JDBC驱动程序。此代码将从上传Excel文件中读取数据,并将其插入名为“users”的MySQL表中。您需要根据自己的数据库和表结构进行更改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值