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. could not build module 'XXXXXXXX'或者error: expected identifier or '(' 。一堆奇怪的错误————错误根源

    一堆奇怪的错误:1⃣️could not build module 'XXXXXXXX' 2⃣️error: expected identifier or '(' 3⃣️EDIT Setting Pr ...

  2. kendo-ui学习笔记(一)

    1.top.jsp: <script src="<%=path%>/kendoui/js/jquery.min.js"></script> &l ...

  3. Linux网络编程的一般步骤(1)

    一.套接字的地址结构. IPV4套接字地址结构通常也称为"网际套接字地址结构",它以sockaddr_in 命名;POSIX定义如下: #include <stdio.h&g ...

  4. Objective-C Memory Management

    Objective-C Memory Management Using Reference Counting 每一个从NSObject派生的对象都继承了对应的内存管理的行为.这些类的内部存在一个称为r ...

  5. 第五篇——C++实现四则运算

    写一个能自动生成小学四则运算题目的命令行 “软件”, 分别满足下面的各种需求.下面这些需求都可以用命令行参数的形式来指定: a) 除了整数以外,还要支持真分数的四则运算. (例如: 1/6 + 1/8 ...

  6. java开发环境

    java开发环境搭建   文中主要内容来自:http://blog.csdn.net/cxwen78/article/details/6400798 .文章对原文有所改动. 1. 开发工具获取 开发工 ...

  7. 使用恶意USB设备解锁 Windows & Mac 锁屏状态

    NSA专业物理入侵设备——USB Armory,可解锁任意锁屏状态的下的Windows和Mac操作系统,含最新发布的Windows10.及较早的Mac OSX El Capitan / Maveric ...

  8. SAPCAR 压缩解压软件的使用方法

    SAPCAR 是 SAP 公司使用的压缩解压软件,从 SAP 网站下载的补丁包和小型软件基本都是扩展名为 car 或 sar 的,它们都可以用 SAPCAR 来解压.下面是它的使用说明: 用法: 创建 ...

  9. MySQL分布式集群之MyCAT(转)

    原文地址:http://blog.itpub.net/29510932/viewspace-1664499/ 隔了好久,才想起来更新博客,最近倒腾的数据库从Oracle换成了MySQL,研究了一段时间 ...

  10. Redis的简介与安装(windows)

    1.简介 Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串). list(链表).set(集合).zset(sorte ...