package addresslist;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent; import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; /*
* 封装的太差啦!
* 要好好理解继承封装多态的使用
* 不然代码就没法看了
* 其实是后期修改太困难啦
* 通用模版的使用
* 代码的可复用性
* 考虑好了之后再动手敲代码
* 不然写了维护起来也比较困难最后只好放弃
*
* 然后就是代码的重构与优化
*
*/
public class login extends JFrame { private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
public Judge judge=new Judge();
static String username;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame fra = new login();
fra.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public void setall(String n,String p){
textField.setText(n);
passwordField.setText(p);
} public login() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new NewPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane); JLabel label = new JLabel("\u7528\u6237\u540D"); textField = new JTextField();
textField.setColumns(10); JLabel label_1 = new JLabel("*");
textField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
try {
String s=textField.getText().toString();
if(!judge.exist(s)){
label_1.setText("no such nickname!");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void focusGained(FocusEvent e) {
label_1.setText("*");
}
}); JLabel label_2 = new JLabel("\u5BC6\u7801"); passwordField = new JPasswordField(); JButton btnLogin = new JButton("login");
btnLogin.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==10){
btnLogin.doClick();
}
}
}); btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=new String(passwordField.getPassword());
String n=textField.getText().toString();
try {
if(judge.canlogin(n,s)){
username=n;
mainFrame frame=new mainFrame();
frame.setVisible(true);
dispose();
}
else{
JOptionPane.showMessageDialog(null, "密码或用户名错误");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}); JButton btnRegister = new JButton("register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
register rs=new register();
rs.setVisible(true);
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(66)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label)
.addComponent(label_2))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(passwordField)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
.addGap(28)
.addComponent(label_1))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(80)
.addComponent(btnLogin)
.addGap(37)
.addComponent(btnRegister)))
.addContainerGap(151, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(48)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(label_1)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(29)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(48)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnLogin)
.addComponent(btnRegister))
.addContainerGap(61, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
}
class NewPanel extends JPanel{
public NewPanel() {} public void paintComponent(Graphics g)
{
int x=0,y=0;
java.net.URL imgURL=getClass().getResource("login.jpg"); //test.jpg是测试图片,与Demo.java放在同一目录下
ImageIcon icon=new ImageIcon(imgURL);//this is really useful
g.drawImage(icon.getImage(),x,y,getSize().width,getSize().height,this);
while(true)
{
g.drawImage(icon.getImage(),x,y,this);
if(x>getSize().width && y>getSize().height)break;
//这段代码是为了保证在窗口大于图片时,图片仍能覆盖整个窗口
if(x>getSize().width)
{
x=0;
y+=icon.getIconHeight();
}
else
x+=icon.getIconWidth();
}
}
}
}
package addresslist;

import java.awt.BorderLayout;
import java.awt.EventQueue; import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton; import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent; public class register extends JFrame { private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
private JPasswordField passwordField_1;
public Judge judge=new Judge(); /**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
register frame = new register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public register() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane); JLabel label = new JLabel("\u7528\u6237\u540D"); textField = new JTextField();
textField.setColumns(10); JLabel label_1 = new JLabel("*");
textField.addFocusListener(new FocusAdapter() {
@Override public void focusGained(FocusEvent e) {
label_1.setText("*");
}
@Override
public void focusLost(FocusEvent e) {
try {
if(!judge.exist(textField.getText().toString())){
label_1.setText("is valiable");
}
else{
label_1.setText("the name is used!");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}); JLabel label_2 = new JLabel("\u5BC6\u7801"); passwordField = new JPasswordField(); JLabel label_3 = new JLabel("\u786E\u8BA4\u5BC6\u7801"); passwordField_1 = new JPasswordField();
JLabel label_4 = new JLabel("*");
passwordField_1.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
label_4.setText("*");
}
@Override
public void focusLost(FocusEvent e) {
if(new String(passwordField.getPassword()).equals(new String(passwordField_1.getPassword()))){
label_4.setText("*");
}
else{
label_4.setText("the password is not the same,please check it!");
}
}
}); JButton btnRegister = new JButton("register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { try {
if(!judge.exist(textField.getText().toString())){
String n=textField.getText().toString();
String p=new String(passwordField.getPassword());
judge.regist(n,p);
login lg=new login();
lg.setVisible(true);
lg.setall(n,p);
dispose();
}
else{
JOptionPane.showMessageDialog(null, "the name is exist please try another!");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} });
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(39)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(btnRegister)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(47)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(label_2)
.addComponent(label))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
.addComponent(passwordField)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(39)
.addComponent(label_3)
.addGap(18)
.addComponent(passwordField_1))))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label_1)
.addComponent(label_4))
.addContainerGap(199, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(46)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(26)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_3)
.addComponent(passwordField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_4))
.addGap(34)
.addComponent(btnRegister)
.addContainerGap(41, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
} }
package addresslist;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement; public class Judge {
public static void main(String[] args) throws Exception {
String url ="jdbc:mysql://localhost:3306/login"; //数据库连接字符串
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
Connection conn= (Connection) DriverManager.getConnection(url,"tan","tn5201314"); //建立连接
Statement stmt=(Statement) conn.createStatement(); //创建SQL容器
String sql="select * from user"; //表为teacher
ResultSet rs=stmt.executeQuery(sql); //获得结果集 while( rs.next() ) { //处理结果集
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getString("password")+"\n");
}
rs.close(); stmt.close(); conn.close(); //关闭次序 } public Connection getConnection()throws Exception{
String url ="jdbc:mysql://localhost:3306/login"; //数据库连接字符串
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
Connection conn= DriverManager.getConnection(url,"tan","tn5201314");
return conn;
}
public boolean exist(String s) throws Exception{
Connection conn= getConnection(); //建立连接
Statement stmt=conn.createStatement(); //创建SQL容器
String sql="select * from user where name='"+s+"'"; //表为user
ResultSet rs=stmt.executeQuery(sql); //获得结果集
if( rs.next() ) {
rs.close(); stmt.close(); conn.close();
return true;
}
else
{
rs.close(); stmt.close(); conn.close();
return false;
}
//关闭次序 } public boolean canlogin(String n,String p) throws Exception{
Connection conn=getConnection(); //建立连接
Statement stmt=conn.createStatement(); //创建SQL容器
String sql="select password from user where name='"+n+"'"; //表为user
ResultSet rs=stmt.executeQuery(sql); //获得结果集
if(rs.next()){
if(rs.getString("password").equals(p)){
rs.close(); stmt.close(); conn.close();
return true;
}
else{
rs.close(); stmt.close(); conn.close();
return false;
}
}
else{
rs.close(); stmt.close(); conn.close();
return false;
} } public void regist(String n,String p) throws Exception{
Connection conn=getConnection(); //建立连接
Statement stmt=conn.createStatement(); //创建SQL容器
String sql="insert into user values('"+n+"','"+p+"')"; //表为user
stmt.executeUpdate(sql);
stmt.close(); conn.close();
}
}

login的更多相关文章

  1. 浅谈SQL注入风险 - 一个Login拿下Server

    前两天,带着学生们学习了简单的ASP.NET MVC,通过ADO.NET方式连接数据库,实现增删改查. 可能有一部分学生提前预习过,在我写登录SQL的时候,他们鄙视我说:“老师你这SQL有注入,随便都 ...

  2. 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?

    p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...

  3. Day01 login module

    知识点:模块导入  变量赋值的两种形式  格式化输出  for循环  if...else 嵌套 #!C:\Program Files\Python35/bin # -*- conding:utf-8 ...

  4. Login控件尝试

    新建web项目,添加default.aspx.Register.aspx.Login.aspx. default.aspx中添加LoginName.LoginStatus,LoginName的Form ...

  5. [转载] 构造linux 系统下免密码ssh登陆  _How to establish password-less login with SSH

    In present (post production) IT infrastructure many different workstations, servers etc. have to be ...

  6. Security9:查询Login被授予的权限

    在给一个Login授予权限时,发现该Login已经存在,其对应的User也存在于指定的DB中,查看该Login在指定DB中已被授予的权限. 1,查看Login的Server PrincipalID s ...

  7. php中的登陆login

    Login <?php require "../include/DBClass.php"; $username=$_POST['UserName']; $password=$ ...

  8. MS SQL Could not obtain information about Windows NT group/user 'domain\login', error code 0x5. [SQLSTATE 42000] (Error 15404)

    最近碰到一个有趣的错误:海外的一台数据库服务器上某些作业偶尔会报错,报错信息如下所示: -------------------------------------------------------- ...

  9. MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems

          早晨宁波那边的IT人员打电话告知数据库无法访问了.其实我在早晨也发现Ignite监控下的宁波的数据库服务器出现了异常,但是当时正在检查查看其它服务器发过来的各类邮件,还没等到我去确认具体情 ...

  10. Spring Security笔记:自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken

    在前面的学习中,配置文件中的<http>...</http>都是采用的auto-config="true"这种自动配置模式,根据Spring Securit ...

随机推荐

  1. java1234初学maven

    第一讲: maven maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. maven安装与下载: .确定jdk已经安装并且配置 .安装mav ...

  2. OC基本数据存储方式

    /** 一,数据存储 常用方式(5种) 1,XML属性列表 -- 保存在Doucuments文件夹 2,偏好设置(NSUserDefault)-- Library/Preference 需要配合wri ...

  3. The Pragmatic Programmer Quick Reference Guide

    1.关心你的技艺 Care About Your Craft 如果不在乎能否漂亮地开发出软件,你又为何要耗费生命去开发软件呢? 2.思考!你的工作 Think! About Your Work 关掉自 ...

  4. K均值聚类算法的MATLAB实现

    1.K-均值聚类法的概述    之前在参加数学建模的过程中用到过这种聚类方法,但是当时只是简单知道了在matlab中如何调用工具箱进行聚类,并不是特别清楚它的原理.最近因为在学模式识别,又重新接触了这 ...

  5. SRS文档

    1什么是用例? 在介始用例方法之前,我们首先来看一下传统的需求表述方式-"软件需求规约"(Software Requirement Specification).传统的软件需求规约 ...

  6. Python学习之变量

    Python 变量 python不用事先声明变量,赋值过程中就包含了变量声明和定义的过程 用“=”赋值,左边是变量名,右边是变量的值 数字 整数 int_var = 1 长整数 long_var = ...

  7. C#中MessageBox用法大全

    我们在程序中经常会用到MessageBox. MessageBox.Show()共有21中重载方法.现将其常见用法总结如下: 1.MessageBox.Show("Hello~~~~&quo ...

  8. HDU 1863

    http://acm.hdu.edu.cn/showproblem.php?pid=1863 复习考研练练写Prim,第一次写,乱搞的,有点难看 邻接表+堆 #include <iostream ...

  9. 安装go语言,配置环境及IDE,只需3步

    安装go语言,配置环境及IDE,只需3步 ( 欢迎加入go语言群: 218160862 , 群内有实践) 第1.下载 go压缩包,解压   ,如果你是window系统,请选择go1.5.windows ...

  10. iOS Block循环引用

    在介绍block循环引用前我们先了解一下typeof. typeof是什么??? typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型. 它返回值是一个字符串,该字符串说明运算数的类 ...