码迷,mamicode.com
首页 > 编程语言 > 详细

java 登录注册小程序

时间:2017-05-26 00:56:02      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:命名   ati   value   账户   invoke   ram   dad   成功   equals   

Blog系统的操作步骤

下面安装包的下载地址

https://pan.baidu.com/s/1cMqDK6

提取码cyqd

首先安装jdk;;;下面这个网址是叫你如何安装jdk

https://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html

安装eclipse  下面这个教程是叫你如何安装eclipse

https://jingyan.baidu.com/article/d7130635194f1513fcf47557.html

安装连接数据库的组件

http://www.cnblogs.com/Angella/p/6878632.html

 安装mysql

http://www.imooc.com/video/1803

 

开始〉〉运行    输入cmd

输入下面的语句

Mysql –uroot –p –P3306 –h127.0.0.1      (-u用户名,我的用户名是root,所以我输入的是-uroot,密码在Enter password那里输入,-P端口号,-h服务器名称,输入好后按enter)

技术分享

现在就登陆进mysql

创建一个数据库

语句如下:

create database BlogLogin;

use BlogLogin;

create table client

(

account varchar(10) primary key not null,

password varchar(20) not null

);

desc client;

 

技术分享

 

插入数据

写入以下代码:

Insert into client

Values(‘12345’,’a54321’);

Insert into client

Values(‘angella’,’yan771018’);

 

查看所有用户

Select * from client;

技术分享

打开eclipse

File>new >java project

技术分享

技术分享

我这里命名为blog    点击>>finish

右击blog>>new >>package

技术分享

技术分享

我这里也是命名为blog   >>finish

 

技术分享

技术分享

命名按我这个BlogFrame来,因为如果你命名的跟我的不一样,直接复制代码会发生错误。

〉〉finish

技术分享

public class BlogFrame{} 删掉;

把以下代码复制进去

 

import java.awt.EventQueue;

 

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

 

publicclass BlogFrame extends JFrame {

  /**

   * author vicky yan

   */

  privatestaticfinallongserialVersionUID = 1L;

  String str1 = null;

  String str2 = null;

 

  JLabel label1 = new JLabel("用户名:");

  JLabel label2 = new JLabel("     :");

  JTextField tf1 = new JTextField(20);// 填写框的长度

  JTextField tf2 = new JTextField(20);

  // 按钮

  JButton bt1 = new JButton("登陆");

  JButton bt2 = new JButton("取消");

  JButton bt3 = new JButton("注册");

 

  public BlogFrame() {

     init();

  }

 

  publicvoid init() {

     this.setTitle("Blog version 0.1");

 

     this.setBounds(300, 300, DEFULAT_WIDTH, DEFULAT_HEIGHT);

 

     bt1.addActionListener(new ActionListener() {

       publicvoid actionPerformed(ActionEvent e) {

         str1 = tf1.getText();

         str2 = tf2.getText();

         new BlogDB().check(str1, str2);

         tf1.setText("");

         tf2.setText("");

       }

     });

 

     bt2.addActionListener(new ActionListener() {

       publicvoid actionPerformed(ActionEvent e) {

         System.exit(0);

       }

     });

     bt3.addActionListener(new ActionListener() {

       publicvoid actionPerformed(ActionEvent e) {

         str1 = tf1.getText();

         str2 = tf2.getText();

         new BlogDB().addAccount(str1, str2);

 

         tf1.setText("");

         tf2.setText("");

       }

     });

 

     JPanel p1 = new JPanel();

     JPanel p2 = new JPanel();

     JPanel p3 = new JPanel();

     p1.add(label1);

     p1.add(tf1);

     p2.add(label2);

     p2.add(tf2);

     p3.add(bt1);

     p3.add(bt2);

     p3.add(bt3);

 

     this.setLayout(new GridLayout(3, 1));

     this.add(p1);

     this.add(p2);

     this.add(p3);

     this.pack();

  }

 

  publicstaticvoid main(String[] args) {

 

     EventQueue.invokeLater(new Runnable() {

       publicvoid run() {

         JFrame frame = new BlogFrame();

         frame.setVisible(true);

         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       }

     });

  }

 

  privatestaticfinalintDEFULAT_WIDTH = 150;

  privatestaticfinalintDEFULAT_HEIGHT = 80;

}

 

class BlogDB {

  Connection conn = null;

  PreparedStatement ps = null;

  Statement stmt = null;

  ResultSet res = null;

  String str1 = null;

  String str2 = null;

  String url = "jdbc:mysql://localhost:3306/BlogLogin?user=root&password=root";

 

  // 连接数据库的urlBlogLogin是我自己的一个数据库.//输入你自己的用户名和密码我的用户名和密码都是root

  public BlogDB() {

     init();

  }

 

  publicvoid init() {

     try {

       Class.forName("com.mysql.jdbc.Driver");

       conn = DriverManager.getConnection(url);

       System.out.println("恭喜你,你已经成功连接数据库*——*");

     } catch (SQLException e) {

       e.printStackTrace();

     } catch (ClassNotFoundException e) {

       e.printStackTrace();

     }

  }

 

  // 把从文本框里面取的值传进来和数据库里面的对比

  publicvoid check(String st1, String st2) {

     this.str1 = st1;

     this.str2 = st2;

     try {

       // 当用户名= client表中搜索密码是否正确

       String sql = "select  password  from  client  where account= ‘"

            + str1 + "‘";

       stmt = conn.createStatement();// Statement

       res = stmt.executeQuery(sql);

       if (res.next()) {// 直接判断有木有结果集

         System.out.println("您的账户是存在的。");

         if (str2.equals(res.getString("password"))) {

            System.out.println("您输入的密码正确!");

         } else {

            System.out.println("很遗憾,你输入的密码不正确");

         }

       }

     } catch (SQLException e) {

       e.printStackTrace();

     } finally {

       try {

         stmt.close();

         res.close();

         conn.close();

       } catch (SQLException e) {

         e.printStackTrace();

       }

 

     }

  }

 

  publicvoid addAccount(String str3, String str4) {

     // 插入数据

     String sql2 = "insert into  client(account,password) values(?,?)";

     try {

       ps = conn.prepareStatement(sql2);

       ps.setString(1, str3);// 跟数据库类型相对应

       ps.setString(2, str4);

       int resultInt = ps.executeUpdate();

       if (resultInt >= 0)

 

         System.out.println("您的账号已注册成功哈哈哈^_^");// 一般不报错就能成功,可以不加判断。

     } catch (SQLException e) {

       e.printStackTrace();

 

     } finally {

       try {

         ps.close();

         conn.close();

       } catch (SQLException e) {

         e.printStackTrace();

       }

 

     }

 

  }

 

}

按如图箭头所指的绿色箭头。技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

 

。。

Blog系统的操作步骤

下面安装包的下载地址

https://pan.baidu.com/s/1cMqDK6

提取码cyqd

首先安装jdk;;;下面这个网址是叫你如何安装jdk

https://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html

安装eclipse  下面这个教程是叫你如何安装eclipse

https://jingyan.baidu.com/article/d7130635194f1513fcf47557.html

安装连接数据库的组件

http://www.cnblogs.com/Angella/p/6878632.html

 安装mysql

http://www.imooc.com/video/1803

 

开始〉〉运行    输入cmd

输入下面的语句

Mysql –uroot –p –P3306 –h127.0.0.1      (-u用户名,我的用户名是root,所以我输入的是-uroot,密码在Enter password那里输入,-P端口号,-h服务器名称,输入好后按enter)

技术分享

现在就登陆进mysql

创建一个数据库

语句如下:

create database BlogLogin;

use BlogLogin;

create table client

(

account varchar(10) primary key not null,

password varchar(20) not null

);

desc client;

 

技术分享

 

插入数据

写入以下代码:

Insert into client

Values(‘12345’,’a54321’);

Insert into client

Values(‘angella’,’yan771018’);

 

查看所有用户

Select * from client;

技术分享

打开eclipse

File>new >java project

技术分享

技术分享

我这里命名为blog    点击>>finish

右击blog>>new >>package

技术分享

技术分享

我这里也是命名为blog   >>finish

 

技术分享

技术分享

命名按我这个BlogFrame来,因为如果你命名的跟我的不一样,直接复制代码会发生错误。

〉〉finish

技术分享

public class BlogFrame{} 删掉;

把以下代码复制进去

 

import java.awt.EventQueue;

 

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

 

publicclass BlogFrame extends JFrame {

  /**

   * author vicky yan

   */

  privatestaticfinallongserialVersionUID = 1L;

  String str1 = null;

  String str2 = null;

 

  JLabel label1 = new JLabel("用户名:");

  JLabel label2 = new JLabel("     :");

  JTextField tf1 = new JTextField(20);// 填写框的长度

  JTextField tf2 = new JTextField(20);

  // 按钮

  JButton bt1 = new JButton("登陆");

  JButton bt2 = new JButton("取消");

  JButton bt3 = new JButton("注册");

 

  public BlogFrame() {

     init();

  }

 

  publicvoid init() {

     this.setTitle("Blog version 0.1");

 

     this.setBounds(300, 300, DEFULAT_WIDTH, DEFULAT_HEIGHT);

 

     bt1.addActionListener(new ActionListener() {

       publicvoid actionPerformed(ActionEvent e) {

         str1 = tf1.getText();

         str2 = tf2.getText();

         new BlogDB().check(str1, str2);

         tf1.setText("");

         tf2.setText("");

       }

     });

 

     bt2.addActionListener(new ActionListener() {

       publicvoid actionPerformed(ActionEvent e) {

         System.exit(0);

       }

     });

     bt3.addActionListener(new ActionListener() {

       publicvoid actionPerformed(ActionEvent e) {

         str1 = tf1.getText();

         str2 = tf2.getText();

         new BlogDB().addAccount(str1, str2);

 

         tf1.setText("");

         tf2.setText("");

       }

     });

 

     JPanel p1 = new JPanel();

     JPanel p2 = new JPanel();

     JPanel p3 = new JPanel();

     p1.add(label1);

     p1.add(tf1);

     p2.add(label2);

     p2.add(tf2);

     p3.add(bt1);

     p3.add(bt2);

     p3.add(bt3);

 

     this.setLayout(new GridLayout(3, 1));

     this.add(p1);

     this.add(p2);

     this.add(p3);

     this.pack();

  }

 

  publicstaticvoid main(String[] args) {

 

     EventQueue.invokeLater(new Runnable() {

       publicvoid run() {

         JFrame frame = new BlogFrame();

         frame.setVisible(true);

         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       }

     });

  }

 

  privatestaticfinalintDEFULAT_WIDTH = 150;

  privatestaticfinalintDEFULAT_HEIGHT = 80;

}

 

class BlogDB {

  Connection conn = null;

  PreparedStatement ps = null;

  Statement stmt = null;

  ResultSet res = null;

  String str1 = null;

  String str2 = null;

  String url = "jdbc:mysql://localhost:3306/BlogLogin?user=root&password=root";

 

  // 连接数据库的urlBlogLogin是我自己的一个数据库.//输入你自己的用户名和密码我的用户名和密码都是root

  public BlogDB() {

     init();

  }

 

  publicvoid init() {

     try {

       Class.forName("com.mysql.jdbc.Driver");

       conn = DriverManager.getConnection(url);

       System.out.println("恭喜你,你已经成功连接数据库*——*");

     } catch (SQLException e) {

       e.printStackTrace();

     } catch (ClassNotFoundException e) {

       e.printStackTrace();

     }

  }

 

  // 把从文本框里面取的值传进来和数据库里面的对比

  publicvoid check(String st1, String st2) {

     this.str1 = st1;

     this.str2 = st2;

     try {

       // 当用户名= client表中搜索密码是否正确

       String sql = "select  password  from  client  where account= ‘"

            + str1 + "‘";

       stmt = conn.createStatement();// Statement

       res = stmt.executeQuery(sql);

       if (res.next()) {// 直接判断有木有结果集

         System.out.println("您的账户是存在的。");

         if (str2.equals(res.getString("password"))) {

            System.out.println("您输入的密码正确!");

         } else {

            System.out.println("很遗憾,你输入的密码不正确");

         }

       }

     } catch (SQLException e) {

       e.printStackTrace();

     } finally {

       try {

         stmt.close();

         res.close();

         conn.close();

       } catch (SQLException e) {

         e.printStackTrace();

       }

 

     }

  }

 

  publicvoid addAccount(String str3, String str4) {

     // 插入数据

     String sql2 = "insert into  client(account,password) values(?,?)";

     try {

       ps = conn.prepareStatement(sql2);

       ps.setString(1, str3);// 跟数据库类型相对应

       ps.setString(2, str4);

       int resultInt = ps.executeUpdate();

       if (resultInt >= 0)

 

         System.out.println("您的账号已注册成功哈哈哈^_^");// 一般不报错就能成功,可以不加判断。

     } catch (SQLException e) {

       e.printStackTrace();

 

     } finally {

       try {

         ps.close();

         conn.close();

       } catch (SQLException e) {

         e.printStackTrace();

       }

 

     }

 

  }

 

}

按如图箭头所指的绿色箭头。技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

。。

java 登录注册小程序

标签:命名   ati   value   账户   invoke   ram   dad   成功   equals   

原文地址:http://www.cnblogs.com/Angella/p/6906314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!