| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 397 人关注过本帖
标题:求指教1,有点错!1
只看楼主 加入收藏
wanghaomin
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2010-11-18
结帖率:66.67%
收藏
 问题点数:0 回复次数:0 
求指教1,有点错!1
实现学生登陆,用户名和密码都是学生的姓名   

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

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

public class Info_Manage extends Frame
implements ActionListener
{
    JLabel     JLUserName =new JLabel("用户名:");//创建一个标签对象,显示”用户名“
    JLabel     JLPaw =new JLabel("密    码:");//创建一个标签对象,显示”密码“
    JTextField JTUserName=new JTextField();//创建一个文本框对象
    JPasswordField JPsw  =new JPasswordField();//创建一个密码框对象
    //Icon okIcon = new ImageIcon("ico/ok.png");
    JButton    JB1 =new JButton("登录");//创建按钮对象
    JButton    JB2  =new JButton("取消");
    public Info_Manage()
    {
        this.setTitle("学生信息管理系统");//设置窗口的显示标题
        this.setLayout(null);
        
        
        JLUserName.setBounds(100,40,100,20);//设置姓名标签显示的大小以及位置
        this.add(JLUserName);                //将组件添加到容器中
        
        JTUserName.setBounds(200,40,80,20);//设置姓名输入对话框标签显示的大小以及位置
        this.add(JTUserName);                //将组件添加到容器中
        
        JLPaw.setBounds(100,100,60,20);//设置密码标签显示的大小以及位置
        this.add(JLPaw);                //将组件添加到容器中
        
        JPsw.setBounds(200,100,80,20);//设置密码框显示的大小以及位置
        this.add(JPsw );                //将组件添加到容器中
        
        JB1.setBounds(100,200,60,20);//设置按钮显示的大小以及位置
        this.add(JB1);                //将组件添加到容器中
        JB1.addActionListener(this);
        
        JB2.setBounds(200,200,60,20);
        this.add(JB2);                //将组件添加到容器中
        JB2.addActionListener(this);
        
        this.setVisible(true);
        this.setBounds(10,10,400,250);
        addWindowListener(new WindowAdapter()//设置关闭窗口的事件监听

                    { public void windowClosing(WindowEvent e)

                       {

                          System.exit(0);

                         }

                    });
        
    }
   
    public static void main(String args[])//程序的主方法
    {
        new Info_Manage();
    }
   

   
    public void actionPerformed(ActionEvent e)    //单击登陆时的事件处理
    {        String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎
        String dbUrl = "jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName=student";// 数据源

        try {
            Class.forName(dbDriver);// 加载数据库引擎,返回给定字符串名的类
        } catch (ClassNotFoundException n) {
            // e.printStackTrace();
            System.out.println("加载数据库引擎失败");
            System.exit(0);
        }
        System.out.println("数据库驱动成功");

        try {
            Connection conn = DriverManager.getConnection(dbUrl);// 连接数据库对象
            System.out.println("连接数据库成功");
            Statement stmt = conn.createStatement();// 创建SQL命令对象
            
            if(e.getSource()==JB1)
        {
            String name=JTUserName.getText();    //获取到用户名
            String password=new String(JPsw.getPassword());//获取到密码
            
            String sqlname="SELECT 用户名 FROM 登录";
            stmt.executeUpdate(sqlname);
            
            String sqlpassword="SELECT 密码 FROM 登录";
            stmt.executeUpdate(sqlpassword);
            
            if((name!=null&&(name==sqlname))&&(password!=null&&
            (password==sqlpassword)))//判断用户名和密码是否匹配
            
            
                {
            
                new student_manage();//打开主页面
                this.setVisible(false);//隐藏该登陆窗口
               
                }
              else
              {
                JOptionPane.showMessageDialog(null,"用户名或密码不正确,请重新填写...");
              }
            
        }
        

            } catch (SQLException n) {
            n.printStackTrace();
            // System.out.println("数据库连接错误");
            System.exit(0);
        }
        
        
        
            
        
    }
}
搜索更多相关主题的帖子: 标签 密码 public 用户名 import 
2012-01-02 12:33
快速回复:求指教1,有点错!1
数据加载中...
 
   



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

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