hy1000000
2017-11-14 14:58
采纳率: 0%
浏览 1.8k

java文本框读取显示乱码

package Conn;

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

import sun.security.krb5.internal.PAData;
import sun.security.util.Password;

public class conmysql {

     static String userword;
     static String pwd;//账号和密码
     static Connection connection=null;
     static Statement stmt=null;//预备处理语句
     static ResultSet rs=null;//结果集
     static Vector<Object> sVec=new Vector<Object>();
     static Vector<Object> cVec=new Vector<Object>();
     static String[] sName={"username"};
     static String[] sName1={"password"};
     static String sql="select*from accountnumber";
 public  void connectMysql() {
    try {
        Class.forName("com.mysql.jdbc.Driver");//驱动加载
        connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root","root");
        System.out.println("数据库连接成功");
        stmt = connection.createStatement() ;
        rs=stmt.executeQuery(sql);
        mySqlselect(rs);




    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        try {
            connection.close();
            stmt.close();
            rs.close();

        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

}

private static void mySqlselect(ResultSet RS) {

    try {

        while (rs.next()) {
            String str=RS.getString("username");
            String str1=RS.getString("password");
            sVec.add(str);
            cVec.add(str1);


        }

        }catch(Exception e){
            e.printStackTrace();
        }
    }


    public  int ISName(String user,String passwd) {
        // TODO Auto-generated method stub
        int m_b=0;
        Object[] str2=sVec.toArray();
        Object[] str3=cVec.toArray();
        System.out.println(str2.length);
        System.out.println(str3.length);


        for(int i=0;i<str2.length;i++){
            if((String)str2[i]==user && (String)str3[i]==passwd){
                m_b=1;
                break;
            }
        }

        return m_b;

    }


 }

package loginSystem;
import Conn.*;
import java.awt.GradientPaint;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor.GetterOnlyReflection;

public class login extends JFrame implements ActionListener{
JPanel panel,panel1,panel2,panel3=null;
JLabel label,label1,label2=null;
JButton button,button1,button2=null;
JMenuBar bar1;
JMenu jm,jm1;
JRadioButton jra,jra1;
ButtonGroup bg;
JTextField jt;
JPasswordField jpa;
conmysql conn=new conmysql();

public login() {
    //初始化一个空白容器
     panel=new JPanel();
     panel1=new JPanel();
     panel2=new JPanel();
     panel3=new JPanel();

     label=new JLabel("用户名");//创建一个 用户名的标签
     label1=new JLabel("密   码 ");
     label2=new JLabel("权   限");



     button=new JButton("登录");//创建一个“登录”按钮
     button1=new JButton("重置");
     button2=new JButton("退出");

     button.addActionListener( this);//设置监听
     button1.addActionListener( this);
     button2.addActionListener(this);

    bar1=new JMenuBar();//新建一个菜单栏
    jm=new JMenu("选项");//创建一个菜单
    jm1=new JMenu("退出系统");
    jm.add(jm1);//把“退出系统”菜单添加到“选项”菜单中
    bar1.add(jm);

    //jra=new JRadioButton("管理员",true);//创建一个复选框
    //jra1=new JRadioButton("学生");

    bg=new ButtonGroup();
    //bg.add(jra);//把复选框添加到按钮组里面
    //bg.add(jra1);
    jt=new JTextField(10);//新建一个文本框
    jpa=new JPasswordField(10);//新建一个密码文本框

    panel.add(label);
    panel.add(jt);

    panel1.add(label1);
    panel1.add(jpa);

    panel2.add(label2);
    //panel2.add(jra);
    //panel2.add(jra1);

    panel3.add(button);
    panel3.add(button1);
    panel3.add(button2);

    this.setJMenuBar(bar1);
    this.add(panel);
    this.add(panel1);
    this.add(panel2);
    this.add(panel3);

    this.setLayout(new GridLayout(4,1));
    this.setTitle("学生宿舍系统");
    this.setSize(300, 250);
    this.setLocation(600,600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    this.setResizable(true);



}
public static void main(String[] args) {
    new login();


}
@Override

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand()=="退出") {//获取标签名
        System.exit(0);
        }
    else if (e.getActionCommand()=="登录") {
        String UserText=jt.getText();
        char[] PaText=jpa.getPassword();
        String PasswdText=PaText.toString();
        System.out.println(PaText+" "+PasswdText);

        if (UserText==null||PasswdText==null) {
            System.out.println("用户名和密码为空");

        }

        if (UserText==null&&PasswdText==null) {
            System.out.println("用户名或密码为空");

            }
        conn.connectMysql();
        int i=conn.ISName(UserText, PasswdText);
        System.out.println(i);
        if(1==i){
                System.out.println("信息已确认");
            }else{
                System.out.println("用户名或密码错误");
                //continue;
            }



        }

    }

}

图片说明

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题