package data3; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLDataException; import java.sql.SQLException; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Scanner; import data3.test.classselect; public class main { private static List<tele> telelist=new ArrayList<tele>(); public static void main(String[] args) throws ClassNotFoundException, SQLException { Scanner scanner = new Scanner(System.in); int choice; while (true) { displayMenu(); choice = scanner.nextInt(); scanner.nextLine(); // 清除输入缓冲区中的换行符 switch (choice) { case 1: showUserList(); break; case 2: update(); break; case 3: Insert(); break; case 4: delete(); break; case 0: System.out.println("感谢使用!"); return; default: System.out.println("无效的选项,请重新输入。"); } } } private static void displayMenu() { System.out.println("------------------手机营销充值中心----------------"); System.out.print("1. 查看手机用户列表"); System.out.print(" 2. 充值"); System.out.print(" 3. 新用户注册"); System.out.print(" 4. 老用户注销"); System.out.println(" 0. 退出"); System.out.print("请选择操作:"); } private static List<tele> showUserList() throws SQLException { Connection conn=null; PreparedStatement pst =null; ResultSet rs =null; try { conn = classselect.getConnection(); String sql = "select * from phone"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); List<tele;;这是一串代码,请帮我完善
时间: 2024-03-30 07:33:37 浏览: 141
telelist = new ArrayList<tele>(); // 创建手机用户列表 while (rs.next()) { // 遍历结果集并将每个用户添加到列表中 int id = rs.getInt(1); String name = rs.getString(2); String gender = rs.getString(3); String phone = rs.getString(4); int balance = rs.getInt(5); telelist.add(new tele(id, name, gender, phone, balance)); } System.out.println("手机用户列表:"); System.out.println("编号\t姓名\t性别\t手机号码\t余额"); for (tele t : telelist) { // 遍历用户列表并输出用户信息 System.out.println(t.getId() + "\t" + t.getName() + "\t" + t.getGender() + "\t" + t.getPhone() + "\t" + t.getBalance()); } return telelist; } catch (SQLException e) { e.printStackTrace(); } finally { rs.close(); pst.close(); conn.close(); } return null; } private static void update() throws SQLException { Scanner scanner = new Scanner(System.in); System.out.print("请输入要充值的手机号码:"); String phone = scanner.next(); System.out.print("请输入充值金额:"); int amount = scanner.nextInt(); Connection conn=null; PreparedStatement pst =null; ResultSet rs =null; try { conn = classselect.getConnection(); String sql = "update phone set balance=balance+? where phone=?"; pst = conn.prepareStatement(sql); pst.setInt(1, amount); pst.setString(2, phone); int rows = pst.executeUpdate(); if (rows > 0) { System.out.println("充值成功!"); } else { System.out.println("充值失败,请检查手机号码是否正确。"); } } catch (SQLException e) { e.printStackTrace(); } finally { rs.close(); pst.close(); conn.close(); } } private static void Insert() throws SQLException { Scanner scanner = new Scanner(System.in); System.out.print("请输入姓名:"); String name = scanner.next(); System.out.print("请输入性别:"); String gender = scanner.next(); System.out.print("请输入手机号码:"); String phone = scanner.next(); System.out.print("请输入初始余额:"); int balance = scanner.nextInt(); Connection conn=null; PreparedStatement pst =null; ResultSet rs =null; try { conn = classselect.getConnection(); String sql = "insert into phone(name,gender,phone,balance) values(?,?,?,?)"; pst = conn.prepareStatement(sql); pst.setString(1, name); pst.setString(2, gender); pst.setString(3, phone); pst.setInt(4, balance); int rows = pst.executeUpdate(); if (rows > 0) { System.out.println("注册成功!"); } else { System.out.println("注册失败,请检查手机号码是否已注册。"); } } catch (SQLException e) { e.printStackTrace(); } finally { rs.close(); pst.close(); conn.close(); } } private static void delete() throws SQLException { Scanner scanner = new Scanner(System.in); System.out.print("请输入要注销的手机号码:"); String phone = scanner.next(); Connection conn=null; PreparedStatement pst =null; ResultSet rs =null; try { conn = classselect.getConnection(); String sql = "delete from phone where phone=?"; pst = conn.prepareStatement(sql); pst.setString(1, phone); int rows = pst.executeUpdate(); if (rows > 0) { System.out.println("注销成功!"); } else { System.out.println("注销失败,请检查手机号码是否正确。"); } } catch (SQLException e) { e.printStackTrace(); } finally { rs.close(); pst.close(); conn.close(); } }
阅读全文