客户端登陆实现(swing)

首先是结构框架


dao层

package com.java1234.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.java1234.model.User;
import com.mysql.jdbc.ResultSet;

/**
 * 登陆验证
 * @author ufida
 *
 */

public class UserDao {
    
    public User login(Connection con,User user) throws Exception{
        User resultUser = null;
        String sql = "select * from t_user where userName=? and password=?";
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1, user.getUserName());
        pstmt.setString(2, user.getPassword());
        java.sql.ResultSet rs = pstmt.executeQuery();
        if(rs.next()){
            resultUser = new User();
            resultUser.setUserName(rs.getString("userName"));
            resultUser.setPassword(rs.getString("password"));
        }
        return resultUser;
    }
}


model层

package com.java1234.model;
/**
 * 用户模型类
 * @author ufida
 *
 */
public class User {
    private int id;
    private String userName;
    private String password;
    
    public User() {
        
    }
    
    public User(String userName, String password){
        this.userName = userName;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

util工具包

package com.java1234.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbUtil {
    private String dbUrl = "jdbc:mysql://localhost:3306/db_book";
    private String dbUserName = "root";
    private String dbPassword = "root";
    private String jdbcName = "com.mysql.jdbc.Driver";
    
    
    /**
     * 获取数据库连接
     * @return
     * @throws Exception
     */
    public Connection getCon() throws Exception{
        Class.forName(jdbcName);
        Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
        return con;
    }
    /**
     * 关闭数据库连接
     * @param con
     * @throws Exception
     */
    public void colseCon(Connection con) throws Exception{
        if(con != null){
            con.close();
        }
    }
    
//测试
    
    public static void main(String[] args) {
        DbUtil dbUtil = new DbUtil();
        try {
            dbUtil.getCon();
            System.out.println("数据库连接成功!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}



package com.java1234.util;

public class StringUtil {
    public static boolean isEmpty(String str) {
        if (str.equals("") || str == null) {
            return true;
        } else {
            return false;
        }
    }
}


view层

/*
 * logOnFrm.java
 *
 * Created on __DATE__, __TIME__
 */

package com.java1234.view;

import javax.swing.JOptionPane;

import com.java1234.dao.UserDao;
import com.java1234.model.User;
import com.java1234.util.DbUtil;
import com.java1234.util.StringUtil;

/**
 *
 * @author  __USER__
 */
public class logOnFrm extends javax.swing.JFrame {
    
    UserDao userDao = new UserDao();
    DbUtil dbUtil = new DbUtil();

    /** Creates new form logOnFrm */
    public logOnFrm() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        userNameTxt = new javax.swing.JTextField();
        passwordTxt = new javax.swing.JPasswordField();
        jb_logon = new javax.swing.JButton();
        jb_reset = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("\u7ba1\u7406\u5458\u767b\u9646");
        setResizable(false);
        jLabel1.setText("\u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf");

        jLabel2.setText("\u7528\u6237\u540d\uff1a");

        jLabel3.setText("\u5bc6\u7801:");

        jb_logon.setText("\u767b\u9646");
        jb_logon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_logonActionPerformed(evt);
            }
        });

        jb_reset.setText("\u91cd\u7f6e");
        jb_reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_resetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGap(155,
                                                                        155,
                                                                        155)
                                                                .addComponent(
                                                                        jLabel1))
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGap(57, 57,
                                                                        57)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(
                                                                                        jLabel2)
                                                                                .addComponent(
                                                                                        jLabel3))
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                                                false)
                                                                                .addComponent(
                                                                                        passwordTxt,
                                                                                        0,
                                                                                        0,
                                                                                        Short.MAX_VALUE)
                                                                                .addComponent(
                                                                                        userNameTxt,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        185,
                                                                                        Short.MAX_VALUE)))
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGap(83, 83,
                                                                        83)
                                                                .addComponent(
                                                                        jb_logon)
                                                                .addGap(59, 59,
                                                                        59)
                                                                .addComponent(
                                                                        jb_reset)))
                                .addContainerGap(105, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addGap(47, 47, 47)
                                .addComponent(jLabel1)
                                .addGap(36, 36, 36)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel2)
                                                .addComponent(
                                                        userNameTxt,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(42, 42, 42)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel3)
                                                .addComponent(
                                                        passwordTxt,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        47, Short.MAX_VALUE)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jb_reset)
                                                .addComponent(
                                                        jb_logon,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        25,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(42, 42, 42)));

        pack();
    }// </editor-fold>
        //GEN-END:initComponents

    private void jb_logonActionPerformed(java.awt.event.ActionEvent evt) {
        String userName = this.userNameTxt.getText();
        String password = new String(this.passwordTxt.getPassword());
        if (StringUtil.isEmpty(userName)) {
            JOptionPane.showMessageDialog(null, "用户名不能为空");
            return;
        }
        if (StringUtil.isEmpty(password)) {
            JOptionPane.showMessageDialog(null, "密码不能为空");
            return;
        }
        User user = new User(userName, password);
        try {
            User logonuser = userDao.login(dbUtil.getCon(), user);
            if (logonuser != null){
                JOptionPane.showConfirmDialog(null, "登陆成功");
                
            }else{
                JOptionPane.showConfirmDialog(null, "登陆失败");
                
            }
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showConfirmDialog(null, "登陆异常");
        }
    }

    private void jb_resetActionPerformed(java.awt.event.ActionEvent evt) {
        //            String userName = this.userNameTxt.getText();
        //            String password = new String(this.passwordTxt.getPassword());
        this.userNameTxt.setText("");
        this.passwordTxt.setText("");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new logOnFrm().setVisible(true);
            }
        });
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton jb_logon;
    private javax.swing.JButton jb_reset;
    private javax.swing.JPasswordField passwordTxt;
    private javax.swing.JTextField userNameTxt;
    // End of variables declaration//GEN-END:variables

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值