當前位置:編程學習大全網 - 編程語言 - 用java語言寫壹個用戶登陸界面

用java語言寫壹個用戶登陸界面

//這個是我寫的,裏面有連接數據庫的部分。妳可以拿去參考壹下

import?java.awt.*;

import?javax.swing.*;

import?java.awt.event.*;

import?java.sql.*;

class?LoginFrm?extends?JFrame?implements?ActionListener//?throws?Exception

{

JLabel?lbl1?=?new?JLabel("用戶名:");

JLabel?lbl2?=?new?JLabel("密碼:");

JTextField?txt?=?new?JTextField(5);

JPasswordField?pf?=?new?JPasswordField();

JButton?btn1?=?new?JButton("確定");

JButton?btn2?=?new?JButton("取消");

public?LoginFrm()?{

this.setTitle("登陸");

JPanel?jp?=?(JPanel)?this.getContentPane();

jp.setLayout(new?GridLayout(3,?2,?5,?5));

jp.add(lbl1);

jp.add(txt);

jp.add(lbl2);

jp.add(pf);

jp.add(btn1);

jp.add(btn2);

btn1.addActionListener(this);

btn2.addActionListener(this);

}

public?void?actionPerformed(ActionEvent?ae)?{

if?(ae.getSource()?==?btn1)?{

try?{

Class.forName("com.mysql.jdbc.Driver");//?mysql數據庫

Connection?con?=?DriverManager.getConnection(

"jdbc:mysql://localhost/Car_zl",?"root",?"1");//?數據庫名為Car_zl,密碼為1

System.out.println("com?:?"+?con);

Statement?cmd?=?con.createStatement();

String?sql?=?"select?*?from?user?where?User_ID='"

+?txt.getText()?+?"'?and?User_ps='"

+?pf.getText()?+?"'"?;

ResultSet?rs?=?cmd

.executeQuery(sql);//?表名為user,user_ID和User_ps是存放用戶名和密碼的字段名

if?(rs.next())?{

JOptionPane.showMessageDialog(null,?"登陸成功!");

}?else

JOptionPane.showMessageDialog(null,?"用戶名或密碼錯誤!");

}?catch?(Exception?ex)?{

}

if?(ae.getSource()?==?btn2)?{

System.out.println("1111111111111");

//txt.setText("");

//pf.setText("");

System.exit(0);

}

}

}

public?static?void?main(String?arg[])?{

JFrame.setDefaultLookAndFeelDecorated(true);

LoginFrm?frm?=?new?LoginFrm();

frm.setSize(400,?200);

frm.setVisible(true);

}

}

  • 上一篇:集中化、標準化、常態化?
  • 下一篇:壹年級數學創意繪畫
  • copyright 2024編程學習大全網