读取csv文件,计算实发工资并将相应信息存入数据库中
Main类:
package org.example;
public class Main {
public static void main(String[] args) {
SalaryService salaryService = new SalaryService();
salaryService.read();
salaryService.compute();
salaryService.save();
}
}
Employee类:
package org.example;
import java.math.BigDecimal;
/**
* @author xhh
* @version 1.0
* @since 1.0
*/
public class Employee {
Integer id;
String name;
String gender;
String job;
String type;
Double baseSalary;
Double timeSalary;
Double bonus;
Double insurance;
Integer days;
Double totalSalary;
}
SalaryService类:
package org.example;
import sun.nio.cs.ext.GBK;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author xhh
* @version 1.0
* @since 1.0
*/
public class SalaryService {
List<Employee> list = new ArrayList<>();
/**
* 计算工资
*/
public void compute(){
for (Employee e : list) {
if("专职".equals(e.type)) {
e.totalSalary = e.baseSalary + e.bonus - e.insurance;
} else if("兼职".equals(e.type)) {
e.totalSalary = e.timeSalary * e.days * 8;
}
}
}
/**
* 读取文件
*/
public void read(){
String path = "/Users/xhhbrilliant/工资统计.csv";
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
String line = null;
bufferedReader.readLine();
while (true) {
line = bufferedReader.readLine();
if(line == null) {
break;
}
String[] split = line.split(",");
Employee e = new Employee();
e.id = Integer.parseInt(split[0]);
e.name = split[1];
e.gender = split[2];
e.job = split[3];
e.type = split[4];
// 空指针调用方法也会出现异常,故加了个前置判断 "判断其是否为 null " 后再调用 equals 进行判断
// 调用 equals 进行判断时,若存在常量,则将常量放前面。原因:https://docs.pingcode.com/ask/40556.html
e.baseSalary = (split[5] != null && !split[5].equals("") ? Double.parseDouble(split[5]) : null);
e.timeSalary = (split[6] != null && !split[6].equals("") ? Double.parseDouble(split[6]) : null);
e.bonus = (split[7] != null && !split[7].equals("") ? Double.parseDouble(split[7]) : null);
e.insurance = (split[8] != null && !split[8].equals("") ? Double.parseDouble(split[8]) : null);
e.days = Integer.parseInt(split[9]);
list.add(e);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 插入数据库
*/
public void save(){
new SalaryDAO().insert(list);
}
}
SalaryDao类:
package org.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
/**
* @author xhh
* @version 1.0
* @since 1.0
*/
public class SalaryDAO {
public void insert(List<Employee> list){
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/company", "root", "root");
connection.setAutoCommit(false);
statement = connection.createStatement();
for (Employee e : list) {
String sql = "INSERT INTO company.`_salary` (emp_id, total_salary) VALUES("+ e.id +","+ e.totalSalary + ")";
statement.addBatch(sql);
}
statement.executeBatch();
connection.commit();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
statement.close();
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
已经有了Mybaits以及Mybaits Plus等框架,JDBC的存在是否还有需要?
答:有,在开发大型系统进行百万级以上数据处理时,框架中的反射太过于耗时,此时应该用JDBC来处理->效率更高
JDBC的全称是Java数据库连接(Java Database connect),它是一套用于执行SQL语句的Java API
投票程序
候选人5人,姓名,编号,票数,
录入候选人信息,键盘录入姓名,按顺序自动产生编号
显示候选人信息
键盘录入候选人编号,给相应的后选人投一票,录入0投票结束
根据票数排序(可选)
显示最终结果
Main类:
package org.example;
public class Main{
public static void main(String[] args) {
VoteService voteService = new VoteService();
voteService.input();
voteService.show();
voteService.vote();
voteService.sort();
voteService.show();
}
}
Candidate类:
package org.example;
/**
* @author xhh
* @version 1.0
* @since 1.0
*/
public class Candidate {
int id;
String name;
int points;
}
VoteService类:
package org.example;
import java.util.Scanner;
/**
* @author xhh
* @version 1.0
* @since 1.0
*/
public class VoteService {
// 这里注意:当调用方法传入引用对象时,在方法中改变的若是引用对象中的值则无需返回,若重新指向一个新的引用,则需要返回
Candidate[] cs = new Candidate[5];
//输入候选人
public void input(){
// Scanner本身不是接收键盘录入的,而是处理流的。所以推荐不要 nextInt() 和 nextLine() 混着用,就用一个 nextLine() 即可
Scanner scanner = new Scanner(System.in);
for(int i = 0; i < 5; i++) {
System.out.println("请输入第" + (i + 1) + "位候选人姓名:");
cs[i] = new Candidate();
//输入候选人姓名
cs[i].name = scanner.nextLine();
//产生编号
cs[i].id = i + 1;
}
}
//开始投票
public void vote(){
Scanner scanner = new Scanner(System.in);
System.out.println("开始投票:");
while(true) {
String p = scanner.nextLine();
//输入0的时候结束投票
if("0".equals(p)) {
break;
}
int index = Integer.parseInt(p) - 1;
cs[index].points++;
}
}
//排序
public void sort(){
//根据票数排序
for(int i = 0; i < 4; i++) {
for(int j = i + 1; j < 5; j++) {
if(cs[i].points < cs[j].points) {
Candidate tmp = cs[i];
cs[i] = cs[j];
cs[j] = tmp;
}
}
}
}
public void show(){
System.out.println("编号\t姓名\t票数");
for(int i = 0; i < 5; i++) {
System.out.println(cs[i].id + "\t" + cs[i].name + "\t" + cs[i].points);
}
}
}
Kim:体现出 Java 面向对象思想,即尽量保证一个类只完成一个功能