思路:先把地区的疫情数据写入到txt文件中,读取该txt文件返回一个字符串,然后再把字符串分割,封装成javaBean对象。连接数据库,把增删改查操作,把javaBean对象添加到数据库中。
数据库表:
create table info(
id int primary key auto_increment,
infoLocation varchar(20),
infoAdd int,
infoTotal int,
infoCure int,
infoDie int
)
项目结构:
JavaBean
public class Info{
private int infoId;//地区id
private String infoLocation;//地区名字
private int infoAdd;//新增确诊人数
private int infoTotal;//确诊总人数
private int infoCure;//治愈人数
private int infoDie;//死亡人数
public Info(){
}
public Info(int infoId, String infoLocation, int infoAdd, int infoTotal, int infoCure, int infoDie) {
this.infoId = infoId;
this.infoLocation = infoLocation;
this.infoAdd = infoAdd;
this.infoTotal = infoTotal;
this.infoCure = infoCure;
this.infoDie = infoDie;
}
public int getInfoId() {
return infoId;
}
public void setInfoId(int infoId) {
this.infoId = infoId;
}
public String getInfoLocation() {
return infoLocation;
}
public void setInfoLocation(String infoLocation) {
this.infoLocation = infoLocation;
}
public int getInfoAdd() {
return infoAdd;
}
public void setInfoAdd(int infoAdd) {
this.infoAdd = infoAdd;
}
public int getInfoTotal() {
return infoTotal;
}
public void setInfoTotal(int infoTotal) {
this.infoTotal = infoTotal;
}
public int getInfoCure() {
return infoCure;
}
public void setInfoCure(int infoCure) {
this.infoCure = infoCure;
}
public int getInfoDie() {
return infoDie;
}
public void setInfoDie(int infoDie) {
this.infoDie = infoDie;
}
@Override
public String toString() {
return "Info{" +
"infoId=" + infoId +
", infoLocation='" + infoLocation + '\'' +
", infoAdd=" + infoAdd +
", infoTotal=" + infoTotal +
", infoCure=" + infoCure +
", infoDie=" + infoDie +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Info info = (Info) o;
return infoId == info.infoId &&
infoAdd == info.infoAdd &&
infoTotal == info.infoTotal &&
infoCure == info.infoCure &&
infoDie == info.infoDie &&
infoLocation.equals(info.infoLocation);
}
@Override
public int hashCode() {
return Objects.hash(infoId, infoLocation, infoAdd, infoTotal, infoCure, infoDie);
}
}
业务逻辑实现InfoDao
public class InfoDao {
private Driver driver;
java.sql.Connection conn;
public Connection getConnection() {//JDBC获取数据库的连接
try {
driver = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/bigdata?useUnicode=true&characterEncoding=utf8";
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "xxxx");
conn = driver.connect(url, info);
return conn;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public int addInfo(Info info) {//根据info对象插入到数据库中
conn = getConnection();
try {
String sql = "insert into info values(?,?,?,?,?,?)";
PreparedStatement statement = conn.prepareStatement(sql);//预编译sql
statement.setInt(1, info.getInfoId());
statement.setString(2, info.getInfoLocation());
statement.setInt(3, info.getInfoAdd());
statement.setInt(4, info.getInfoTotal());
statement.setInt(5, info.getInfoCure());
statement.setInt(6, info.getInfoDie());
int res = statement.executeUpdate();
return res;
} catch (SQLException e) {
e.printStackTrace();
return -1;
}
}
public int deleteInfo(int infoId) {//根据infoId删除数据库的数据信息
conn = getConnection();
try {
String sql = "delete from info where infoId=?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setInt(1, infoId);
int res = statement.executeUpdate();
return res;
} catch (SQLException e) {
e.printStackTrace();
return -1;
}
}
public int updateInfo(int infoId, Info info) {//根据infoId修改数据为info对象的信息
conn = getConnection();
try {
String sql = "update info set infoId=?,infoLocation=?,infoAdd=?,infoTotal=?,infoCure=?,infoDie=? where infoId=?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setInt(1, info.getInfoId());
statement.setString(2, info.getInfoLocation());
statement.setInt(3, info.getInfoAdd());
statement.setInt(4, info.getInfoTotal());
statement.setInt(5, info.getInfoCure());
statement.setInt(6, info.getInfoDie());
statement.setInt(7,infoId);
int res = statement.executeUpdate();
return res;
} catch (SQLException e) {
e.printStackTrace();
return -1;
}
}
public Info getInfoById(int infoId) {//根据infoId查询数据,返回Info对象
conn = getConnection();
Info info = null;
try {
String sql = "select * from info where infoId=?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setInt(1, infoId);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
ResultSetMetaData metaData = resultSet.getMetaData();
int getInfoId = resultSet.getInt(1);
String getInfoLocation = resultSet.getString(2);
int getInfoAdd = resultSet.getInt(3);
int getInfoTotal = resultSet.getInt(4);
int getInfoCure = resultSet.getInt(5);
int getInfoDie = resultSet.getInt(6);
info = new Info(getInfoId, getInfoLocation, getInfoAdd, getInfoTotal, getInfoCure, getInfoDie);
}
return info;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public List<Info> getOrderedInfo() {//获得所有区域的数据,按照确诊总数降序排列
conn = getConnection();
String sql = "select * from info";
List<Info> list = new ArrayList<Info>();
Info info = null;
try {
PreparedStatement statement = conn.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
int getInfoId = resultSet.getInt(1);
String getInfoLocation = resultSet.getString(2);
int getInfoAdd = resultSet.getInt(3);
int getInfoTotal = resultSet.getInt(4);
int getInfoCure = resultSet.getInt(5);
int getInfoDie = resultSet.getInt(6);
info = new Info(getInfoId, getInfoLocation, getInfoAdd, getInfoTotal, getInfoCure, getInfoDie);
list.add(info);
}
Collections.sort(list, new Comparator<Info>() {
@Override
public int compare(Info o1, Info o2) {
if(o1.getInfoId()>o2.getInfoId()){
return 1;
}else if(o1.getInfoId()<o2.getInfoId()){
return -1;
}else{
return 0;
}
}
});
return list;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
读取txt文件,返回疫情数据的字符串
public class IoUtil {
private final String fileUrl = "E:\\AllCode\\Big_Data\\src\\bigdata.txt";//存放txt文件的路径
public String readFile() {//读取疫情数据txt文件,返回一个字符串
BufferedReader bufferedReader = null;
StringBuffer stringBuffer = new StringBuffer("");
char[] c = new char[16];
try {
bufferedReader = new BufferedReader(new FileReader(fileUrl));
int len = 0;
while (true) {
try {
if (!((len = bufferedReader.read(c)) != -1)) break;
} catch (IOException e) {
e.printStackTrace();
}
stringBuffer.append(new String(c, 0, len));
} // 追加字符串
return stringBuffer.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
}
将疫情数据的字符串,分割过后加入到list
public class InfoUtil {
private final String fileUrl="src\\bigdata.txt"; //txt 文件路径。
public String preString (String s,String oldString,String newString){//完成字符串的替换
return s.replaceAll(oldString,newString);//字符串的替换
}
public List<Info> stringToInfo(String s){//将特定规格字符串转换为info实体类对象,添加进List集合并返回
ArrayList<Info> infoList = new ArrayList<>();
String[] infoStrings = s.split(";");//先按照";"拆分字符串
for (String infoString:infoStrings){
String[] props = infoString.split(",");//再按照","分割字符串
Info info = new Info();//把分出来的字符串封装成JavaBean对象
info.setInfoId(Integer.parseInt(props[0]));
info.setInfoLocation(props[1]);
info.setInfoAdd(Integer.parseInt(props[2]));
info.setInfoTotal(Integer.parseInt(props[3]));
info.setInfoCure(Integer.parseInt(props[4]));
info.setInfoDie(Integer.parseInt(props[5]));
infoList.add(info);//添加进list
}
return infoList;
}
}
测试
public class IoUtilTest {
public static void main(String[] args) {
IoUtil ioUtil = new IoUtil();
String s = ioUtil.readFile();//读取疫情数据的txt文件
// System.out.println(s);
InfoUtil infoUtil = new InfoUtil();
List<Info> infos = infoUtil.stringToInfo(s);//把疫情数据的txt文件转换成String类型
InfoDao infoDao = new InfoDao();
int size=infos.size();//获取infos这个list的实例数目
while (size>0){
size--;
Info info = infos.get(size);
infoDao.addInfo(info);
}
}
}