excel 数据 jdbc 导入到数据库

在项目开发中我们经常遇到 excel 数据导入系统的需求,如下代码 通过 jdbc 方式导入excel 数据到数据库:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class readFile {
	public static void readFile(File file) {
		Properties props = null;
		Connection con = null;
		PreparedStatement ps = null;
		String url = "";
		String user = "";
		String password = "";
		try {
			try {
				String filepath = readFile.class.getClassLoader().getResource("jdbc.properties").getFile();
				InputStream in;
				String websiteURL = filepath;
				if (!System.getProperties().getProperty("os.name").equals("Linux")) {
					websiteURL = (filepath.replace("%20", " ")).replaceFirst("/", "");
				}
				try {
					in = new FileInputStream(websiteURL);
					props = new Properties();
					props.load(in);
				} catch (Exception e) {
					e.printStackTrace();
				}
				String driverClassName = props.getProperty("jdbc.driverClassName");
				url = props.getProperty("jdbc.url.preference");
				user = props.getProperty("jdbc.username");
				password = props.getProperty("jdbc.password");
				Class.forName(driverClassName);
			} catch (ClassNotFoundException e) {
				throw new ExceptionInInitializerError(e);
			}
			con = DriverManager.getConnection(url, user, password);
			InputStream is = new FileInputStream(file);
			XSSFWorkbook wb = new XSSFWorkbook(is);
			XSSFCell cell = null;
			XSSFSheet st = wb.getSheetAt(0);
            //开始读取行 1
			for (int rowIndex = 0; rowIndex <= st.getLastRowNum(); rowIndex++) {
				String sql = "insert into Table_Name values(?,?,?,?,?)";
				ps = con.prepareStatement(sql);
				System.out.println(rowIndex);
				XSSFRow row = st.getRow(rowIndex);
                // 列数 20 
				for (int i = 0; i < 20; i++) {
					cell = row.getCell(i);
					System.out.println(i + "--->" + cell);
					if (cell == null) {
						ps.setString(i + 1, "");
					} else {
						cell.setCellType(Cell.CELL_TYPE_NUMERIC);
						ps.setDouble(i + 1, cell.getNumericCellValue());
						// cell.setCellType(Cell.CELL_TYPE_STRING);
						// ps.setString(i + 1, cell.getStringCellValue());
					}

				}
				ps.executeUpdate();
				ps.close();
			}
			try {
			} catch (Exception e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (Exception e2) {
				}
			}
			if (con != null) {
				try {
					con.close();
				} catch (Exception e2) {
				}
			}
		}
	}

	public static void main(String[] args) {
		readFile(new File("F:\\待导入数据1.xlsx"));
	}
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值