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
I used Vue.js to make a nine-grid image display m...
Table of contents Manual deployment 1. Create a s...
The parameter passed by ${param} will be treated ...
Including the use of check boxes and radio buttons...
When I first started setting up an ftp server on ...
Root directory and index file The root directive ...
Earlier we talked about how to make a square with...
Table of contents 1. What is Pinia? 2. Pinia is e...
1. Scenario description: Our environment uses mic...
The pitfalls 1. Many tutorials on the Internet wr...
Table of contents Preface 1. Demand and Effect ne...
A very useful function group_concat(), the manual...
1. Shut down the mysql service # service mysqld s...
NERDTree is a file system browser for Vim. With t...
What is bond NIC bond is a technology that is com...