loginframe.java

来自「在eclipse开发环境下使用java变成语言连接Mysql,读取数据」· Java 代码 · 共 62 行

JAVA
62
字号
package verity;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.print.DocFlavor.STRING;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import java.awt.GridLayout;
import java.sql.*;

public class LoginFrame extends JFrame implements ActionListener{

    JLabel label =new JLabel("用户明和密码");
    JTextField tfname=new JTextField("");
    JPasswordField tfpassword=new JPasswordField("");
    JButton button=new JButton("login");
    public LoginFrame(){
	this.setTitle("verfy login");
	button.addActionListener(this);
	this.getContentPane().setLayout(new GridLayout(4,1));
	this.getContentPane().add(label);
	this.getContentPane().add(tfname);
	this.getContentPane().add(tfpassword);
	this.getContentPane().add(button);
	this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	this.setSize(300,200);
	this.setLocation(200,300);
	this.setVisible(true);
    }
    public void actionPerformed(ActionEvent arg0) {
	try{
	    Class.forName("org.gjt.mm.mysql.Driver");
	    java.sql.Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","root");
	    java.sql.Statement stmt=conn.createStatement();
	    String sql="select* from account where username='"+tfname.getText()+"'";
	    sql+="and password='"+tfpassword.getText()+"'";
	    ResultSet rs=stmt.executeQuery(sql);
	    if(rs.next())
		label.setText("hefa");
	    else
		label.setText("no");
	}catch(Exception ee){
	    ee.printStackTrace();
	}
	
    }
    public static void main(String[] args){
	new LoginFrame();
    }
    

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?