Sometimes in our actual work, we need to import data from Excel into the database. If the amount of data is hundreds or even more, I believe that a little bit of ctrlc and ctrlv is not a solution. Here we take the MySQL database as an example to store the data in Excel into the database. My idea is: first take out the data from Excel, and then transfer the data to the database. To operate Excel, you need jxl.jar, and to operate the database, you can use the most basic jdbc, which requires the jar package mysql-connector-java-5.0.8-bin.jar. Let's take a look at the Excel screenshot: Let’s look at the final rendering: The entire code is posted below: package browser; import java.io.File; import java.sql.*; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; public class PushExcelToMysql { public static void main(String[] args) throws Exception { Sheet sheet; Workbook workbook; Cell [][] cells=new Cell[3][3]; try{ workbook = Workbook.getWorkbook(new File("E:\\lalala.xls")); sheet = workbook.getSheet(0); for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ cells[i][j]=sheet.getCell(j,i); } } }catch (Exception e) { e.printStackTrace(); } try{ Class.forName("com.mysql.jdbc.Driver"); }catch(Exception e){ e.printStackTrace(); } Connection c=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8", "root","root"); String sql="insert into tosql(name,age,sex) values(?,?,?)"; PreparedStatement ps=c.prepareStatement(sql); for(int i=0;i<3;i++){ ps.setString(1, cells[i][0].getContents()); ps.setString(2, cells[i][1].getContents()); ps.setString(3, cells[i][2].getContents()); ps.execute(); } System.out.println("ok"); } } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: WeChat applet silent login and maintenance of custom login state detailed explanation
>>: A brief introduction to Tomcat's overall structure
XML/HTML CodeCopy content to clipboard < butto...
Table of contents 1.Json string 1.1Json Syntax 1....
I'm currently learning about front-end perform...
Table of contents 1. What is a custom instruction...
Detailed explanation of MySql automatic truncatio...
This article shares the specific code of Vue to i...
When I was taking a break, a phone call completel...
1. Command Introduction The ipcs command is used ...
The specific steps of installing mysql5.7.18 unde...
How to write Vue foreach array and traverse array...
The first point to make is that people can judge t...
Preface: I'm currently learning Linux and .Ne...
Table of contents Preface Core code File shows pa...
Problems that may arise from optimization Optimiz...
This article discusses the difficulties and ideas...