(部分代码) import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class property_expenses { private int number; private int PE; private int HE; private int all; public property_expenses(int i) { try { getall_p(i); } catch (Exception e) { System.out.println(e); } } public property_expenses() { } public int getAll() { return all; } public void setAll(int all) { this.all = all; } public property_expenses(int number, int PE, int HE) { this.number = number; this.PE = PE; this.HE = HE; try { jdbc_PE(); } catch (Exception e) { e.printStackTrace(); } } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public int getPE() { return PE; } public void setPE(int PE) { this.PE = PE; } public int getHE() { return HE; } public void setHE(int HE) { this.HE = HE; } public void jdbc_PE() throws Exception { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = "*****"; Connection conn = DriverManager.getConnection(url, username, password); //写sql语句 String sql = "insert into property_expenses (number,PE,HE)values (?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, number); pstmt.setInt(2, PE); pstmt.setInt(3, HE); pstmt.execute(); //释放资源 pstmt.close(); conn.close(); } public void getall_p(int i) throws Exception { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = "*****"; Connection conn = DriverManager.getConnection(url, username, password); String sql; //写sql语句 if (i == 0) { sql = "select SUM(PE) from property_expenses;"; } else { sql = "select SUM(HE) from property_expenses;"; } PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet resultSet = pstmt.executeQuery(); if (resultSet.next()) { int anInt = resultSet.getInt(1); this.all = anInt; } //释放资源 pstmt.close(); conn.close(); } public int get_sbp(int i,int id) throws Exception { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = "*****"; Connection conn = DriverManager.getConnection(url, username, password); String sql; //写sql语句 if (i == 1) { sql = "select PE from property_expenses where number =(select number from users where id = ?)"; } else { sql = "select HE from property_expenses where number =(select number from users where id = ?)"; } PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); ResultSet resultSet = pstmt.executeQuery(); int anInt = 0; if (resultSet.next()) { anInt = resultSet.getInt(1); } //释放资源 pstmt.close(); conn.close(); return anInt; } public void get_add(int i,int id) throws Exception { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password ="*****"; Connection conn = DriverManager.getConnection(url, username, password); String sql; //写sql语句 if (i == 1) { sql = "update property_expenses\n" + "set PE = (PE + 400)\n" + "where number = (select number from users where id = ?)"; } else { sql = "update property_expenses\n" + "set HE = (HE + 400)\n" + "where number = (select number from users where id = ?);"; } PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.execute(); //释放资源 pstmt.close(); conn.close(); } public Boolean get_boolean(int id) throws Exception { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = "*****"; Connection conn = DriverManager.getConnection(url, username, password); String sql; //写sql语句 sql = "select * from property_expenses where number =(select number from users where id = ?) "; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); ResultSet resultSet = pstmt.executeQuery(); if (resultSet.next()&&resultSet.getInt(2)!=0&&resultSet.getInt(3) != 0) { pstmt.close(); conn.close(); return true; } //释放资源 pstmt.close(); conn.close(); return false; } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; public class property { private int id; private String name; private Date data; private String passWord; public String getPassword() { return passWord; } public void setPassword(String password) { this.passWord = password; } public property(int id, String name, String data,String passWord) { this.id = id; this.name = name; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); ParsePosition pos = new ParsePosition(0); this.data = formatter.parse(data, pos); this.passWord=passWord; try { jdbc_property(); System.out.println("yes"); } catch (Exception e) { e.printStackTrace(); } } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getData() { return data; } public void setData(String data) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); ParsePosition pos = new ParsePosition(0); this.data = formatter.parse(data, pos); } public void jdbc_property() throws Exception{ //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = "*****"; Connection conn = DriverManager.getConnection(url, username, password); //写sql语句 String sql = "insert into property (id, name, Entry_time,passWord) VALUE (?,?,?,?)"; PreparedStatement pstmt= conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.setString(2, name); java.sql.Date dayDateSql = new java.sql.Date(this.data.getTime()); pstmt.setDate(3, dayDateSql); pstmt.setString(4, passWord); pstmt.execute(); //释放资源 pstmt.close(); conn.close(); } }
package nuc.Community_Management.datebaseClass; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Random; public class users { private int id; private String name; private String sex; private int Room_number; private int select_remove; public users() { } private String select; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getRoom_number() { return Room_number; } public void setRoom_number(int room_number) { this.Room_number = room_number; } public users(int id, String name, String sex, int room_number, String select, int select_remove) { this.id = id; this.name = name; this.sex = sex; this.Room_number = room_number; this.select_remove = select_remove; int sexx = 0; if (this.sex.equals("MAN")) { sexx = 1; } this.select = select; try { int i = Integer.parseInt(select); try { jdbc_select(i, sexx, select_remove); System.out.println("1"); } catch (Exception e) { e.printStackTrace(); System.out.println("2"); } } catch (Exception e) { try { jdbc_select(select, sexx, select_remove); System.out.println("3"); } catch (Exception e2) { e.printStackTrace(); System.out.println("4"); } } } public users(int id, String name, String sex, int room_number) { this.id = id; this.name = name; this.sex = sex; this.Room_number = room_number; int sexx = 0; if (this.sex.equals("MAN")) { sexx = 1; } try { jdbc_users(this.id, this.name, sexx, this.Room_number); } catch (Exception e) { e.printStackTrace(); } } public void jdbc_users(int id, String name, int sex, int room) throws Exception { Random r = new Random(); double v = r.nextDouble(1500); //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = "*****"; Connection conn = DriverManager.getConnection(url, username, password); //写sql语句 String sql = "insert into users (id, name, sex, number,elecE)values (?,?,?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.setString(2, name); pstmt.setInt(3, sex); pstmt.setInt(4, room); pstmt.setDouble(5,v); wat_ele we = new wat_ele(room,0,0); property_expenses pe = new property_expenses(room,0,0); pstmt.execute(); //释放资源 pstmt.close(); conn.close(); } public void jdbc_select(int select, int sexx, int select_remove) throws Exception { Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = ""*****"; Connection conn = DriverManager.getConnection(url, username, password); //写sql语句 String sql = "select * from users where id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, select); ResultSet resultSet = pstmt.executeQuery(); if (resultSet.next()) { if (select_remove == 1) { String sql_update = "update users set id = ?,name = ?,sex = ?,number = ? where id = ?"; PreparedStatement pstmt_update = conn.prepareStatement(sql_update); pstmt_update.setInt(1, id); pstmt_update.setString(2, name); pstmt_update.setInt(3, sexx); pstmt_update.setInt(4, Room_number); pstmt_update.setInt(5, select); pstmt_update.execute(); System.out.println("5"); } else if(select_remove == 0) { String sql_remove = "delete from users where id =?"; PreparedStatement pstmt_remove = conn.prepareStatement(sql_remove); pstmt_remove.setInt(1,select); pstmt_remove.execute(); System.out.println("6"); } else if (select_remove == 2) { int id = resultSet.getInt(1); String name = resultSet.getString(2); int sex = resultSet.getInt(3); int number = resultSet.getInt(4); System.out.println(id+" "+name+" "+sex+" "+number); this.id = id; this.name = name; if (sex == 1) { this.sex = "MAN"; } else { this.sex = "WOMAN"; } this.Room_number = number; System.out.println(this.id + " "+this.name + " "+this.sex + " "+this.Room_number); } } else { System.out.println("wrong");//报错 } //释放资源 pstmt.close(); conn.close(); } public void jdbc_select(String name, int sexx, int select_remove) throws Exception { this.name = name; Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = "*****"; Connection conn = DriverManager.getConnection(url, username, password); //写sql语句 String sql = "select * from users where name = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, name); ResultSet resultSet = pstmt.executeQuery(); if (resultSet.next()) { if(select_remove == 1) { String sql_update = "update users set id = ?,name = ?,sex = ?,number = ? where name = ?"; PreparedStatement pstmt_update = conn.prepareStatement(sql_update); pstmt_update.setInt(1, id); pstmt_update.setString(2, name); pstmt_update.setInt(3, sexx); pstmt_update.setInt(4, Room_number); pstmt_update.setString(5, name); pstmt_update.execute(); } else if (select_remove == 0) { String sql_remove = "delete from users where name =?"; PreparedStatement pstmt_remove = conn.prepareStatement(sql_remove); pstmt_remove.setString(1,name); pstmt_remove.execute(); System.out.println("7"); } else if (select_remove == 2) { int id = resultSet.getInt(1); String names = resultSet.getString(2); int sex = resultSet.getInt(3); int number = resultSet.getInt(4); //System.out.println(id+" "+names+" "+sex+" "+number); this.id = id; this.name = names; if (sex == 1) { this.sex = "MAN"; } else { this.sex = "WOMAN"; } this.Room_number = number; System.out.println(this.id + " "+this.name + " "+this.sex + " "+this.Room_number); } } else { System.out.println("wrong");//报错 } //释放资源 pstmt.close(); conn.close(); } public int Jdbc_select_id(int id) throws Exception { Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = "*****"; Connection conn = DriverManager.getConnection(url, username, password); //写sql语句 String sql = "select * from users where id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); ResultSet resultSet = pstmt.executeQuery(); if( resultSet.next()){ return 1; } return 0; } }
package nuc.Community_Management.datebaseClass; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class wat_ele { private int number; private int WE; private int EE; public wat_ele() { } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public int getWE() { return WE; } public void setWE(int WE) { this.WE = WE; } public int getEE() { return EE; } public void setEE(int EE) { this.EE = EE; } public wat_ele(int number, int WE, int EE) { this.number = number; this.WE = WE; this.EE = EE; try { jdbc_WE(); } catch (Exception e) { e.printStackTrace(); } } public void jdbc_WE() throws Exception { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = "***"; Connection conn = DriverManager.getConnection(url, username, password); //写sql语句 String sql = "insert into wat_ele (number,WE,EE)values (?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, number); pstmt.setInt(2, WE); pstmt.setInt(3, EE); pstmt.execute(); //释放资源 pstmt.close(); conn.close(); } public int get_sbp(int i, int id) throws Exception { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = "*****"; Connection conn = DriverManager.getConnection(url, username, password); String sql; //写sql语句 if (i == 1) { sql = "select WE from wat_ele where number =(select number from users where id = ?)"; } else { sql = "select EE from wat_ele where number =(select number from users where id = ?)"; } PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); ResultSet resultSet = pstmt.executeQuery(); int anInt = 0; if (resultSet.next()) { anInt = resultSet.getInt(1); } //释放资源 pstmt.close(); conn.close(); return anInt; } public void get_add(int i, int id) throws Exception { //注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //获取链接 String url = "jdbc:mysql:///java?useSSL=false"; String username = "root"; String password = "*****"; Connection conn = DriverManager.getConnection(url, username, password); String sql; //写sql语句 if (i == 1) { sql = "update wat_ele\n" + "set WE = (WE + 400)\n" + "where number = (select number from users where id = ?)"; } else { sql = "update wat_ele\n" + "set EE = (EE + 400)\n" + "where number = (select number from users where id = ?);"; } PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.execute(); //释放资源 pstmt.close(); conn.close(); } }