标题:为什么输入用户名和密码没用?
只看楼主
你好ewvew
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2016-1-7
结帖率:50%
已结贴  问题点数:10 回复次数:2 
为什么输入用户名和密码没用?
程序代码:
/*

 * 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
泡泡先生
Rank: 1
来 自:山东
等 级:新手上路
帖 子:1
专家分:5
注 册:2016-6-12
得分:5 
你登录数据库时用的账号是本机机名,当然登录不上,话说awt我都没用过
2016-06-12 17:58
shaoyongyang
Rank: 2
等 级:论坛游民
帖 子:20
专家分:25
注 册:2012-7-12
得分:5 
错误是,表不存在,基本能确定是数据库问题了。
2016-06-12 18:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-465774-1-1.html




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

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