⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usermanager.java

📁 找回密码的程序
💻 JAVA
字号:
package password;

import java.sql.*;

/**
 * <p>Title: 项目实战 Stage2</p>
 *
 * <p>Description: ACCP 4.0</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: jb-aptech.com.cn</p>
 *
 * @author Dong Ping
 * @version 1.0
 */
public class UserManager {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    public UserManager() {
    }

    public void getConnection() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:xiangmuanli",
                                               "accp40", "accp40");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    public void closeConnection() {
        try {
            if (stmt != null) {
                stmt.close();
            }
            if (rs != null) {
                rs.close();
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }

    public ResultSet executeQuery(String sql) {
        rs = null;
        try {
            getConnection();
            if (conn != null) {
                stmt = conn.createStatement();
                rs = stmt.executeQuery(sql);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * login
     *
     * @param username String
     * @param password String
     */
    public boolean login(String username, String password) throws
            SystemException {
        boolean success = false;
        try {
            getConnection();
            PreparedStatement pstmt = conn.prepareStatement(
                    "select * from UserTbl where username = ? and password = ?");
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            if (rs.next()) {
               // pstmt.close();
              //  rs.close();
                success = true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new SystemException("系统产生错误");
        } finally {
            closeConnection();
        }
        return success;
    }

    /**
     * UsernameEmailExist
     *
     * @param username String
     * @param email String
     */
    public boolean UsernameEmailExist(String username, String email) throws
            SystemException {
        boolean exist = false;
        try {

            getConnection();
            PreparedStatement pstmt = conn.prepareStatement(
                    "select * from UserTbl where username = ? and email = ?");
            pstmt.setString(1, username);
            pstmt.setString(2, email);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                exist = true;
            }
           // pstmt.close();
          //  rs.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new SystemException("系统产生错误!");
        } finally {
            closeConnection();
        }
        return exist;
    }
    public String getPasswordByUsernameAndEmailaddress
            (String username, String email) throws
               SystemException {
           String password = null;
           try {

               getConnection();
               PreparedStatement pstmt = conn.prepareStatement(
                       "select password from UserTbl where username = ? and email = ?");
               pstmt.setString(1, username);
               pstmt.setString(2, email);
               rs = pstmt.executeQuery();
               if (rs.next()) {
                   password = rs.getString(1);
               }
             //  pstmt.close();
             //  rs.close();
           } catch (Exception ex) {
               ex.printStackTrace();
               throw new SystemException("系统产生错误!");
           } finally {
               closeConnection();
           }
           return password;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -