import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Login { Admin admin; void setAdmin(Admin admin) { this.admin=admin; //System.out.println(this.admin.getPassword()+" " + this.admin.getID()); } /* * JudgeAdmin()方法 * 判断User的ID和密码是否正确,如果正确,显示登录成功 * 如果错误,弹出一个窗口,显示账号或密码错误 */ private String driver = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://localhost:3306/qqq?serverTimezone=UTC&characterEncoding=utf-8"; private String user = "root"; private String password = "root"; public boolean login(Admin admin) throws SQLException, ClassNotFoundException { String sql="select * from admin where id=? and password=?"; Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, password); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, admin.getID()); ps.setString(2, admin.getPassword()); ResultSet rs = ps.executeQuery(); int ans = 0; if(rs.next()) { ans = 1; } rs.close(); ps.close(); conn.close(); if(ans == 1) { return true; } else return false; } int JudgeAdmin() { try { if(login(this.admin)) { System.out.println("登录成功"); return 1; }else { return 0; } }catch(Exception e) { //e.printStackTrace(); //System.out.println("!!!!!!!!!"); } return 0; } }
时间: 2024-04-22 10:23:34 浏览: 19
这段代码是一个 Java 类,名为 Login,其中包含了一个 Admin 类型的属性 admin 和多个方法。其中 setAdmin 方法用于设置 admin 属性的值,login 方法用于判断用户输入的 ID 和密码是否正确,JudgeAdmin 方法用于判断用户的登录是否成功。在 login 方法中,使用了 JDBC 连接数据库,执行了一次 SQL 查询语句,根据查询结果返回 true 或 false。在 JudgeAdmin 方法中,调用了 login 方法,如果返回 true,则打印出登录成功的信息并返回 1,否则返回 0。
相关问题
package project; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class jia { public void ad(String sql, String[] objects) throws SQLException { Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.24.224:3306/dorm", "root", "123456"); PreparedStatement pstm = conn.prepareStatement(sql, 1004, 1007); if (objects != null && objects.length > 0) { for (int i = 0; i < objects.length; i++) { pstm.setObject(i + 1, objects[i]); } } Statement stmt=null; try { stmt =conn.createStatement(); int count=stmt.executeUpdate(sql); System.out.println(count==1?"保存成功":"保存失败"); }catch(SQLException e) { e.printStackTrace(); }finally { try{ if(stmt!=null) { } stmt.close(); }catch(SQLException e) { e.printStackTrace(); } try{ if(conn!=null) { } conn.close(); }catch(SQLException e) { e.printStackTrace(); } } pstm.close(); } }
这是一个 Java 类,其中包含了一个名为 "ad" 的方法。这个方法接受两个参数:一个是 SQL 语句,另一个是一个字符串数组,表示 SQL 语句中的占位符的值。这个方法的作用是将给定的数据保存到 MySQL 数据库中。在方法中,首先通过 DriverManager 类的 getConnection 方法获取一个数据库连接对象,然后使用 PreparedStatement 类的 setObject 方法将占位符的值设置进去。接着,使用 Statement 类的 executeUpdate 方法执行 SQL 语句,并将返回结果保存在 count 变量中。最后,根据 count 的值判断数据是否保存成功,并关闭所有的数据库连接。
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;;这是一串代码,请帮我完善
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(); } }
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)