批量向数据库导入数据:各数据段以 | 作为分界线(使用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 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 { 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 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文件,提取数据,存入数据库中。 以上内容由 华夏名网 搜集整理,如转载请注明原文出处,并保留这一部分内容。从文件导入: