java 导入导出txt

Oracle数据导出成txt格式

dao
                
/**
	 * 获得所有数据
	 */
	public  String getAllWord() {
		Session session=super.getSession();
		StringBuffer strb = new StringBuffer();
		String sql="SELECT * FROM WORD_T"; 
		try {
			Connection conn = session.connection();
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				String word = rs.getString("word");
				long num =  rs.getLong("num");
				strb.append(word + "\t" + num + "\r\n");
			}
			rs.close();
			st.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return strb.toString();
	}
	
	
	/**
	 * 批量导入数据
	 * @throws SQLException 
	 */
	@SuppressWarnings("deprecation")
    public void insertWord(String word,Long num){
		Session session=super.getSession(); 
		Connection conn=session.connection();
		PreparedStatement stmt;
        try
        {
            stmt = conn.prepareStatement("insert into KEYWORD_T(id,word,num,last_time) values(WORD_ID_SEQ.nextval,?,?,sysdate)");
    		stmt.setString(1, word);
    		stmt.setLong(2, num);
    		stmt.execute();
    		stmt.close();
        } catch (SQLException e)
        {
            
        }
	}


Servlet
/**
*导出成txt格式
*/
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException { 
	 
		try {
			WebApplicationContext cxt = ContextLoader.getCurrentWebApplicationContext();
			WordDao wordDao=(WordDao)cxt.getBean("wordDao");
			String txt = wordDao.getAllWord();
			response.setHeader("Content-Disposition", "attachment; filename=\"download.txt\"");
			response.setHeader("Content-Type ", "application/octet-stream");

			/* 创建输出流 */
			ServletOutputStream servletOS = response.getOutputStream();
			try {
				IOUtils.write(txt, servletOS);
			} catch (RuntimeException e) { 
			}
			servletOS.flush();

		} catch (Exception e) {
			System.out.println("servlet导出异常  ");
		} finally {
			 
		}

Action 
/**
*导入txt到oracle数据库
*/

/**
	 * 关键字导入
	 * 
	 * @return
	 */
	public String fileUp() {
		if (super.getRequest().getMethod().equalsIgnoreCase("post")) {
			if (txtWord.length() == 0) {
				saveMessage("上传失败:请选择上传文件");
				return INPUT;
			}
			if (txtWord.length() > 10000000) {
				saveMessage("上传失败:请选择上传文件");
				return INPUT;
			}
			try {
				if (null != getTxtWordFileName()
						& idEnableUploadType(1, getTxtWordFileName())) {
					FileInputStream inStream = new FileInputStream(txtWord);
					List<String> lines = IOUtils.readLines(inStream, "GBK");
					Iterator<String> itr = lines.iterator();

					while (itr.hasNext()) {
						String line = itr.next();
						String[] temps = line.split("\t");
						Word word = new Word();
						try {
						if (temps.length > 1) {
							wordService.insertWord(lines.size(), temps[0], Long
									.valueOf(temps[1]));

						} else {
							wordService.insertWord(lines.size(), temps[0], Long
									.valueOf(1)); 
						} 
						} catch (Exception e) {
						 
						}
					}
				}
			} catch (Exception e) {
				 
			}

		}
		return SUCCESS;

	}	/**
	 * 判断文件格式
	 * 
	 * @param fileType
	 * @param filename
	 * @return
	 */
	public static boolean idEnableUploadType(int fileType, String filename) {
		String enableExtNames = null;
		int p = filename.lastIndexOf(".");
		String fileExtName = filename.substring(p).toLowerCase();
		if (fileType == 1) {
			enableExtNames = ".txt";
		}
		// else if(fileType==2)
		// {
		// enableExtNames="";
		// }
		if (enableExtNames != null) {
			if (enableExtNames.indexOf(fileExtName) != -1)
				return true;
			else
				return false;
		} else {
			return true;
		}

	}

你可能感兴趣的:(java,DAO,oracle,sql,servlet)