读文件要用流,比如FileInputStream,或BufferedReader等都可以。另外,BufferedReader支持readLine(),一行行的读。插入到数据库的话,文件内容小,就用varchar字段存就行了;大的话用Blob字段直接存流。你这个用varchar就行了。
解决方案 »
- Struts2上传文件的怪问题,在线等解答
- 关于CVS下载下来的项目用MyEclipse部署的问题,求助!急!
- 急救:怎么解决大量图片显示时报错????!!!!
- JAVA实现RSS
- flexigrid表格出现滚动条时列和下面的数据错位的问题
- 严重: Servlet.service() for servlet [jsp] in context with path [] threw exception
- 写个连接池出现严重错误,TOMCAT6.0 JDBC driver原因?
- 一个简单问题:关于sql的!!help me!
- 请问,这是什么错误!java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][Named Pipes]??????? SQL Server?虽然分少,但一定给,只要您是前5名回复者中最好的以为!
- https下 uploadify控件 servlet中获取图片流位0字节
- Servlet只实例化一次,那不同用户访问,数据岂不乱套了?
- 一个在一个form里实现多个input请求action
2.但是楼主要读取word的话,就要借助工具吧了 给你推荐2个常用工具包吧 apache POI /jacob 前者不需要dll文件,读取方便,后者写,操作word方便,楼主只有读的需求,用POI就好了,先读取所有的文本,然后分行处理就好了
代码如下:import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;import org.apache.poi.hwpf.extractor.WordExtractor;
public class jdbc_neike { static Connection conn;
static Statement st;
public static void main(String[] args) throws Exception{
FileInputStream inp = null;
try {
inp = new FileInputStream("E:/bigdatagroup/temp2.doc");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//System.out.println("kaishi!!!!!!");
WordExtractor extractor = null;
try {
extractor = new WordExtractor(inp);
} catch (IOException e) {
e.printStackTrace();
}
String str=extractor.getText();
System.out.println(str);
String[] paragraphText=extractor.getParagraphText();
System.out.println("改文档一共有"+paragraphText.length+"段");
int n=paragraphText.length;
conn=getConnection();
for(int i=0;i<n;i++){
System.out.println("第"+i+"段");
System.out.println(paragraphText[i]);
String[] arr = paragraphText[i].split("\t");
System.out.println("arr[0]:"+arr[0]);
System.out.println("arr[1]:"+arr[1]);
String sql="update doc_dep set time='"+arr[0]+"' where doc_name='"+arr[1]+"'";
try {
st=(Statement)conn.createStatement();
int count=st.executeUpdate(sql);
System.out.println("向doc_dep表中插入 " + count + " 条数据"); //输出插入操作的处理结果
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("插入数据库失败 " +e.getMessage());
}
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("执行完毕!!");
}
//获取数据库连接的函数
public static Connection getConnection(){
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/guahao", "root", "wenruijie");
System.out.println("数据库连接成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库连接失败"+e.getMessage());
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败"+e.getMessage());
}
return con;
}}
可以提取出word中的内容,但是就是更新不到数据表中,控制台输出如下:
kaishi!!!!!!
周四上午 单兆亮
周三上午 王广义
周四下午 谢晓华
周一下午、周三上午 刘秀华改文档一共有4段
数据库连接成功
第0段
周四上午 单兆亮arr[0]:周四上午
arr[1]:单兆亮update doc_dep set time='周三' where doc_name='汪寅'
向doc_dep表中插入 0 条数据
第1段
周三上午 王广义arr[0]:周三上午
arr[1]:王广义update doc_dep set time='周三' where doc_name='汪寅'
向doc_dep表中插入 0 条数据
第2段
周四下午 谢晓华arr[0]:周四下午
arr[1]:谢晓华update doc_dep set time='周三' where doc_name='汪寅'
向doc_dep表中插入 0 条数据
第3段
周一下午、周三上午 刘秀华arr[0]:周一下午、周三上午
arr[1]:刘秀华update doc_dep set time='周三' where doc_name='汪寅'
向doc_dep表中插入 0 条数据
执行完毕!!这是怎么回事呢?帮忙看一下 谢谢