我是靠谱客的博主 不安奇异果,这篇文章主要介绍excel 数据 jdbc 导入到数据库,现在分享给大家,希望可以做个参考。

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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")); } }

 

最后

以上就是不安奇异果最近收集整理的关于excel 数据 jdbc 导入到数据库的全部内容,更多相关excel内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部