您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 平顶山分类信息网,免费分类信息发布

java的poi技术读取Excel数据到MySQL

2026/7/2 7:30:14发布6次查看
这篇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 hongten13 * @created 2014-5-1814*/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 database13 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 // common22 public static final string excel_path = lib/student_info.xls;23 24 // sql25 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 hongten22 * @created 2014-5-1823*/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 // 循环工作表sheet32 for (int numsheet = 0; numsheet /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 hongten16 * @created 2014-5-1817*/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 /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 hongten10 * @created 2014-5-1811*/12 public class student {13 /**14 * id15*/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
========================================================
平顶山分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product