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. C++Primer 一

    1.vertor和数组的区别: 数组的长度固定.而且程序是无法知道一个给定数组的长度,数组没有获取器容量大小的size操作,也不提供puch_back操作在其中自动添加元素 2.数组定义中的类型可以示 ...

  2. nginx+tomcat+dubbo单机部署多台dubbo应用

    前面的博客已经介绍如何使用nginx+tomcat,今天做的是如何在单台服务器上如何部署多台dubbo 应用的集群. 由于在项目中遇到了这个问题,今天就把它记录下来. 1.

  3. sql转db,后台坑货

    打开 创建一个db文件然后点击文件--新建---Sqlite 导入空db成功后点击左侧栏 点击表 点击右上角+号把sql文件的语句复制粘贴到 然后点击运行,运行完成后保存ok

  4. 在web.config配置中添加xml内容

    在web.config 中添加需要的内容时, 就是在<configuration>节点内添加一个新的<configSections>元素, 在configSections元素中 ...

  5. RSA_SHA256数字签名

    ------------------ rsa 加密与解密 ----------------------------- 数论知识的实际应用: rsa加密 把问题归结为对数字的加密. 被加密的数字为明文. ...

  6. HDU 4691 正解后缀数组(暴力也能过)

    本来是个后缀数组,考察算法的中级题目,暴力居然也可以水过,就看你跳不跳坑了(c++和G++返回结果就很不一样,关键看编译器) 丝毫不差的代码,就看运气如何了.唯一差别c++还是G++,但正解是后缀数组 ...

  7. system_call中断处理过程分析

    本文所有的分析内容都是基于Linux3.18.6内核,鉴于对应不同内核版本,系统调用的实现不相同.若需要分析其他版本内核的系统调用的实现过程,请谨慎参考. system_call函数的功能是用来响应外 ...

  8. linux工作用到的

    SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议. SSH 是目前较可靠, ...

  9. git 较基础命令

    还需要进一步了解git的组织形式: git clone *.git 下载下来以git方式管理 如果直接下载压缩包做不到 git branch 分支相关命令 git checkout 可以换分支 git ...

  10. php组合

    为了提高代码的复用性,降低代码的耦合(组合实现的两种方式) 模式一: <?php //组合模式一 class Person{ public function eat(){ echo " ...