一.新建一个java项目
如前面步骤不需要可以直接跳转到二功能实现
1.导入jar包
新建一个lib导入相应jar包,图片如下
jar包已提供资源(大家自行下载)
2.编写DBHelper
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBHelper {
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static Connection getConn() {
Connection conn = null;
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_stu","root","123456");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
public static void MyClose(Connection conn,PreparedStatement ps,ResultSet rs) {
try {
if(conn!=null&&!conn.isClosed()) {
conn.close();
}
if(ps!=null) {
ps.close();
}
if(rs!=null) {
rs.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(getConn());
}
}
连接MySQL数据库,博主这里用的是MySQL8.2的jar包
3.编写实体类以及dao类
博主这里只提供dao类方法
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class StuDao {
//查询所有
List<stu> cxsy() {
Connection conn =null;
PreparedStatement ps = null;
ResultSet rs =null;
List<stu> list = new ArrayList<stu>();
try {
conn= DBHelper.getConn();
ps=conn.prepareStatement("select * from tb_stu");
rs=ps.executeQuery();
while (rs.next()){
list.add(new stu(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4)));
}
}catch(Exception e){
e.printStackTrace();
}finally {
DBHelper.MyClose(conn,ps,rs);
}
return list;
}
//删除
int SC(int sid){
Connection conn =null;
PreparedStatement ps = null;
ResultSet rs =null;
List<stu> list = new ArrayList<stu>();
try {
conn= DBHelper.getConn();
ps=conn.prepareStatement("delete from tb_stu where sid = "+sid);
return ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally {
DBHelper.MyClose(conn,ps,rs);
}
return 0;
};
//添加
int xz(stu st){
Connection conn =null;
PreparedStatement ps = null;
ResultSet rs =null;
List<stu> list = new ArrayList<stu>();
try {
conn= DBHelper.getConn();
ps=conn.prepareStatement("insert into tb_stu(sid,sname,ssex,sage) value(?,?,?,?)");
ps.setInt(1,st.getSid());
ps.setString(2,st.getSname());
ps.setString(3,st.getSsex());
ps.setInt(4,st.getSage());
return ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally {
DBHelper.MyClose(conn,ps,rs);
}
return 0;
};
}
二.实现数据从MySQL写入到Excel中
1.创建一个实现类
2.实例化dao方法
在try内创建工作薄,通过流对象找到文件路径,若没有路径系统会自动创建
初始化表格后拿到表头第一行,因为要编写中文所以表头的第一行不需要遍历,所以放在循环外面,循环内创建的新行需要i+1循环完成之后写入到Excel表格中代码如下:
3.完整代码
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.util.List;
public class text {
public static void main(String[] args) {
StuDao stu = new StuDao();
try (Workbook work = new XSSFWorkbook();//创建工作簿
FileOutputStream file = new FileOutputStream("C:\\Users\\fujie\\Desktop\\aaa.xlsx");//找到文件路径
){
Sheet se = work.createSheet();//初始化Excel表格
List<main.java.stu> cxsy = stu.cxsy();//调用方法
Row row = se.createRow(0);//占用第一行作为表头
Cell cell = row.createCell(0);
cell.setCellValue("编号");//第一个位置赋值为编号,下同
Cell cell2 = row.createCell(1);
cell2.setCellValue("姓名");
Cell cell3 = row.createCell(2);
cell3.setCellValue("性别");
Cell cell4 = row.createCell(3);
cell4.setCellValue("年龄");
for (int i = 0; i < cxsy.size(); i++) {//遍历数据
Row ro = se.createRow(i+1);//创建新的行
Cell idcell = ro.createCell(0);//获取第一个单元格
idcell.setCellValue(cxsy.get(i).getSid());//将数据赋值到单元格中
Cell namecell = ro.createCell(1);
namecell.setCellValue(cxsy.get(i).getSname());
Cell sexcell = ro.createCell(2);
sexcell.setCellValue(cxsy.get(i).getSsex());
Cell agecell = ro.createCell(3);
agecell.setCellValue(cxsy.get(i).getSage());
}
work.write(file);//写入到Excel表格中
System.out.println("取出完成");
}catch (Exception e){
e.printStackTrace();
}
}
}
三.实现Excel数据导入到Mysql中
1.创建实现类并创建对象
本文本有注释,大家自行理解
2.循环取出数据
这里大家需要注意一下由于Excel如果输入的是数字形式导入到MySQL会报错,所以我们需要将其强行转换为String类型。
第一个if判断的意思是将Excel表头的数据取出掉,如果是第一行则直接退出循环,进入到下一循环第二个与第三个if判断均为强转。
3.完整代码
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class text2 {
public static void main(String[] args) {
StuDao st = new StuDao();
try ( FileInputStream stream = new FileInputStream("C:\\Users\\fujie\\Desktop\\aaa.xlsx");){
// 创建工作簿
XSSFWorkbook workbook = new XSSFWorkbook(stream);
// 获取一个工作表,下标从0开始
XSSFSheet sheet = workbook.getSheetAt(0);
// 通过循环,逐行取出表中每行数据
for (Row r:sheet) {
if(r.getRowNum()==0){//判断是否是第一行是第一行则跳过此循环,执行下一个循环
continue;
}
Cell cell = r.getCell(1);//获取到name数据
if (cell!=null){//判断不为空
cell.setCellType(Cell.CELL_TYPE_STRING);//则将数据类型转换为String
}
Cell cell1 = r.getCell(2);
if (cell1!=null){
cell1.setCellType(Cell.CELL_TYPE_STRING);
}
int a = (int)r.getCell(0).getNumericCellValue();//获取数据并强转为int类型接收
String b = cell.getStringCellValue();
String c = cell1.getStringCellValue();
int d = (int)r.getCell(3).getNumericCellValue();
stu stu = new stu(a,b,c,d);//实例化
st.SC(a);//删除编号相同的数据
st.xz(stu);//新增
System.out.println("加入成功");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
好啦,完整代码都贡献给大家啦!大家多多关注关注博主!持续更新哈哈哈^_^