Java登录小程序

//登录源码

package com.shat.login;
import com.shat.dao.*;
import com.shat.MainFrame;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import java.awt.event.*;
public class Login1 extends javax.swing.JFrame {
private JLabel jl_UserCode;
private JTextField jt_UserCode;
private JLabel jl_UserName;
private JTextField jt_UserName;
private JLabel jl_Password;
private JPasswordField jp_Password;
private JButton jb_login;
private JButton jb_reset;
private JLabel jl_vserion;

/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Login1 inst = new Login1();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});

}

public Login1() {
super();
initGUI();
}

private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jl_UserCode = new JLabel();
getContentPane().add(jl_UserCode, "West");
jl_UserCode.setText("操作码");
jl_UserCode.setBounds(50, 40, 100, 22);

jt_UserCode = new JTextField();
getContentPane().add(jt_UserCode, "North");
//jt_UserCode.setText("UserCode");

jt_UserCode.setBounds(100, 40, 200, 22);

jl_UserName = new JLabel();
getContentPane().add(jl_UserName, "West");
jl_UserName.setText("姓 名");
jl_UserName.setBounds(50, 80, 100, 22);

jt_UserName = new JTextField();
getContentPane().add(jt_UserName, "North");
//jt_UserName.setText("UserCode");
jt_UserName.setEditable(false);
jt_UserName.setBounds(100, 80, 200, 22);

jl_Password = new JLabel();
getContentPane().add(jl_Password, "West");
jl_Password.setText("口 令");
jl_Password.setBounds(50, 120, 100, 22);

jp_Password = new JPasswordField();
getContentPane().add(jp_Password, "North");
jp_Password.setText("Password");
jp_Password.setBounds(100, 120, 200, 22);

jb_login = new JButton();
getContentPane().add(jb_login, "North");
jb_login.setText("登录");
jb_login.setBounds(100, 160, 80, 22);

jb_reset = new JButton();
getContentPane().add(jb_reset, "North");
jb_reset.setText("重置");
jb_reset.setBounds(200, 160, 80, 22);

jl_vserion = new JLabel();
getContentPane().add(jl_vserion, "West");
jl_vserion.setText("CopyRight 2009");
jl_vserion.setBounds(150, 200, 100, 22);

jt_UserCode.addFocusListener(new FocusListener(){
//组件失去焦点时调用的方法
public void focusLost(FocusEvent arg0) {

//JOptionPane.showMessageDialog(null, "文本框失去焦点");
jt_UserName.setText(Dao.GetName(jt_UserCode.getText().trim()));
}
//组件获取键盘焦点时调用的方法
public void focusGained(FocusEvent arg0) {
}
});
jp_Password.addFocusListener(new FocusListener(){
//组件失去焦点时调用的方法
public void focusLost(FocusEvent arg0) {
//JOptionPane.showMessageDialog(null, "文本框失去焦点");
}
//组件获取键盘焦点时调用的方法
public void focusGained(FocusEvent arg0) {
jp_Password.setText("");
}
});
//密码框响应回车事件
jp_Password.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent e) {
if(e.getKeyChar()=='/n')
jb_login.doClick();
}
});

jb_login.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(jt_UserCode.getText().trim().length()==0||jp_Password.getText().trim().length()==0){
JOptionPane.showMessageDialog(null, "用户名密码不允许为空");
return;
}
if(Dao.LoginCheck(jt_UserCode.getText().trim(), jp_Password.getText().trim())){
setVisible(false);
JOptionPane.showMessageDialog(null, "登录成功");
//MainFrame.main(null);
MainFrame m=new MainFrame();
m.main(null);


}else{
JOptionPane.showMessageDialog(null, "用户名密码错误");
}
}
});

jb_reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
jt_UserCode.setText("");
jt_UserName.setText("");
jp_Password.setText("");
}
});

}
setTitle("ERP系统登录窗口");
pack();
setSize(400, 300);

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

}

//DAO

package com.shat.dao;
import java.lang.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Dao {

protected static String dbClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
protected static String dbUrl = "jdbc:microsoft:sqlserver://localhost:1433;"
+ "DatabaseName=MyErp;SelectMethod=Cursor";
protected static String dbUser = "sa";
protected static String dbPwd = "SVTEP";
protected static String second = null;
public static Connection conn = null;
static {
try {
if (conn == null) {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
}
} catch (Exception ee) {
ee.printStackTrace();
}
}
private Dao() {
}

public static String GetName(String UserCode){
try{
Statement stmt=conn.createStatement();
String sql="select * from [User] where UserCode='"+UserCode+"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
return rs.getString("UserName");
}else{
return "账号不存在";
}
}catch(Exception ex){
System.err.println(ex.getMessage());
return "连接数据错误";
}
}
public static boolean LoginCheck(String UserCode,String password){
try{
Statement stmt=conn.createStatement();
String sql="select * from [User] where UserCode='"+UserCode+"' and Password='"+password+"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
return true;
}else{
return false;
}
}catch(Exception ex){
System.err.println(ex.getMessage());
return false;
}
}
}

你可能感兴趣的:(java)