刚写的三个模块,欢迎大家指正!
//注册模块
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();
}
}
}