本系统需要使用MySQL的java驱动包,这个包可以在网站下载,具体安装方法不同的编译器不同,这个需要另行进行搜索,下面是具体的代码实现:
package cn.com.Universty;
import java.sql.*;
public class MySQLConnection {
public static final String DRIVER = "com.mysql.cj.jdbc.Driver";//设置driver
public static final String URL = "jdbc:mysql://localhost:3306/university";//设置连接端口和数据库名
public static final String USER = "root";//设置数据库的用户名
public static final String PASSWORD = "123456789";//设置数据库的密码
public static Connection getConnection(){//设置连接函数
Connection connection = null;
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(URL,USER,PASSWORD);//建立连接
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection conn) throws SQLException {
if (conn != null){
conn.close();
}
}
public static void close(PreparedStatement pstmt) throws SQLException {
if (pstmt != null){
pstmt.close();
}
}
public static void close(Statement stmt) throws SQLException {
if (stmt != null){
stmt.close();
}
}
public static void close(ResultSet rs) throws SQLException {
if (rs != null){
rs.close();
}
}
}
package cn.com.Universty;
import java.io.Serializable;
import java.util.Objects;
public class Student implements Serializable {//toString这个方法很重要
private long snum;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return snum == student.snum &&
sage == student.sage &&
Double.compare(student.select, select) == 0 &&
Double.compare(student.swater, swater) == 0 &&
sname.equals(student.sname) &&
ssex.equals(student.ssex) &&
sclass.equals(student.sclass) &&
secost.equals(student.secost) &&
swcost.equals(student.swcost);
}
@Override
public int hashCode() {
return Objects.hash(snum, sname, ssex, sage, sclass, select, swater, secost, swcost);
}
private String sname;
private String ssex;
@Override
public String toString() {
return "Student{" +
"snum=" + snum +
", sname='" + sname + '\'' +
", ssex='" + ssex + '\'' +
", sage=" + sage +
", sclass='" + sclass + '\'' +
", select=" + select +
", swater=" + swater +
", secost='" + secost + '\'' +
", swcost='" + swcost + '\'' +
'}';
}
private int sage;
private String sclass;
private double select;
private double swater;
private String secost;
private String swcost;
public long getSnum() {
return snum;
}
public void setSnum(long snum) {
this.snum = snum;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getSclass() {
return sclass;
}
public void setSclass(String sclass) {
this.sclass = sclass;
}
public double getSelect() {
return select;
}
public void setSelect(double select) {
this.select = select;
}
public double getSwater() {
return swater;
}
public void setSwater(double swater) {
this.swater = swater;
}
public String getSecost() {
return secost;
}
public void setSecost(String secost) {
this.secost = secost;
}
public String getSwcost() {
return swcost;
}
public void setSwcost(String swcost) {
this.swcost = swcost;
}
}
package cn.com.Universty;
import java.sql.*;
import java.util.ArrayList;
public class MySQLUse {
public static String MadeString(Student student){//原始的建立MySQL的语句的方式
String sql = "insert into Student values("+student.getSnum()+",\'"+student.getSname()+"\',\'"+student.getSsex()+"\',"+student.getSage()+",\'"+student.getSclass()+"\',"+student.getSelect()+","+student.getSwater()+",\'"+student.getSecost()+"\',\'"+student.getSwcost()+"\')";
return sql;
}
public static int Add(Student student){//向数据库中添加数据的方法
int a=0;
Connection connection;
connection = MySQLConnection.getConnection();//连接数据库
try {
if (connection.isClosed())
System.out.println("增加数据时数据库连接失败!!!");
Statement statement = connection.createStatement();
String sql = MadeString(student);
//System.out.println(sql);
if (statement.executeUpdate(sql)!=0)//判断数据库的语言是否已经执行
{System.out.println("插入成功!");a=1;}
else
{ System.out.println("插入失败!");a=0;}
MySQLConnection.close(statement);
MySQLConnection.close(connection);
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
public static int Delete(long student){//以学号为媒介删除;
int a=0;
Connection connection;
connection = MySQLConnection.getConnection();
try {
if (connection.isClosed()){
System.out.println("删除数据时数据库连接失败!!!");
}
Statement statement = connection.createStatement();
String sql = "delete from Student where snum="+student;
if (statement.executeUpdate(sql)!=0){
System.out.println("删除成功!");a=1;
}
else
{
System.out.println("删除失败!");a=0;
}
MySQLConnection.close(statement);
MySQLConnection.close(connection);
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
public static int Delete(String string){//以名字为媒介删除;
int a=0;
Connection connection;
connection = MySQLConnection.getConnection();
try {
if (connection.isClosed()){
System.out.println("删除数据时数据库连接失败!!!");
}
Statement statement = connection.createStatement();
String sql = "delete from Student where sname='%s'";
//PreparedStatement ps = connection.prepareStatement(sql);
//ps.setString(1,string);
//System.out.println(ps.getResultSet());
//String str = ps.toString();
//System.out.println(ps.toString());
//ResultSet rs = ps.executeQuery();
if (statement.executeUpdate(String.format(sql,string))!=0){
System.out.println("删除成功!");a=1;
}
else
{
System.out.println("删除失败!");a=0;
}
MySQLConnection.close(statement);
MySQLConnection.close(connection);
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
public static Student Select(long id){//以学号为媒介进行查看数据
Student student = new Student();
Connection connection;
connection = MySQLConnection.getConnection();
try {
if (connection.isClosed()){
System.out.println("查询数据库内容时连接数据库失败!!!");
}
Statement statement = connection.createStatement();
String sql = "select * from Student where snum=?";//采用PreparedStatement进行占位
PreparedStatement ps = connection.prepareStatement(sql);
ps.setLong(1,id);
ResultSet rs = ps.executeQuery();
while (rs.next()){
Student stu = new Student();
stu.setSnum(rs.getLong("snum"));
//System.out.print(stu.getSnum()+"\t");
stu.setSname(rs.getString("sname"));
//System.out.print(stu.getSname()+"\t");
stu.setSsex(rs.getString("ssex"));
//System.out.print(stu.getSsex()+"\t");
stu.setSage(rs.getInt("sage"));
//System.out.print(stu.getSage()+"\t");
stu.setSclass(rs.getString("sclass"));
//System.out.print(stu.getSclass()+"\t");
stu.setSelect(rs.getDouble("selec"));
//System.out.print(stu.getSelect()+"\t");
stu.setSwater(rs.getDouble("swater"));
//System.out.print(stu.getSwater()+"\t");
stu.setSecost(rs.getString("secost"));
//System.out.print(stu.getSecost()+"\t");
stu.setSwcost(rs.getString("swcost"));
//System.out.print(stu.getSwcost());
student=stu;
}
MySQLConnection.close(rs);
MySQLConnection.close(ps);
MySQLConnection.close(statement);
MySQLConnection.close(connection);
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
public static ArrayList<Student> Select(){//以列表的方式进行提取所有数据库中的信息
ArrayList<Student> students = new ArrayList<>();
Connection connection;
connection = MySQLConnection.getConnection();
try {
if (connection.isClosed()){
System.out.println("查寻数据库时连接数据库失败!!!");
}
Statement statement = connection.createStatement();
String sql = "select * from Student ";
ResultSet rs = statement.executeQuery(sql);
while (rs.next()){
Student student = new Student();
student.setSnum(rs.getLong("snum"));
student.setSname(rs.getString("sname"));
student.setSsex(rs.getString("ssex"));
student.setSclass(rs.getString("sclass"));
student.setSage(rs.getInt("sage"));
student.setSelect(rs.getDouble("selec"));
student.setSwater(rs.getDouble("swater"));
student.setSecost(rs.getString("secost"));
student.setSwcost(rs.getString("swcost"));
students.add(student);
}
MySQLConnection.close(rs);
MySQLConnection.close(statement);
MySQLConnection.close(connection);
} catch (SQLException e) {
e.printStackTrace();
}
//System.out.println(students);
return students;
}
public static int Update(long id,Student student) {//更新数据库中的数据,采用的是以学号为媒介先删除原有的再添加修改后的
int a = 0,b = 0,c = 0;
b = Delete(id);
if (b == 1 ){
a = 1;
c = Add(student);
if ( c ==0) {
a = 0;
System.out.println("修改学生信息时增加更新信息失败! !");
}
}else System.out.println("删除信息失败");
return a;
}
}
package cn.com.Universty;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentControl {
public static void StudentSet(ArrayList<Student> list){//向数据库中添加数据
Student student = new Student();
Scanner scanner = new Scanner(System.in);
int i=0;
// Boolean bool = false;
System.out.println("=====================欢迎来到学生信息添加系统====================");
System.out.print("请输入学生的学号:");
student.setSnum(scanner.nextLong());
for (i=0;i<list.size();i++){
if (list.get(i).getSnum()==student.getSnum()){//判断键入的学号原有数据库中是否已存在
//bool=true;
System.out.print("学号已存在,请重新输入:");
student.setSnum(scanner.nextLong());
//i=0;
// continue;
}
}
//if ()
System.out.print("请输入学生的姓名:");
student.setSname(scanner.next());
do {
System.out.print("请输入学生的性别:");
student.setSsex(scanner.next());
}while (!student.getSsex().equals("男")&&!student.getSsex().equals("女"));
System.out.print("请输入学生的年龄: ");
student.setSage(scanner.nextInt());
System.out.print("请输入学生的班级: ");
student.setSclass(scanner.next());
System.out.print("请输入学生的用电量(度):");
student.setSelect(scanner.nextDouble());
do {
System.out.print("该学生是否已经缴纳电费(填“是”或“否”):");
student.setSecost(scanner.next());
}while (!student.getSecost().equals("是")&&!student.getSecost().equals("否"));
System.out.print("请输入学生的用水量(吨):");
student.setSwater(scanner.nextDouble());
do {
System.out.print("请输入学生是否缴纳水费:");
student.setSwcost(scanner.next());
}while (!student.getSwcost().equals("是")&&!student.getSwcost().equals("否"));
MySQLUse.Add(student);
}
public static void StudentGetAll(ArrayList<Student> list){//输出所有的数据库中的信息
int i=0;
System.out.println("学生信息如下:");
System.out.print("学号\t");
System.out.print("姓名\t");
System.out.print("性别\t");
System.out.print("年龄\t");
System.out.print("班级\t");
System.out.print("用电量(度)\t");
System.out.print("电费缴纳情况\t");
System.out.print("用水量(吨)\t");
System.out.print("水费缴纳情况\t");
System.out.println();
for (i=0;i<list.size();i++){
System.out.print(list.get(i).getSnum());System.out.print("\t");
System.out.print(list.get(i).getSname());System.out.print("\t");
System.out.print(list.get(i).getSsex());System.out.print("\t");
System.out.print(list.get(i).getSage());System.out.print("\t");
System.out.print(list.get(i).getSclass());System.out.print("\t");
System.out.print(list.get(i).getSelect());System.out.print("\t");
System.out.print(list.get(i).getSecost());System.out.print("\t");
System.out.print(list.get(i).getSwater());System.out.print("\t");
System.out.print(list.get(i).getSwcost());System.out.print("\t");
System.out.println();
}
}
public static void StudentGetOne(Long num){//以学号为媒介进行查找学生信息
Student student = new Student();
student = MySQLUse.Select(num);
if (student==null) {
System.out.println("查无此人!!");
}
else {
System.out.println("学生信息如下:");
System.out.print("学号\t");
System.out.print("姓名\t");
System.out.print("性别\t");
System.out.print("年龄\t");
System.out.print("班级\t");
System.out.print("用电量(度)\t");
System.out.print("电费缴纳情况\t");
System.out.print("用水量(吨)\t");
System.out.print("水费缴纳情况\t");
System.out.println();
System.out.print(student.getSnum());
System.out.print("\t");
System.out.print(student.getSname());
System.out.print("\t");
System.out.print(student.getSsex());
System.out.print("\t");
System.out.print(student.getSage());
System.out.print("\t");
System.out.print(student.getSclass());
System.out.print("\t");
System.out.print(student.getSelect());
System.out.print("\t");
System.out.print(student.getSecost());
System.out.print("\t");
System.out.print(student.getSwater());
System.out.print("\t");
System.out.print(student.getSwcost());
System.out.print("\t");
System.out.println();
}
}
public static void StudentGetOne(String sname){//以姓名为媒介进行查找数据库中的信息
ArrayList<Student> list = new ArrayList<>();
list = MySQLUse.Select();
int i = 0;
System.out.print("学号\t");
System.out.print("姓名\t");
System.out.print("性别\t");
System.out.print("年龄\t");
System.out.print("班级\t");
System.out.print("用电量(度)\t");
System.out.print("电费缴纳情况\t");
System.out.print("用水量(吨)\t");
System.out.print("水费缴纳情况\t");
System.out.println();
for (i=0;i<list.size();i++){
if (list.get(i).getSname().equals(sname)){
System.out.print(list.get(i).getSnum());System.out.print("\t");
System.out.print(list.get(i).getSname());System.out.print("\t");
System.out.print(list.get(i).getSsex());System.out.print("\t");
System.out.print(list.get(i).getSage());System.out.print("\t");
System.out.print(list.get(i).getSclass());System.out.print("\t");
System.out.print(list.get(i).getSelect());System.out.print("\t");
System.out.print(list.get(i).getSecost());System.out.print("\t");
System.out.print(list.get(i).getSwater());System.out.print("\t");
System.out.print(list.get(i).getSwcost());System.out.print("\t");
System.out.println();
}
}
}
public static void StudentSesrch(){//查询系统
Scanner scanner = new Scanner(System.in);int choice;
while (true) {
System.out.println("*************欢迎来到查询系统****************");
System.out.println("**************1.学号查询********************");
System.out.println("**************2.姓名查询********************");
System.out.println("**************3.用电量查询******************");
System.out.println("**************4.用水量查询******************");
System.out.println("**************0.退出查询系统****************");
System.out.print("请输入要执行的操作指令代码(1、2、3、4、0): ");
choice = scanner.nextInt();
while (true) {
if (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 0) {
System.out.print("输入指令错误,请重新输入:");
choice = scanner.nextInt();
//continue;
} else
break;
}
if (choice == 0){
break;
}
else if (choice == 1){
long snum;
System.out.print("请输入要查询的学生的学号:");
snum = scanner.nextLong();
StudentGetOne(snum);
}
else if (choice == 2){
String sname;
System.out.print("请输入要查询的学生的姓名:");
sname = scanner.next();
StudentGetOne(sname);
}
else if (choice == 3){
System.out.print("请输入学生的学号:");
long snum;
snum = scanner.nextLong();
Student student = new Student();
student = MySQLUse.Select(snum);
System.out.println(student.getSname()+"的用电量为:"+student.getSelect());
if (student.getSecost().equals("否")){
System.out.println("还未交电费,请及时缴纳!");
}else
System.out.println("已缴纳电费。");
}
else if (choice == 4){
System.out.print("请输入要查询学生的学号:");
long sum;
sum = scanner.nextLong();
Student student = new Student();
student = MySQLUse.Select(sum);
System.out.println(student.getSname()+"的用水量为:"+student.getSwater());
if (student.getSwcost().equals("否")){
System.out.println("还未交水费,请及时缴纳!");
}else
System.out.println("水费已缴纳。");
}
}
}
public static void StudentChange(){//修改信息
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的学生的学号: ");
long snum;
snum = scanner.nextLong();
Student student = new Student();
System.out.print("输入修改后的学号:");
student.setSnum(scanner.nextLong());
System.out.print("输入修改后的姓名:");
student.setSname(scanner.next());
System.out.print("输入修改后的性别:");
student.setSsex(scanner.next());
System.out.print("输入修改后的年龄:");
student.setSage(scanner.nextInt());
System.out.print("输入修改后的班级:");
student.setSclass(scanner.next());
System.out.print("输入修改后的用电量(度):");
student.setSelect(scanner.nextDouble());
System.out.print("输入修改后的电费缴纳情况: ");
do {
student.setSecost(scanner.next());
}while (!student.getSecost().equals("是")&&!student.getSecost().equals("否"));
System.out.print("输入修改后的用水量(吨): ");
student.setSwater(scanner.nextDouble());
System.out.print("输入修改后的水费缴纳情况:");
do {
student.setSwcost(scanner.next());
}while (!student.getSwcost().equals("是")&&!student.getSwcost().equals("否"));
int a = 0;
a = MySQLUse.Update(snum,student);
if (a==1){
System.out.println("修改成功!");
}else
System.out.println("修改失败!");
}
public static void StudentDelete(){//删除学生信息
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除人的学号:");
long snum;
snum = scanner.nextLong();
int a =0;
a = MySQLUse.Delete(snum);
if (a==1){
System.out.println("删除成功!");
}
else
System.out.println("删除失败!");
}
public static void StudentCount() {//信息统计
Scanner scanner = new Scanner(System.in);
int choice;
double useelec=0,arrangeuseelec=0,usewater=0,arrangeusewater=0;//依次是电量使用、平均电量、用水量、平均水量
ArrayList<Student> list = new ArrayList<>();
list = MySQLUse.Select();
while (true) {
System.out.println("********************欢迎来到统计系统******************");
System.out.println("********************1.统计用电量*********************");
System.out.println("********************2.统计用水量*********************");
System.out.println("********************3.统计电费***********************");
System.out.println("********************4.统计水费***********************");
System.out.println("********************0.退出统计系统*******************");
System.out.print("请输入你要执行的操作:");
choice = scanner.nextInt();
while (true) {
if (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 0) {
System.out.print("输入指令错误,请重新输入:");
choice = scanner.nextInt();
//continue;
} else break;
}
if (choice == 0) {
break;
}
else if (choice == 1){
int i = 0,upeople=0,downpeople=0,equalpeople=0;
for (i=0;i<list.size();i++){
useelec+=list.get(i).getSelect();
//i++;
}
arrangeuseelec=useelec/i;
//i=0;
for (i=0;i<list.size();i++){
if (list.get(i).getSelect()>arrangeuseelec){
upeople++;
}
else if (list.get(i).getSelect()==arrangeuseelec){
equalpeople++;
}
else if (list.get(i).getSelect()<arrangeuseelec){
downpeople++;
}
//i++;
}
System.out.println("用电量情况统计如下:");
System.out.println("------------------------------");
System.out.println("| 总用电量 |"+useelec+"度");
System.out.println("| 平均用电量 |"+arrangeuseelec+"度");
System.out.println("| 超过平均用电量的人数 |"+upeople+"人");
System.out.println("| 等于平均用电量的人数 |"+equalpeople+"人");
System.out.println("| 低于平均用电量的人数 |"+downpeople+"人");
System.out.println("------------------------------");
}
else if (choice == 2){
int i = 0,upeople=0,downpeople=0,equalpeople=0;
for (i=0;i<list.size();i++){
usewater+=list.get(i).getSwater();
//i++;
}
arrangeusewater=usewater/i;
// i=0;
for (i=0;i<list.size();i++){
if (list.get(i).getSwater()>arrangeusewater){
upeople++;
}
else if (list.get(i).getSwater()==arrangeusewater){
equalpeople++;
}
else if (list.get(i).getSwater()<arrangeusewater){
downpeople++;
}
//i++;
}
System.out.println("用水量情况统计如下:");
System.out.println("------------------------------");
System.out.println("| 总用水量 |"+useelec+"吨");
System.out.println("| 平均用水量 |"+arrangeuseelec+"吨");
System.out.println("| 超过平均用水量的人数 |"+upeople+"人");
System.out.println("| 等于平均用水量的人数 |"+equalpeople+"人");
System.out.println("| 低于平均用水量的人数 |"+downpeople+"人");
System.out.println("------------------------------");
}
else if (choice == 3){
double price;
double free;
double cost=0;
int sum1=0,sum2=0,i=0;//sum1为已缴纳人数,sum2为未缴纳人数
System.out.println("请输入电费价格(度/元): ");
price = scanner.nextDouble();
System.out.println("请输入免费额度(度): ");
free = scanner.nextDouble();
for (i=0;i<list.size();i++){
if (list.get(i).getSelect()<=free){
cost+=0;
sum1++;
}else if (list.get(i).getSelect()>free){
cost += (list.get(i).getSelect()-free)*price;
if (list.get(i).getSecost().equals("是")){
sum1++;
}else sum2++;
}
//i++;
}
System.out.println("已缴纳人数为: "+sum1);
System.out.println("未缴纳人数为:"+sum2);
}
else if (choice==4){
double price;
double free;
double cost=0;
int sum1=0,sum2=0,i=0;//sum1为已缴纳人数,sum2为未缴纳人数
System.out.println("请输入水费价格(吨/元): ");
price = scanner.nextDouble();
System.out.println("请输入免费额度(吨): ");
free = scanner.nextDouble();
for (i=0;i<list.size();i++){
if (list.get(i).getSwater()<=free){
cost+=0;
sum1++;
}else if (list.get(i).getSwater()>free){
cost += (list.get(i).getSwater()-free)*price;
if (list.get(i).getSwcost().equals("是")){
sum1++;
}else sum2++;
}
// i++;
}
System.out.println("已缴纳人数为: "+sum1);
System.out.println("未缴纳人数为:"+sum2);
}
}
}
public static void MadeStudent(){//学生信息操作总界面
Scanner scanner = new Scanner(System.in);
int choice ;
ArrayList<Student> list = new ArrayList<>();
list=null;
while (true) {
System.out.println("******************学生水电费管理系统************************");
System.out.println("******************1.学生信息录入***************************");
System.out.println("******************2.全部学生信息查询************************");
System.out.println("******************3.单个学生信息查询************************");
System.out.println("******************4.学生信息删除系统************************");
System.out.println("******************5.学生信息修改系统************************");
System.out.println("******************6.学生信息统计系统*************************");
System.out.println("******************0.退出管理系统*****************************");
System.out.print("请输入操作代码:");
choice = scanner.nextInt();
while (true) {
if (choice != 1 && choice != 2 && choice != 3 && choice != 4 &&choice != 5 && choice != 6 && choice != 0) {
System.out.print("输入指令错误,请重新输入:");
choice = scanner.nextInt();
//continue;
} else
break;
}
if (choice == 0){
break;
}else if (choice ==1 ){
list=MySQLUse.Select();
StudentSet(list);
}
else if (choice == 2){
list = MySQLUse.Select();
StudentGetAll(list);
}else if (choice == 3){
StudentSesrch();
}else if (choice == 4){
StudentDelete();
}else if (choice == 5){
StudentChange();
}else if (choice == 6){
StudentCount();
}
}
}
}
package cn.com.Universty;
public class StudentUse {
public static void main(String[] args) {//执行语句
StudentControl.MadeStudent();
}
}