之前写的逻辑都不完善,所以进行了改进。
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
ZhuCei zc=new ZhuCei();
Den2 d2=new Den2();
Den d=new Den();
while(true) {
Thread t1= new MyThread();
t1.start();
t1.join();
System.out.println(" 1.超市系统注册");
System.out.println(" 2.超市系统登录");
System.out.println(" 3.超市系统退出");
System.out.println(" 4.超市管理员系统");
Scanner sc=new Scanner(System.in);
int a=0;
try{
try {
a= sc.nextInt();
}catch (Exception e){
throw new MyException("请输入数字");
}
}catch (Exception ee){
ee.printStackTrace();
}
switch (a) {
case 1:
System.out.println("请输入用户名");
String user = sc.next();
System.out.println("请输入密码");
String password = sc.next();
zc.ZhuCei(user, password);
break;
case 2:
System.out.println("请输入用户名");
user = sc.next();
System.out.println("请输入密码");
password= sc.next();
d.Den(user, password);
break;
case 3:
return;
case 4:
System.out.println("请输入用户名");
user = sc.next();
System.out.println("请输入密码");
password = sc.next();
d2.Den2(user, password);
Guanli gl = new Guanli();
gl.guanli();
}
}
}
}
public class MyException extends Exception{
String msg;
public MyException(){
}
public MyException(String msg){
super(msg);
}
}
import java.io.*; import java.sql.*; import java.util.*; public class Den { public void Den(String user,String password2) throws SQLException /*throws Exception */{ String url="jdbc:mysql://localhost:3306/db1"; String un="root"; String pw="123456"; Connection conn = null;//Drivermanager得一个getconnection try { conn = DriverManager.getConnection(url,un,pw); } catch (SQLException e) { throw new RuntimeException(e); } String sql ="select * from user where username ='"+user+"' and password='"+password2+"'"; Statement stmt = null; stmt = conn.createStatement(); ResultSet count=stmt.executeQuery(sql); boolean a =true; if(count.next()==a){ System.out.println("登录成功"); while(true) { System.out.println("1.购买物品"); System.out.println("2.退出"); Scanner sc=new Scanner(System.in); int aa=0; try{ try { aa= sc.nextInt(); }catch (Exception e){ throw new MyException("请输入数字"); } }catch (Exception ee){ ee.printStackTrace(); } switch(aa) { case 1: Cha b = new Cha(); b.cha(); Buy c = new Buy(); try { c.buy(user); } catch (Exception e) { throw new RuntimeException(e); } break; case 2: return; } } } else { System.out.println("登录失败"); } /*File s=new File("d:/chaoshi/2.txt"); FileReader fr=new FileReader(s); BufferedReader br=new BufferedReader(fr); String zong=user+"@"+password; int count =1; while(true){ String line=br.readLine(); if(zong.equals(line)) { System.out.println("登录成功"); System.out.println("是否购买东西y/n"); System.out.println("请输入"); Scanner sc2=new Scanner(System.in); String c=sc2.next(); if(c.equals("y")){ Cha ch=new Cha(); ch.cha(); System.out.println("请购物"); Buy b=new Buy(); b.buy(); } } if(line == null) break; }*/ } }
import java.sql.*; public class Cha { public void cha() throws SQLException { int cnn=1; String url="jdbc:mysql://localhost:3306/db1"; String username="root"; String password="123456"; Connection conn = null;//Drivermanager得一个getconnection try { conn = DriverManager.getConnection(url,username,password); } catch (SQLException e) { throw new RuntimeException(e); } String sql ="select * from shangpin"; Statement stmt = null; try { stmt = conn.createStatement(); } catch (SQLException e) { throw new RuntimeException(e); } ResultSet count= null; try { count = stmt.executeQuery(sql); } catch (SQLException e) { System.out.println("购买失败"); } while(count.next()){ System.out.println(count.getInt(1)+" "+count.getString(2)+" "+count.getInt(3)+" "+count.getInt(4)); } stmt.close(); conn.close(); } }
import java.io.*; import java.sql.*; public class ZhuCei { public void ZhuCei(String user,String password) throws SQLException /* throws IOException*/ { int count=0; String url="jdbc:mysql://localhost:3306/db1"; String un="root"; String pw="123456"; Connection conn = null; try { conn = DriverManager.getConnection(url,un,pw); } catch (SQLException e) { System.out.println("222222"); } String sql ="insert into user(username,password)values("+user+","+password+")"; Statement stmt = null; try { stmt = conn.createStatement(); } catch (SQLException e) { System.out.println("222222"); } try { count=stmt.executeUpdate(sql); System.out.println("注册成功"); } catch (SQLException e) { System.out.println("注册失败"); } stmt.close(); conn.close(); /*File f=new File("d:/chaoshi/2.txt"); FileReader fr=new FileReader(f); BufferedReader br=new BufferedReader(fr); int sh=0; while(true) { String line=br.readLine(); if(line==null) break; String str = line; String[] split = str.split("@",10); //split:{"1","2"} if(split[0].equals(user)){ System.out.println("已注册"); sh=1; break; } } fr.close(); if(sh==0) { FileWriter fw = new FileWriter(f, true); PrintWriter bw = new PrintWriter(fw); bw.println(user + "@" + password); bw.println("\n"); fw.close(); System.out.println("注册成功"); } }*/ } }
import java.sql.*; import java.util.*; public class Buy { public void buy (String user) throws Exception { System.out.println("选择想购买的那个物品(name)和数量(geshu)"); System.out.println("输入name"); Scanner sc=new Scanner(System.in); String shuname=sc.next(); System.out.println(" 输入个数 "); Scanner sc2=new Scanner(System.in); int shugeshu=0; try { shugeshu = sc2.nextInt(); }catch(Exception e){ System.out.println("请输入数字"); } String url="jdbc:mysql://localhost:3306/db1"; String username="root"; String password="123456"; Connection conn = DriverManager.getConnection(url,username,password); String sql ="select * from shangpin where name='"+shuname+"'"; Statement stmt = conn.createStatement(); ResultSet count= stmt.executeQuery(sql); boolean t=true; if(count.next()==t&&shugeshu<count.getInt(4)&&shugeshu!=0){ System.out.println("购买成功"); int id=count.getInt(1); String name=count.getString(2); int price =count.getInt(3); int geshu=count.getInt(4); int shengeshu=geshu-shugeshu; String url2="jdbc:mysql://localhost:3306/db1"; String username2="root"; String password2="123456"; Connection conn2 = DriverManager.getConnection(url,username,password); String sql2="update shangpin set shangpin.geshu="+shengeshu+" where shangpin.name='"+shuname+"'"; Statement stmt2 = conn2.createStatement(); int count2=stmt2.executeUpdate(sql2); System.out.println(shugeshu*price+"元"); conn2.close(); stmt2.close(); }else{ System.out.println("购买失败"); } conn.close(); stmt.close(); } }
public class MyThread extends Thread{ public void run(){ System.out.println("*********************"); System.out.println(" 欢迎进入shop"); System.out.println("*********************"); } }
import java.sql.*; import java.util.*; public class Guanli { public void guanli() throws Exception { while(true) { System.out.println(" 1.增"); System.out.println(" 2.删"); System.out.println(" 3.改"); System.out.println(" 4.查"); System.out.println(" 5.退出"); Scanner sc=new Scanner(System.in); int a=0; try{ try { a= sc.nextInt(); }catch (Exception e){ throw new MyException("请输入数字"); } }catch (Exception ee){ ee.printStackTrace(); } switch (a) { case 4: System.out.println("查询"); System.out.println("id shangpin price geshua"); //Class.forName("com.mysql.cj.jdbc.Driver");//里面又一个建泰代码块是drivermanager String url = "jdbc:mysql://localhost:3306/db1"; String username = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, username, password);//Drivermanager得一个getconnection String sql = "select * from shangpin"; Statement stmt = conn.createStatement(); ResultSet count = stmt.executeQuery(sql); while (count.next()) { System.out.println(count.getInt(1) + " " + count.getString(2) + " " + count.getInt(3) + " " + count.getInt(4)); } stmt.close(); conn.close(); break; case 3: System.out.println("请输入想要改得商品名字"); Scanner fangxiang = new Scanner(System.in); String f = sc.next(); System.out.println("请输入要改的商品价格"); Scanner fangxiang2 = new Scanner(System.in); int f2 = sc.nextInt(); System.out.println("请输入编号来决定要改的那个物品"); Scanner id = new Scanner(System.in); int i = sc.nextInt(); String url2 = "jdbc:mysql://localhost:3306/db1"; String username2 = "root"; String password2 = "123456"; Connection conn2 = DriverManager.getConnection(url2, username2, password2);//Drivermanager得一个getconnection String sql2 = "update shangpin set shangpin.name ='" + f + "',shangpin. price =" + f2 + " where shangpin.id=" + i + ""; Statement stmt2 = conn2.createStatement(); int count2 = stmt2.executeUpdate(sql2); System.out.println(count2); if (count2 > 0) { System.out.println("修改成功"); } else { System.out.println("修改失败"); } stmt2.close(); conn2.close(); break; case 1: System.out.println("请输入想要改得商品名字"); Scanner fangxiang3 = new Scanner(System.in); String f3 = sc.next(); System.out.println("请输入编号来决定要改的那个物品"); Scanner id3 = new Scanner(System.in); int i3 = sc.nextInt(); String url3 = "jdbc:mysql://localhost:3306/db1"; String username3 = "root"; String password3 = "123456"; Connection conn3 = DriverManager.getConnection(url3, username3, password3);//Drivermanager得一个getconnection String sql3 = "insert into shangpin(id,name,price)values(null,'" + f3 + "'," + i3 + ")"; Statement stmt3 = conn3.createStatement(); int count3 = stmt3.executeUpdate(sql3); if (count3 > 0) { System.out.println("增加成功"); } else { System.out.println("增加失败"); } stmt3.close(); conn3.close(); break; case 2: System.out.println("请输入你要删的行的编号"); Scanner id4 = new Scanner(System.in); int i4 = sc.nextInt(); String url4 = "jdbc:mysql://localhost:3306/db1"; String username4 = "root"; String password4 = "123456"; Connection conn4 = DriverManager.getConnection(url4, username4, password4);//Drivermanager得一个getconnection String sql4 = "delete from shangpin where id=" + i4 + ""; Statement stmt4 = conn4.createStatement(); int count4 = stmt4.executeUpdate(sql4); if (count4 > 0) { System.out.println("删除成功"); } else { System.out.println("删除失败"); } break; case 5: return; } } } }
import java.io.*; public class Den2 { public void Den2(String user,String password) throws IOException { File s=new File("d:/chaoshi/2.txt"); FileReader fr=new FileReader(s); BufferedReader br=new BufferedReader(fr); String zong=user+"@"+password; int count =1; while(true){ String line=br.readLine(); if(zong.equals(line)) { System.out.println("登录成功"); } if(line == null) break; } } }