| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2353 人关注过本帖
标题:为什么输入用户名和密码没用?
取消只看楼主 加入收藏
你好ewvew
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2016-1-7
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:0 
为什么输入用户名和密码没用?
程序代码:
/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */
package xueshengxuanke;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class User extends JFrame {

    private JLabel use, password;
    private JTextField k1;//用户名输入框
    private JPasswordField k2;//密码输入框
    private JButton b1, b2;

//登录窗口
    public User(JFrame f) {
        super("系统登录");
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        use = new JLabel("username:");
        use.setFont(new Font("Serif", Font.PLAIN, 20));
        password = new JLabel("password:");
        password.setFont(new Font("Serif", Font.PLAIN, 20));
        k1 = new JTextField(12);
        k2 = new JPasswordField(12);
        b1 = new JButton("登录");
        b2 = new JButton("退出");

        //    设置登录方法
        BHandler b = new BHandler();
        EXIT d = new EXIT();
        b1.addActionListener(b);
        b2.addActionListener(d);

        //添加控件
        c.add(use);
        c.add(k1);
        c.add(password);
        c.add(k2);
        c.add(b1);
        c.add(b2);

        setBounds(600, 300, 250, 150);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
//主函数

    public static void main(String[] args) {
        User f1 = new User(new JFrame());
    }
//登录按钮方法

   

    private class BHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            if (k1.getText().equals("") || k2.getText().equals("")) {
                JOptionPane.showMessageDialog(User.this, "用户名密码不能为空!");
            } else {
                Statement stmt = null;
                ResultSet rs = null;
                String sql;
                sql = "select * from admin where username='" + k1.getText() + "'";
                try {
                    Connection dbConn1 = students();
                    stmt = (Statement) dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    rs = stmt.executeQuery(sql);
                    if (rs.next()) {
                        String xm = rs.getString("password");
                        if (k2.getText().equals(xm.trim())) {
                            JOptionPane.showMessageDialog(User.this, "登录成功");
                            dispose();
                            new Menu();//管理窗口                                                                
                        } else {
                            JOptionPane.showMessageDialog(User.this, "密码错误");
                        }
                    } else {
                        JOptionPane.showMessageDialog(User.this, "用户名错误");
                    }
                    rs.close();
                    stmt.close();
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(User.this, "SQL Exception occur.Message is:" + e.getMessage());
                }
            }
        }
    }
//退出方法结束

    private class EXIT implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent even) {
            System.exit(0);
        }
    }

//连接数据库方法
    public static Connection students(){
       String driverName = "com.mysql.jdbc.Driver";   //加载JDBC驱动
       String dbURL = "jdbc:mysql://localhost:3306/students?useUnicode = true&characterEncoding = utf8";   //连接服务器和数据库test
       String userName = "root";   //默认用户名
       String userPwd = "root";   //密码
       Connection dbConn=null;
       try {
       Class.forName(driverName);
       dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
       System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!
       } catch (Exception e) {
       e.printStackTrace();
       }
       return dbConn;
}
}



我输入用户名和密码之后,显示
图片附件: 游客没有浏览图片的权限,请 登录注册


[此贴子已经被作者于2016-6-10 10:34编辑过]

搜索更多相关主题的帖子: file 密码 change 用户名 
2016-06-10 10:32
快速回复:为什么输入用户名和密码没用?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043796 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved