📄 login.java
字号:
/*
* login.java
*
* Created on 2007年12月19日, 上午6:42
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import java.sql.*;
import java.util.*;
import java.util.Date;
/**
*
* @author Owner
*/
public class login extends JPanel implements ActionListener
{
userflag userflag;
JTextField tid;
JTextField tpassword;
Box box1,box2,box3,basebox;
JButton submit,reset;
register regist;
Connection con;
Statement sql;
ResultSet rs;
/** Creates a new instance of login */
public login(userflag userflag) {
this.regist=regist;
setLayout(null);
this.userflag=userflag;
submit=new JButton("确认");
reset=new JButton("重设");
submit.addActionListener(this);
reset.addActionListener(this);
tid=new JTextField(15);
tpassword=new JTextField(15);
box1=Box.createHorizontalBox();
box1.add(new JLabel("用户名"));
box1.add(Box.createHorizontalStrut(10));
box1.add(tid);
box2=Box.createHorizontalBox();
box2.add(new JLabel("密码"));
box2.add(Box.createHorizontalStrut(22));
box2.add(tpassword);
box3=Box.createHorizontalBox();
box3.add(submit);
box3.add(Box.createHorizontalStrut(20));
box3.add(reset);
basebox=Box.createVerticalBox();
basebox.add(box1);
basebox.add(Box.createVerticalStrut(10));
basebox.add(box2);
basebox.add(Box.createVerticalStrut(20));
basebox.add(box3);
add(basebox);
basebox.setBounds(200,140,160,120);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==submit)
{
if(tid.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"用户名不能为空");
}
else if(tpassword.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"密码不能为空");
}
else
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e2){}
try
{
con=DriverManager.getConnection("jdbc:odbc:sample","std","123");
sql=con.createStatement();
String str="select * from user where id='"+tid.getText()+"' and password='"+tpassword.getText()+"'";
System.out.println(str);
rs=sql.executeQuery(str);
if(rs.next()==true)
{
System.out.println(tid.getText());
userflag.login(tid.getText());
JOptionPane.showMessageDialog(this,"已经登录");
}
else
{
JOptionPane.showMessageDialog(this,"没有此用户名或密码错误");
}
}
catch(SQLException e3)
{
System.out.println("e3"+e3);
}
}
}
else if(e.getSource()==reset)
{
tid.setText(null);
tpassword.setText(null);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -