Java imports data from excel into mysql

Java imports data from excel into mysql

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:
  • How to add data to an existing Excel table in Java
  • Batch import method of excel table data in Java
  • Java implements the method of batch importing excel table data into the database
  • Java uses POI to parse Excel and get all cell data examples
  • How to import Excel data using Idea or Datagrip

<<:  WeChat applet silent login and maintenance of custom login state detailed explanation

>>:  A brief introduction to Tomcat's overall structure

Recommend

Json string + Cookie + localstorage in JS

Table of contents 1.Json string 1.1Json Syntax 1....

A detailed guide to custom directives in Vue

Table of contents 1. What is a custom instruction...

Detailed explanation of MySql automatic truncation example

Detailed explanation of MySql automatic truncatio...

Vue implements the countdown component for second kills

This article shares the specific code of Vue to i...

Steps for restoring a single MySQL table

When I was taking a break, a phone call completel...

Use of Linux ipcs command

1. Command Introduction The ipcs command is used ...

CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial

The specific steps of installing mysql5.7.18 unde...

Introduction to Semantic XHTML Tags

The first point to make is that people can judge t...

Implementation example of uploading multiple attachments in Vue

Table of contents Preface Core code File shows pa...

MySQL Optimization Solution Reference

Problems that may arise from optimization Optimiz...

Implementation of CSS linear gradient concave rectangle transition effect

This article discusses the difficulties and ideas...