注册,登陆,删除账户模块

 刚写的三个模块,欢迎大家指正!

//注册模块
package cn.bless_remind;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Array;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class RegisterDemo
{

 public static void main(String[] args) throws ClassNotFoundException
 {
  try
  {
   System.out.print("请输入要注册的用户名:");
   BufferedReader input1 = new BufferedReader(new InputStreamReader(System.in));
   String name = input1.readLine();
   System.out.print("请输入要注册的密码:");
   BufferedReader input2 = new BufferedReader(new InputStreamReader(System.in));
   String password = input2.readLine();
   register(name,password);
   input1.close();
   input2.close();
  } catch (IOException e)
  {
   e.printStackTrace();
  }
  
 }

 public static void register(String name,String password) throws ClassNotFoundException
 {
  try
  {
   Class.forName("com.mysql.jdbc.Driver");
   Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/itcast","root","");
   Statement statement = (Statement) connection.createStatement();
   ResultSet result = (ResultSet) statement.executeQuery("select name from student where name="+"'"+name+"'");
      if(result.next())
      {
       System.out.print("你输入的用户名已被注册!");
       
      }
      else
      {
       statement.execute("insert into student(name,password) values"+"("+"'"+name+"'"+","+"'"+password+"'"+")");
    System.out.print("注册成功!");
      }
      connection.close();
      result.close();
      statement.close();
  } catch (SQLException e)
  {
   e.printStackTrace();
  }
 }

}

//登陆模块

package cn.bless_remind;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class LoginDemo
{
 public static void main(String[] args)
 {
  try
  {
   System.out.print("请输入用户名:");
   BufferedReader bfReader = new BufferedReader(new InputStreamReader(System.in));
   String name = bfReader.readLine();
   System.out.println();
   System.out.print("请输入密码:");
   BufferedReader bfReader2 = new BufferedReader(new InputStreamReader(System.in));
   String password =  bfReader2.readLine();
   login(name,password);
   bfReader.close();
   bfReader2.close();
  } catch (IOException e)
  {
   e.printStackTrace();
  }
  
 }
 
 public static void  login(String name,String password)
 {
  try
  {
   Class.forName("com.mysql.jdbc.Driver");
   Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/itcast","root","");
   Statement statement = (Statement) connection.createStatement();
      ResultSet result = (ResultSet) statement.executeQuery("select * from student where name='"+name+"' and password='"+password+"'");
   
   if(result.next())
   {
    System.out.print("欢迎!"+name);
   }
   else
   {
    System.out.print("您输入的用户名不存在或密码错误!");
   }
   statement.close();
   connection.close();
   result.close();
  } catch (SQLException e)
  {
   e.printStackTrace();
  } catch (ClassNotFoundException e)
  {
   e.printStackTrace();
  }
 }

}


//账户删除模块
package cn.bless_remind;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class DestoryRegister
{

 public static void main(String[] args)
 {
  try
  {
   System.out.print("请输入要删除账户的用户名:");
   BufferedReader input1 = new BufferedReader(new InputStreamReader(System.in));
   String name = input1.readLine();
   System.out.print("请输入密码:");
   BufferedReader input2 = new BufferedReader(new InputStreamReader(System.in));
   String password = input2.readLine();
   destoryRegister(name,password);
   input1.close();
   input2.close();
  } catch (IOException e)
  {
   e.printStackTrace();
  }
 }
 
 public static void destoryRegister(String name,String password)
 {
  try
  {
   Class.forName("com.mysql.jdbc.Driver");
   Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/itcast","root","");
   Statement statement =(Statement) connection.createStatement();
   ResultSet result = (ResultSet) statement.executeQuery("select name from student where name="+"'"+name+"'"+"and password="+"'"+password+"'");
   if(result.next())
   {
    statement.execute("delete from student where name="+"'"+name+"'");
    System.out.print("删除成功!");
   }
   else
   {
    System.out.print("你输入的用户名或密码有误!");
   }
   connection.close();
   statement.close();
   result.close();
  } catch (ClassNotFoundException e)
  {
   e.printStackTrace();
  } catch (SQLException e)
  {
   e.printStackTrace();
  }
 }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值