读取excel数据插入表

1)准备工作-jar包

a、mysql-connector-java-5.1.20-bin.jar

b、ojdbc14.jar

c、poi-3.10.1-20140818.jar

d、poi-excelant-3.10.1-20140818.jar

e、poi-ooxml-3.10.1-20140818.jar

f、poi-ooxml-schemas-3.10.1-20140818.jar

g、poi-scratchpad-3.10.1-20140818.jar


2)实施-创建普通Java工程,编写代码

// 输入参数判断
		// 1)hostFileUrl设备文件路径(从设备管理列表导出的excel文件)
		// 2)DB_URL
		// 3)Username
		// 4)Password
		if (args == null || args.length < 4) {
			System.out.println("传递的参数不足4个【hostFilePath,Db_url,Username,Password】,(含空格用双引号引起)参数之间用空格分开,路径不能含中文。");
			return;
		}
		
		// 获取JDBC连接
		Connection connection = getConnection(args);
		Statement statement = null;
		// 解析数据
		
		HSSFWorkbook hwb = null;
		try {
			File file = new File(args[0]);
			hwb = new HSSFWorkbook(new FileInputStream(file));
			
			HSSFSheet e = hwb.getSheet("sheet名");
			statement = connection.createStatement();

			for (int i = 1; i <= e.getLastRowNum(); ++i) {
				HSSFRow row = e.getRow(i);
				Map<Integer, String> map = new HashMap<Integer, String>();

				for (int j = 0; j < row.getLastCellNum(); ++j) {
					HSSFCell cell = row.getCell(j);
					cell.setCellType(1);
					map.put(Integer.valueOf(j), cell.getStringCellValue());
				}
				
				// 查询主机是否存在
				if(StringUtils.isNullOrEmpty(map.get(0))){
					continue;
				}
				ResultSet resultSet = statement.executeQuery("select * from `hosts` where `name`='" + map.get(1) + "'");
				if(!resultSet.next()){
					continue;
				}

				// 插入数据库连接
				StringBuffer sqlStr = new StringBuffer();
				strBuffer.append("INSERT INTO table values(...)");
				statement.addBatch(strBuffer.toString());
			}
			
			
			e = hwb.getSheet("sheet名");
			for (int i = 1; i <= e.getLastRowNum(); ++i) {
				HSSFRow row = e.getRow(i);
				Map<Integer, String> map = new HashMap<Integer, String>();

				for (int j = 0; j < row.getLastCellNum(); ++j) {
					HSSFCell cell = row.getCell(j);
					String value = "";
					if(cell != null){
						cell.setCellType(1);
						value = cell.getStringCellValue();
					}
					map.put(Integer.valueOf(j), value);
				}
				
				// 查询主机是否存在
				if(StringUtils.isNullOrEmpty(map.get(0))){
					continue;
				}
				ResultSet resultSet = statement.executeQuery("select * from `hosts` where `name`='" + map.get(1) + "'");
				if(!resultSet.next()){
					continue;
				}

				// 插入数据库连接
				StringBuffer strBuffer = new StringBuffer();
				strBuffer.append("INSERT INTO table values(...)");
				statement.addBatch(strBuffer.toString());
			}
			
			statement.executeBatch();
			System.out.println("数据插入成功.");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				System.out.println("关闭连接.");
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	
	
	private static Connection getConnection(String[] args) {
		// 获取参数
		String url = args[1];
		String user = args[2];
		String password = args[3];

		// 设置驱动名称
		String driverName = "oracle.jdbc.driver.OracleDriver";
		if (url.contains("jdbc:mysql")) {
			driverName = "com.mysql.jdbc.Driver";
		}

		// 加载驱动,建立连接
		Connection con = null;
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url, user, password);
			System.out.println("db连接成功.");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} 

		return con;
	}

3)打成jar包,在命令行运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值