任务:
- 把存储在在Excel中的多条User类数据导入到目标数据库
- 把存储在在数据库中的多条User类数据导出到Excel表格
准备阶段:
- 配置了Mysql数据库
- 编译器导入了 jxl.jar 包,Excel表格处理包
- 编译器导入了 sql.jar 包,Mysql包
全部代码如下:
JavaBean类(User类):
package sample;
/**
* User账号类
*/
public class User {
private String userName;
private String password;
private String sex;
private String address;
private String tel;
private int flowerSum;
private String time;
public User(){
}
public User(String userName,String password){
this.userName = userName;
this.password = password;
}
public User(String userName,String password,String sex,String address,String tel,int flowerSum,String time){
this(userName, password);
this.sex = sex;
this.address = address;
this.tel = tel;
this.flowerSum = flowerSum;
this.time =time;
}
//setter和getter方法,要是没写无法读入tableView
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public int getFlowerSum() {
return flowerSum;
}
public void setFlowerSum(int flowerSum) {
this.flowerSum = flowerSum;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", password='" + password + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
", tel='" + tel + '\'' +
", flowerSum=" + flowerSum +
", time='" + time + '\'' +
'}';
}
}
数据库工具类:
作用:连接数据库,对数据库进行增删改查
package sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import sta