最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

解析文件写入数据库代码

来源:动视网 责编:小OO 时间:2025-09-30 09:00:43
文档

解析文件写入数据库代码

1、自动解析文件写入数据库代码批量向数据库导入数据:各数据段以|作为分界线(使用struts框架)action:publicActionForwardupLoadOne(ActionMappingmapping,ActionFormform,   HttpServletRequestrequest,HttpServletResponseresponse){  UpfileFormupfileForm=(UpfileForm)form;//TODOAuto-generated          
推荐度:
导读1、自动解析文件写入数据库代码批量向数据库导入数据:各数据段以|作为分界线(使用struts框架)action:publicActionForwardupLoadOne(ActionMappingmapping,ActionFormform,   HttpServletRequestrequest,HttpServletResponseresponse){  UpfileFormupfileForm=(UpfileForm)form;//TODOAuto-generated          
1、自动解析文件写入数据库代码

批量向数据库导入数据:各数据段以 | 作为分界线(使用struts框架)

action:

public ActionForward upLoadOne(ActionMapping mapping, ActionForm form,

   HttpServletRequest request, HttpServletResponse response) {

  UpfileForm upfileForm = (UpfileForm) form;// TODO Auto-generated

             // method stub

  FormFile file = upfileForm.getFiledata();// 取得上传的文件

  String encoding = request.getCharacterEncoding(); // 设置编码

  if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {

   response.setContentType("text/html; charset=gb2312");// 如果没有指定编码,编码格式为gb2312

  }

  ServletContext context = getServlet().getServletContext();

  String filePath = context.getRealPath("/") + "upLoad";

 

  UpLoad up = new UpLoad();

  try {

    String path = up.up(filePath,file);

    HashMap map = up.Validate(path);

    request.setAttribute("all", map.get(0));

    request.setAttribute("err", map.get(1));

    request.setAttribute("ret", map.get(2));

  } catch (Exception e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

   return mapping.findForward("win");

 }

 

server:

public class UpLoad {

 public String up(String path, FormFile file) throws Exception {

  // TODO 自动生成方法存根

    String dataPath = null;  // 数据路径

    String fileName = null;

          InputStream stream = file.getInputStream();// 把文件读入

           // String filePath1 = request.getRealPath("/");//取当前系统路径

       

          //  String filePath = getServlet().getServletContext().getRealPath("/")+"upLoad";  // Servlet 取路径的方式

         

           if(!new File(path).isDirectory())         //如果没有此文件夹就建立一个

           {

            new File(path).mkdirs();

           }

           fileName = file.getFileName();

           dataPath = path +"/"+ fileName;

           OutputStream bos = new FileOutputStream(dataPath);// 建立一个上传文件的输出流

           int bytesRead = 0;

           byte[] buffer = new byte[8192];

           while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {

             bos.write(buffer, 0, bytesRead);// 将文件写入服务器  

           }

           bos.close();

           stream.close();

           return dataPath;

          }

 public HashMap Validate(String path)throws FileNotFoundException ,IOException,DatatypeException, SQLException

 {

   HashMap map_1 = new HashMap();

  String   record   =   null;

  String   fileName   =   path;

  int err = 0;   //错误条数

  int all = 0;   //总条数

  FileReader  fr = new FileReader(fileName);

  BufferedReader br =  new BufferedReader(fr);

  while ((record = br.readLine()) != null)

  {

    all++;

    String[] re = record.split("\\\|");

    int cont = re.length;

    if(cont != 4)

    {

   err++;

   continue;

    }

    HashMap map = new HashMap();

    int i = 0;

    for (String string :re)

    {

     map.put(i, string);

     i++;

    }

   

    //往数据库中批量导入用户资料

   userreg(map.get(0),map.get(1),map.get(2),map.get(3));

   // System.out.println(map.toString()); //map 中存放分析好的数据

  }

  

  map_1.put(0,all);

  map_1.put(1,err);

  map_1.put(2,all-err);

  //System.out.println(map_1);  map_1 中放的是 上传结果 成功多少失败多少总共多少

  return map_1;

 }

 

 

 //往数据库中批量导入用户资料

 public void userreg(String str1,String str2,String str3,String str4) throws SQLException

 {

  String sql = "insert into gs_login(logno,logpass,logname,loglevel) values('"+str1+"','"+str2+"','"+str3+"','"+str4+"')";

  DBOperate.execute(sql);

 }

 

对应的jsp页面:

 

 从文件导入:

 

   

   

 

        返回

         

2、

String sql = "insert into SalesList (title, category, pubDate, link, description) values('"+ title +"', '"+ category +"', '"+ pubDate +"', '"+ link +"', '"+ description +"')" ;

stmt.executeUpdate(sql) ; 

(title, category, pubDate, link, description) 这里的值title等是表中建立的字段名

('"+ title +"', '"+ category +"', '"+ pubDate +"', '"+ link +"', '"+ description +"')这些值title等是存储的变量。

3、思想:当我们要把xml格式的数据存到数据库中。通常的办法是解析xml文件,读出xml中的数据类型并根据它创建数据库表和表之间的结构,读出xml中的数据,存入到数据库之中。

  在读nodeType是要注意二个问题:

  1.读某个节点数据类型可以对上一个节点的孩子,或直接使本节点的类型,只不过要严格注意定位正确,不要混淆。

  例:link.getElementsByTagName("link").item(0).getFirstChild().getNodeType()

  2.对于nodetype的值,如果一般可以得到数据,也可以是与Node类的静态属性值相配的。

如:Node.DOCUMENT_NODE

    Node.ELEMENT_NODE

    Node.CDATA_SECTION_NODE

    Node.TEXT_NODE

  好像与0,1,2,3相对应,自己可以测试一下。

  其实上面的方法并不是一件可取的方法,一种比较好的替代方法是:

  1.使用xnlSchema来描述该xml文件。

  2.对schema文件进行xml解析,获得实体及其类型,然后根据实体名和类型来创建表,根据实体之间的关系,设定表之间的关系。

  3.用xmldom或sxap来解析xml文件,提取数据,存入数据库中。

   

以上内容由 华夏名网 搜集整理,如转载请注明原文出处,并保留这一部分内容。

文档

解析文件写入数据库代码

1、自动解析文件写入数据库代码批量向数据库导入数据:各数据段以|作为分界线(使用struts框架)action:publicActionForwardupLoadOne(ActionMappingmapping,ActionFormform,   HttpServletRequestrequest,HttpServletResponseresponse){  UpfileFormupfileForm=(UpfileForm)form;//TODOAuto-generated          
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top