由于作业需要,要求我们将word文档中的数据直接导入到mysql中,在网上找了很常时间,终于将其解决。
由于比较初级,所以处理的word文档是那种比较规范的那种,条例比较清晰,设计的思路也比较简单,就是根据段落的内容判断每一段是标题还是内容,然后读完一条记录后直接像数据库中添加,知道最后都添加完成。代码如下:
本项目的结构如截图所示:
其中Bean.java里边是一个实体类,我将每一个新闻热词的标题,内容,分类都封装在一个类中,Dao.java是实现向数据库中插入数据的函数,DBUtil.java是链接数据库,WordToSql.java里边是具体的实现代码,test.java是一个测试类。
注:本人用的maven项目,需要导入一下依赖:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.8</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.8</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
Bean.java
package com.chry.WordToSql; public class Bean { public String pian; public String zhang; public String title; public String content; public Bean(String pian, String zhang, String title, String content) { super(); this.pian = pian; this.zhang = zhang; this.title = title; this.content = content; } public String getPian() { return pian; } public String getZhang() { return zhang; } public String getTitle() { return title; } public String getContent() { return content; } public void setPian(String pian) { this.pian = pian; } public void setZhang(String zhang) { this.zhang = zhang; } public void setTitle(String title) { this.title = title; } public void setContent(String content) { this.content = content; } public Bean() { } @Override public String toString() { return "Bean [pian=" + pian + ", zhang=" + zhang + ", title=" + title + ", content=" + content + "]"; } }
Dao.java
package com.chry.WordToSql; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class Dao { public void add(Bean bean) { Connection connection = DBUtil.getConnection(); PreparedStatement preparedStatement = null; String sql = "insert into data (pian,zhang,title,content) value (?,?,?,?)"; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, bean.getPian()); preparedStatement.setString(2, bean.getZhang()); preparedStatement.setString(3, bean.getTitle()); preparedStatement.setString(4, bean.getContent()); preparedStatement.executeUpdate(); System.out.println("插入成功"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { DBUtil.close(preparedStatement); DBUtil.close(connection); } } }
DBUtil.java
package com.chry.WordToSql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { public static Connection getConnection(){ try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (InstantiationException e) { // TODO �Զ����ɵ� catch �� e.printStackTrace(); } catch (IllegalAccessException e) { // TODO �Զ����ɵ� catch �� e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO �Զ����ɵ� catch �� e.printStackTrace(); } String user="root"; String password="root"; String url="jdbc:mysql://localhost:3306/bigdata"; Connection connection=null; try { connection=DriverManager.getConnection(url, user, password); } catch (SQLException e) { // TODO �Զ����ɵ� catch �� e.printStackTrace(); } return connection; } public static void close(Connection connection) { try { if(connection!=null) { connection.close(); } } catch (SQLException e) { // TODO �Զ����ɵ� catch �� e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement) { try { if(preparedStatement!=null) { preparedStatement.close(); } } catch (SQLException e) { // TODO �Զ����ɵ� catch �� e.printStackTrace(); } } public static void close(ResultSet resultSet) { try { if(resultSet!=null) { resultSet.close(); } } catch (SQLException e) { // TODO �Զ����ɵ� catch �� e.printStackTrace(); } } }
注:链接数据库的用户名,密码,还有数据库的名称都要根据的情况进行修改。
WordToSql.java
package com.chry.WordToSql; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.List; import org.apache.poi.xwpf.extractor.XWPFWordExtractor; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; public class WordToSql { public static String pian; public static String zhang; public static void readAndWriterTest4() throws IOException { File file = new File("D:\2.docx"); String str = ""; try { FileInputStream fis = new FileInputStream(file); XWPFDocument xdoc = new XWPFDocument(fis); List<XWPFParagraph> paras=xdoc.getParagraphs(); Bean bean = new Bean(); for(XWPFParagraph p : paras)//遍历段落 { //获取段落的级别 String jibie = p.getStyleID(); if(jibie!=null) { if(jibie.equals("8")) { pian = p.getParagraphText(); bean.setPian(pian); }else if(jibie.equals("9")) { zhang = p.getParagraphText(); bean.setZhang(zhang); }else if(jibie.equals("10")){ //判断的段落为标题 //System.out.println(bean.toString()); //写入数据库 Dao dao = new Dao(); dao.add(bean);
bean.setContent(""); String title = p.getParagraphText(); bean.setTitle(title); }else { //判断段落为正文 String content = p.getParagraphText();
if(bean.getContent()!=null) { content = content + bean.getContent(); } bean.setContent(content); } } } fis.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { readAndWriterTest4(); } }
至此,便可以将一片word文档完整的导入数据库中,后续会进一步研究如何将更加复杂的word文档导入数据库中。
注:在word文档中,每个人的字号的级别都不太一样,所以可以先写一个测试函数,输出每个段落的字号,再去修改正式导入数据库中的代码,本博客是经过修正之后的。
如有问题,请留言。