java poi技术读取到数据库
https://www.cnblogs.com/hongten/p/java_poi_excel.html
java的poi技术读取Excel数据到MySQL
这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中。
你也可以在 : java的poi技术读取和导入Excel 了解到写入Excel的方法信息
使用JXL技术可以在 : java的jxl技术导入Excel
项目结构:
Excel中的测试数据:
数据库结构:
对应的SQL:
复制代码
1 CREATE TABLE student_info (
2 id int(11) NOT NULL AUTO_INCREMENT,
3 no varchar(20) DEFAULT NULL,
4 name varchar(20) DEFAULT NULL,
5 age varchar(10) DEFAULT NULL,
6 score float DEFAULT '0',
7 PRIMARY KEY (id)
8 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
复制代码
插入数据成功:
如果重复数据,则丢掉:
=============================================
源码部分:
=============================================
/ExcelTest/src/com/b510/client/Client.java
复制代码
1 /**
2 *
3 */
4 package com.b510.client;
5
6 import java.io.IOException;
7 import java.sql.SQLException;
8
9 import com.b510.excel.SaveData2DB;
10
11 /**
12 * @author Hongten
13 * @created 2014-5-18
14 */
15 public class Client {
16
17 public static void main(String[] args) throws IOException, SQLException {
18 SaveData2DB saveData2DB = new SaveData2DB();
19 saveData2DB.save();
20 System.out.println("end");
21 }
22 }
复制代码
/ExcelTest/src/com/b510/common/Common.java
复制代码
1 /**
2 *
3 */
4 package com.b510.common;
5
6 /**
7 * @author Hongten
8 * @created 2014-5-18
9 /
10 public class Common {
11
12 // connect the database
13 public static final String DRIVER = "com.mysql.jdbc.Driver";
14 public static final String DB_NAME = "test";
15 public static final String USERNAME = "root";
16 public static final String PASSWORD = "root";
17 public static final String IP = "192.168.1.103";
18 public static final String PORT = "3306";
19 public static final String URL = "jdbc:mysql://" + IP + ":" + PORT + "/" + DB_NAME;
20
21 // common
22 public static final String EXCEL_PATH = "lib/student_info.xls";
23
24 // sql
25 public static final String INSERT_STUDENT_SQL = "insert into student_info(no, name, age, score) values(?, ?, ?, ?)";
26 public static final String UPDATE_STUDENT_SQL = "update student_info set no = ?, name = ?, age= ?, score = ? where id = ? ";
27 public static final String SELECT_STUDENT_ALL_SQL = "select id,no,name,age,score from student_info";
28 public static final String SELECT_STUDENT_SQL = "select from student_info where name like ";
29 }
复制代码
/ExcelTest/src/com/b510/excel/ReadExcel.java
复制代码
1 /**
2 *
3 */
4 package com.b510.excel;
5
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 import org.apache.poi.hssf.usermodel.HSSFCell;
13 import org.apache.poi.hssf.usermodel.HSSFRow;
14 import org.apache.poi.hssf.usermodel.HSSFSheet;
15 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
16
17 import com.b510.common.Common;
18 import com.b510.excel.vo.Student;
19
20 /**
21 * @author Hongten
22 * @created 2014-5-18
23 */
24 public class ReadExcel {
25
26 public List readXls() throws IOException {
27 InputStream is = new FileInputStream(Common.EXCEL_PATH);
28 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
29 Student student = null;
30 List list = new ArrayList();
31 // 循环工作表Sheet
32 for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
33 HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
34 if (hssfSheet == null) {
35 continue;
36 }
37 // 循环行Row
38 for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
39 HSSFRow hssfRow = hssfSheet.getRow(rowNum);
40 if (hssfRow != null) {
41 student = new Student();
42 HSSFCell no = hssfRow.getCell(0);
43 HSSFCell name = hssfRow.getCell(1);
44 HSSFCell age = hssfRow.getCell(2);
45 HSSFCell score = hssfRow.getCell(3);
46 student.setNo(getValue(no));
47 student.setName(getValue(name));
48 student.setAge(getValue(age));
49 student.setScore(Float.valueOf(getValue(score)));
50 list.add(student);
51 }
52 }
53 }
54 return list;
55 }
56
57 @SuppressWarnings("static-access")
58 private String getValue(HSSFCell hssfCell) {
59 if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
60 // 返回布尔类型的值
61 return String.valueOf(hssfCell.getBooleanCellValue());
62 } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
63 // 返回数值类型的值
64 return String.valueOf(hssfCell.getNumericCellValue());
65 } else {
66 // 返回字符串类型的值
67 return String.valueOf(hssfCell.getStringCellValue());
68 }
69 }
70 }
复制代码
/ExcelTest/src/com/b510/excel/SaveData2DB.java
复制代码
1 /**
2 *
3 */
4 package com.b510.excel;
5
6 import java.io.IOException;
7 import java.sql.SQLException;
8 import java.util.List;
9
10 import com.b510.common.Common;
11 import com.b510.excel.util.DbUtil;
12 import com.b510.excel.vo.Student;
13
14 /**
15 * @author Hongten
16 * @created 2014-5-18
17 */
18 public class SaveData2DB {
19
20 @SuppressWarnings({ "rawtypes" })
21 public void save() throws IOException, SQLException {
22 ReadExcel xlsMain = new ReadExcel();
23 Student student = null;
24 List list = xlsMain.readXls();
25
26 for (int i = 0; i < list.size(); i++) {
27 student = list.get(i);
28 List l = DbUtil.selectOne(Common.SELECT_STUDENT_SQL + "'%" + student.getName() + "%'", student);
29 if (!l.contains(1)) {
30 DbUtil.insert(Common.INSERT_STUDENT_SQL, student);
31 } else {
32 System.out.println("The Record was Exist : No. = " + student.getNo() + " , Name = " + student.getName() + ", Age = " + student.getAge() + ", and has been throw away!");
33 }
34 }
35 }
36 }
复制代码
/ExcelTest/src/com/b510/excel/util/DbUtil.java
复制代码
1 /**
2 *
3 */
4 package com.b510.excel.util;
5
6 import java.sql.Connection;
7 import java.sql.DriverManager;
8 import java.sql.PreparedStatement;
9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import com.b510.common.Common;
15 import com.b510.excel.vo.Student;
16
17 /**
18 * @author Hongten
19 * @created 2014-5-18
20 */
21 public class DbUtil {
22
23 /**
24 * @param sql
25 */
26 public static void insert(String sql, Student student) throws SQLException {
27 Connection conn = null;
28 PreparedStatement ps = null;
29 try {
30 Class.forName(Common.DRIVER);
31 conn = DriverManager.getConnection(Common.URL, Common.USERNAME, Common.PASSWORD);
32 ps = conn.prepareStatement(sql);
33 ps.setString(1, student.getNo());
34 ps.setString(2, student.getName());
35 ps.setString(3, student.getAge());
36 ps.setString(4, String.valueOf(student.getScore()));
37 boolean flag = ps.execute();
38 if(!flag){
39 System.out.println("Save data : No. = " + student.getNo() + " , Name = " + student.getName() + ", Age = " + student.getAge() + " succeed!");
40 }
41 } catch (Exception e) {
42 e.printStackTrace();
43 } finally {
44 if (ps != null) {
45 ps.close();
46 }
47 if (conn != null) {
48 conn.close();
49 }
50 }
51 }
52
53 @SuppressWarnings({ "unchecked", "rawtypes" })
54 public static List selectOne(String sql, Student student) throws SQLException {
55 Connection conn = null;
56 PreparedStatement ps = null;
57 ResultSet rs = null;
58 List list = new ArrayList();
59 try {
60 Class.forName(Common.DRIVER);
61 conn = DriverManager.getConnection(Common.URL, Common.USERNAME, Common.PASSWORD);
62 ps = conn.prepareStatement(sql);
63 rs = ps.executeQuery();
64 while(rs.next()){
65 if(rs.getString("no").equals(student.getNo()) || rs.getString("name").equals(student.getName())|| rs.getString("age").equals(student.getAge())){
66 list.add(1);
67 }else{
68 list.add(0);
69 }
70 }
71 } catch (Exception e) {
72 e.printStackTrace();
73 } finally {
74 if (rs != null) {
75 rs.close();
76 }
77 if (ps != null) {
78 ps.close();
79 }
80 if (conn != null) {
81 conn.close();
82 }
83 }
84 return list;
85 }
86
87
88 public static ResultSet selectAll(String sql) throws SQLException {
89 Connection conn = null;
90 PreparedStatement ps = null;
91 ResultSet rs = null;
92 try {
93 Class.forName(Common.DRIVER);
94 conn = DriverManager.getConnection(Common.URL, Common.USERNAME, Common.PASSWORD);
95 ps = conn.prepareStatement(sql);
96 rs = ps.executeQuery();
97 } catch (Exception e) {
98 e.printStackTrace();
99 } finally {
100 if (rs != null) {
101 rs.close();
102 }
103 if (ps != null) {
104 ps.close();
105 }
106 if (conn != null) {
107 conn.close();
108 }
109 }
110 return rs;
111 }
112
113 }
复制代码
/ExcelTest/src/com/b510/excel/vo/Student.java
复制代码
1 /**
2 *
3 */
4 package com.b510.excel.vo;
5
6 /**
7 * Student
8 *
9 * @author Hongten
10 * @created 2014-5-18
11 */
12 public class Student {
13 /**
14 * id
15 */
16 private Integer id;
17 /**
18 * 学号
19 */
20 private String no;
21 /**
22 * 姓名
23 */
24 private String name;
25 /**
26 * 学院
27 */
28 private String age;
29 /**
30 * 成绩
31 */
32 private float score;
33
34 public Integer getId() {
35 return id;
36 }
37
38 public void setId(Integer id) {
39 this.id = id;
40 }
41
42 public String getNo() {
43 return no;
44 }
45
46 public void setNo(String no) {
47 this.no = no;
48 }
49
50 public String getName() {
51 return name;
52 }
53
54 public void setName(String name) {
55 this.name = name;
56 }
57
58 public String getAge() {
59 return age;
60 }
61
62 public void setAge(String age) {
63 this.age = age;
64 }
65
66 public float getScore() {
67 return score;
68 }
69
70 public void setScore(float score) {
71 this.score = score;
72 }
73
74 }
复制代码
源码下载:http://files.cnblogs.com/hongten/ExcelTest.zip
========================================================
多读一些书,英语很重要。
More reading,and english is important.
I'm Hongten
java poi技术读取到数据库的更多相关文章
- java的poi技术读取Excel数据到MySQL
这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...
- java的poi技术读取和导入Excel实例
本篇文章主要介绍了java的poi技术读取和导入Excel实例,报表输出是Java应用开发中经常涉及的内容,有需要的可以了解一下. 报表输出是Java应用开发中经常涉及的内容,而一般的报表往往缺乏通用 ...
- java的poi技术读取Excel[2003-2007,2010]
这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...
- java的poi技术读取Excel数据
这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...
- java的poi技术读取Excel[2003-2007,2010]
这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...
- java通过Access_JDBC30读取access数据库时无法获取最新插入的记录
1.编写了一个循环程序,每几秒钟读取一次,数据库中最新一行数据 连接access数据库的方法和查询的信息.之后开一个定时去掉用. package javacommon.util; import jav ...
- java的poi技术读取和导入Excel
项目结构: http://www.cnblogs.com/hongten/gallery/image/111987.html 用到的Excel文件: http://www.cnblogs.com/h ...
- java POI导出Excel文件数据库的数据
在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.这里我简单实现导出Excel文件. POI jar ...
- java POI技术之导出数据优化(15万条数据1分多钟)
专针对导出excel2007 ,用到poi3.9的jar package com.cares.ynt.util; import java.io.File; import java.io.FileOut ...
随机推荐
- ABI是什么? Swift ABI稳定有什么好处?
ABI是什么? 在软件开发中, 应用程序机器二元码界面(Application Binary Interface 简称ABI)指两个程序模块间的接口; 通常其中一个车还给你徐模块会是库或者操作系统提供 ...
- lock free
#include <thread> #include <iostream> #include <mutex> #include <atomic> #in ...
- memcache和redis的区别和联系
一.区别 Memcache : 1,对每个key的数据最大是1M. 2,对各种技术支持比较全面,session可以存储memcache中,各种框架(例如thinkphp)对memcache支持比较好. ...
- Git 原理入门
Git 是最流行的版本管理工具,也是程序员的必备技能之一. 即使天天使用它,很多人也未必了解它的原理.Git 为什么可以管理版本?git add.git commit这些基本命令,到底在做什么,你说得 ...
- rem和em的用法
1.rem转化为向素值的方法 rem单位转化为像素大小取决于根元素的字体大小,即HTML元素的字体大小,根元素字体大小乘以rem. 例:根元素的字体大小 16px,10rem 将等同于 160px,即 ...
- 最新版的stm32f1xx.h文件中取消了u8, u16, u32的类型定义
使用芯片stm32f103zet6和stm32l151c8t6,在移植程序时发现,编译器提示u8未定义: 在Keil MDK 开发环境里,st定义无符号32位整形数据有很多种表示方法: 1 unsig ...
- vowels_双元音
vowels(美式): 双元音:前长后短.前强后弱,流畅滑动. [e]:两个字母“e”和“I”的结合,单词cake.rain.blame.lack.make.later. [aɪ]:两个字母“a”和“ ...
- linux chrom 系统无法读取用户偏好配置无需删除.config配置文件
鬼使神差的使用了root权限启用了一下浏览器,再次打开就出现了这样的状况. 百度搜索了一下解决方案 几乎都是同一篇 需要删除/.config/google-chrome文件 才能正常启动. 那么如 ...
- Artistic Style 3.1
Artistic Style 3.1 Tab 选项 下面的示例显示空白字符.一个空格(space)用一个 . 表示,一个制表符(tab)用 > (大于号) 表示. ** 默认缩进 ** 如果没有 ...
- Java8 Lambda表达式实战之方法引用(一)
方法的引用 方法引用是用来直接访问类或者实例的已经存在的方法或者构造方法,方法引用提供了一种引用而不执行方法的方式,如果抽象方法的实现恰好可以使用调用另外一个方法来实现,就有可能可以使用方法引用 方法 ...