import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class JDBC {
private static String url;
private static String password;
private static String user;
private static String driver;
static {
Properties pro = new Properties();
try {
pro.load(new FileReader("src\\comTan\\com\\BillingSystem\\Jdbc_until\\jdbc.properties"));
url = pro.getProperty("url");
password = pro.getProperty("password");
user = pro.getProperty("user");
driver = pro.getProperty("driver");
Class.forName(driver);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
public static void JDBC_close(Statement stme, Connection conn) {
if (stme != null) {
try {
stme.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/**
* @关闭
* @param rs
* @param stme
* @param conn
*/
public static void JDBC_close(ResultSet rs, Statement stme, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stme != null) {
try {
stme.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void All_short(String biao){
Connection conn = null;
ResultSet re = null;
Statement st = null;
int i = 0;
int id = 0;
int k = 0;
try {
int jj = 3;
for(int ii = 0;i<jj;ii++){
conn = getConnection();
String sql = "select * from "+biao+"";
st = conn.createStatement();
re = st.executeQuery(sql);
while(re.next()){
i++;
id = re.getInt("id");
String update = "update "+biao+" set id = "+i+" where id = '"+id+"';";
k=conn.createStatement().executeUpdate(update);
}
if(k>0){
System.out.println(("执行成功"));
}else{
System.out.println(("执行失败"));
}
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBC_close(re,st,conn);
}
}
public static int LenTable(String biao){
Connection conn = null;
ResultSet re = null;
Statement st = null;
int i = 0;
try {
conn = JDBC.getConnection();
String sql = "select *from "+biao+"";
st = conn.createStatement();
re = st.executeQuery(sql);
while(re.next()){
i++;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBC_close(re,st,conn);
}
return i;
}
public static int Len_table(String table){
Connection conn = null;
ResultSet re = null;
Statement st = null;
int i = 0;
try {
conn = JDBC.getConnection();
String sql = "select *from "+table+"";
st = conn.createStatement();
re = st.executeQuery(sql);
while(re.next()){
i++;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBC_close(re,st,conn);
}
return i;
}
}
import comTan.com.BillingSystem.Jdbc_until.JDBC;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
public class Add {
/**
* 写入到集合
* @param table
* @param getName
* @param list
*/
public void sql(String table, String getName, List<Integer> list){
Connection conn = null;
ResultSet re = null;
Statement st = null;
try {
int i = 0;
conn = comTan.com.BillingSystem.Jdbc_until.JDBC.getConnection();
String sql = "select *from " + table + "";
st = conn.createStatement();
re = st.executeQuery(sql);
while (re.next()) {
list.add(i,(re.getInt(getName)));
i++;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBC.JDBC_close(re,st,conn);
}
}
/**
*
* @param sql
* @return
*/
public int addSql(String sql){
int i = 0;
Connection conn = null;
Statement stmt = null;
try {
conn = JDBC.getConnection();
stmt = conn.createStatement();
i = stmt.executeUpdate(sql);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBC.JDBC_close(stmt, conn);
}
return i;
}
}
url = jdbc:mysql://...
user = root
password = root1234
driver = com.mysql.cj.jdbc.Driver//这是8.x的驱动
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Collective {
List<Person> people = new ArrayList<>();
Scanner sc = new Scanner(System.in);
List<Integer> PersonId = new ArrayList<>();
List<Integer> PersonIncome = new ArrayList<>();
List<Integer> PersonExpend = new ArrayList<>();
List<String> PersonDetail = new ArrayList<>();
List<Integer> PersonOverall = new ArrayList<>();
}
import comTan.com.billingSystem_new.Jdbc_until.Add;
import comTan.com.billingSystem_new.Jdbc_until.JDBC;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Delete {
public boolean sql(String table) {
Add add = new Add();
try {
Scanner sc = new Scanner(System.in);
System.out.print("哪一个:");
int id = sc.nextInt();
String sql = "delete from " + table + " where id = " + id + "";;
JDBC.All_short(table);
int i = add.addSql(sql);
return i == 1;
}catch (InputMismatchException e){
System.out.println("宝贝你是不是输出成中文或者英文或者特殊字符了。。。\n执行失败");
}
return false;
}
}
public class DisplayInterface {
public Init init = new Init();
/**
* 显示的时候有延迟时间
*/
private void time(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void start(){
System.out.println("---------------------欢迎进入家庭手指记账系统1.0.1---------------------");
time();
}
public void ent(){
System.out.println("菜单\n");
time();
System.out.println("收支明细");
System.out.println("登记支出");
System.out.println("登记收入");
System.out.println("退出(exit)");
System.out.println("删除数据");
}
//结束时
public void Welcome(){
System.out.println("欢迎下次光临");
}
public void inIt(String name){
init.name = name;
init.setPersonPersonIncome();
init.setBecause();
init.setPersonId();
init.setPeronExpend();
init.setPersonOverall();
}
//显示
public void show(){
System.out.println("\t\t目\t录\t\t\t\t余\t额\t\t\t\t支\t出\t\t\t\t收\t入\t\t\t\t收支\t明细");
int t = 0;
int p = 0;
for(int i = 0;i<init.p.PersonIncome.size();i++){
System.out.println("\t\t\t"+init.p.PersonId.get(i)+"\t\t\t\t"
+init.p.PersonOverall.get(i)+"元\t\t\t"
+init.p.PersonExpend.get(i)+"元\t\t\t\t\t"+
init.p.PersonIncome.get(i)+ "元\t\t\t\t"+
init.p.PersonDetail.get(i));
t = (t+init.p.PersonExpend.get(i));
p = (p+init.p.PersonIncome.get(i));
}
System.out.println("\t\t\t\t\t\t\t总支出为:" + Math.abs(t) + "元" + "\t\t总收入为:" + p + "元");
}
}
import comTan.com.BillingSystem.Jdbc_until.JDBC;
import comTan.com.billingSystem_new.Jdbc_until.Add;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Init {
Collective p = new Collective();
Add a = new Add();
public String name;
/**
* 初始Id;
*
*/
public void setPersonId(){ a.sql(name,"id",p.PersonId);
}
/**
* 初始收入
*/
public void setPersonPersonIncome(){
a.sql(name,"income",p.PersonIncome);
}
/**
* 初始支出
*/
public void setPeronExpend(){
a.sql(name,"expend",p.PersonExpend);
}
/**
* 初始余额
*/
public void setPersonOverall(){
int preNext = 0;
for (int i = 0; i < p.PersonId.size(); i++) {
preNext = preNext + p.PersonIncome.get(i) - p.PersonExpend.get(i);
p.PersonOverall.add(preNext);
}
}
/**
* 原因
*/
public void setBecause(){
Connection conn = null;
ResultSet re = null;
Statement st = null;
try {
int i = 0;
conn = JDBC.getConnection();
String sql = "select *from " + name + "";
st = conn.createStatement();
re = st.executeQuery(sql);
while (re.next()) {
String idd = re.getString("detail");
p.PersonDetail.add(idd);
i++;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBC.JDBC_close(re, st, conn);
}
}
/**
* 清除集合
*/
public void clear(){
p.PersonOverall.clear();
p.PersonId.clear();
p.PersonDetail.clear();
p.PersonIncome.clear();
p.PersonExpend.clear();
}
}
import comTan.com.BillingSystem.Jdbc_until.JDBC;
import comTan.com.billingSystem_new.Jdbc_until.Add;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Insert {
Add add = new Add();
Scanner sc = new Scanner(System.in);
public void updateAdd(String table) {
try {
System.out.print("多少钱:");
int addMany = sc.nextInt();
System.out.print("为什么:");
String detail = sc.next();
int id = JDBC.Len_table(table) + 1;
String sql = " insert into " + table + "(id, expend,detail) values(" + id + "," + addMany + ",'" + detail + "');";
add.addSql(sql);
}catch (InputMismatchException e){
System.out.println("宝贝你是不是输出成中文或者英文或者特殊字符了。。。\n执行失败");
}
}
/**
* 写入收入以及原因
*
* @param table
*/
public void updateEnd(String table) {
try{
System.out.print("多少钱:");
int income = sc.nextInt();
System.out.print("为什么:");
String detil = sc.next();
int id = JDBC.Len_table(table) + 1;
String sql = " insert into " + table + "(id,income,detail) values(" + id + "," + income + ",'" + detil + "');";
add.addSql(sql);
}catch (InputMismatchException e){
System.out.println("宝贝你是不是输出成中文或者英文或者特殊字符了。。。\n执行失败");
}
}
}
import comTan.com.BillingSystem.Jdbc_until.JDBC;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Login {
public boolean login(String name ,String password){
if (name == null || password == null) {
return false;
}
Connection conn = null;
PreparedStatement pstemt = null;
ResultSet r = null;
try {
String sql = "select*from user where admin =? and password = ?";
conn = JDBC.getConnection();
pstemt = conn.prepareStatement(sql);
pstemt.setString(1, name);
pstemt.setString(2, password);
r = pstemt.executeQuery();
return r.next();
} catch (
SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBC.JDBC_close(r, pstemt, conn);
}
return false;
}
}
import java.util.InputMismatchException;
public class Main {
public void start(){
DisplayInterface display = new DisplayInterface();
System.out.print("请问你注册还是登录:");
int pen = display.init.p.sc.nextInt();
if(pen==1){
System.out.print("账号:");
String name = display.init.p.sc.next();
System.out.print("密码:");
String password = display.init.p.sc.next();
boolean login = new Login().login(name,password);
if(login){
display.start();
display.ent();
while (new Quit().flags){
int pan = 0;
try {
System.out.print("什么操作:");
pan = display.init.p.sc.nextInt();
}catch (InputMismatchException e){
System.out.println("输入中文了");
return;
}
display.init.clear();
display.inIt(name);
switch (pan){
case 1:
display.show();
break;
case 2:
new Insert().updateAdd(name);
break;
case 3:
new Insert().updateEnd(name);
break;
case 5:
System.out.println("你确定退出吗(yes/no)?");
String num = display.init.p.sc.next();
if (num.equals("yes") || num.equals("Yes") || num.equals("Y") || num.equals("y")) {
System.out.println("bye");
display.Welcome();
return;
}
break;
case 4:
new Delete().sql(name);
break;
default:
System.out.println("看不懂你说的啥");
break;
}
}
}else {
System.out.println("账号或密码错误");
return;
}
}else if(pen==2){
System.out.print("请输入账号:");
String name = display.init.p.sc.next();
boolean pan = new Setup().setup(name);
if(pan){
display.start();
display.ent();
boolean flags = true;
while (new Quit().flags){
display.init.clear();
display.inIt(name);
System.out.print("什么操作:");
int pann = display.init.p.sc.nextInt();
switch (pann){
case 1:
display.show();
break;
case 2:
new Insert().updateAdd(name);
break;
case 3:
new Insert().updateEnd(name);
break;
case 5:
System.out.println("你确定退出吗(yes/no)?");
String num = display.init.p.sc.next();
if (num.equals("yes") || num.equals("Yes") || num.equals("Y") || num.equals("y")) {
System.out.println("bye");
display.Welcome();
return;
}
break;
case 4:
new Delete().sql(name);
break;
default:
System.out.println("看不懂你说的啥");
break;
}
}
}else {
System.out.println("此账号已经注册过了");
}
}else {
System.out.println("啥也不是buy");
}
}
}
import comTan.com.BillingSystem.BillingSystem_until.abstractClass.BillingSystemInterface;
import java.util.Objects;
public class Person implements BillingSystemInterface {
private int id;
private String name;
private String password;
public Person(String admin, String password) {
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return id == person.id &&
Objects.equals(name, person.name) &&
Objects.equals(password, person.password);
}
@Override
public int hashCode() {
return Objects.hash(id, name, password);
}
}
public class Quit {
public boolean flags = true;
public void exit(){
DisplayInterface display = new DisplayInterface();
System.out.println("你确定退出吗(yes/no)?");
String num = display.init.p.sc.next();
if (num.equals("yes") || num.equals("Yes") || num.equals("Y") || num.equals("y")) {
System.out.println("bye");
display.Welcome();
flags = false;
}
}
}
import comTan.com.BillingSystem.Jdbc_until.JDBC;
import comTan.com.billingSystem_new.Jdbc_until.Add;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Setup {
Init init = new Init();
public boolean setup(String admin) {
boolean isname = true;
Statement stmt = null;
Connection conn = null;
ResultSet r = null;
int name = JDBC.Len_table("user") + 1;
try {
conn = JDBC.getConnection();
String sql = "select*from user";
stmt = conn.createStatement();
r = stmt.executeQuery(sql);
String[] username = new String[name];
int i = 0;
while (r.next()) {
i++;
String hh = r.getString("admin");
username[i] = hh;
}
for (int j = 0; j < username.length; j++) {
if (admin.equals(username[i])) {
isname = false;
}
}
if (isname) {
System.out.print("请输入密码:");
String password = init.p.sc.next();
Person person = new Person(admin,password);
init.p.people.add(person);
int id = JDBC.Len_table("user") + 1;
String Sql = "insert into user(id,admin,password) values(" + id + ",'" + admin + "','" + password + "');";
stmt.executeUpdate(Sql);
sql(admin);
JDBC.JDBC_close(r, stmt, conn);
return true;
} else {
JDBC.JDBC_close(r, stmt, conn);
return false;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBC.JDBC_close(r, stmt, conn);
}
return true;
}
private boolean sql(String table) {
Add add = new Add();
String sql = "create table " + table + "( id int, overall int DEFAULT 0,expend int DEFAULT 0, income int DEFAULT 0,detail varchar(1000),time timestamp DEFAULT NOW()); ";
int i = add.addSql(sql);
return i==0;
}
}
public class Test {
public static void main(String[] args) {
new Main().start();
}
}