Java项目

发布于 2017-04-26  8.95k 次阅读


test.java

===============================================

//package com.kunpeng.test;
//
//import com.sykp.jdbc.SQLHelper;
//
//public class test {
// public static void main(String[] args) {
// SQLHelper sql = new SQLHelper();
// sql.open();
// sql.close();
// }
//}

package com.kunpeng.test;

import com.sykp.jdbc.SQLHelper;
import com.sykp.md5.Md5;

public class test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SQLHelper sql = new SQLHelper();
sql.ip="192.168.25.31";
sql.open();
System.out.println("打开成功");
sql.close();
System.out.println("关闭成功");
System.out.println(Md5.getMD5("admin"));
}

}

===============================================

Eshop.java

package com.sykp.eshop;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import com.sykp.jdbc.SQLHelper;
import com.sykp.md5.Md5;

public class Eshop {
static SQLHelper sql = new SQLHelper();
static String logUser="未登录";

/**
* @param input
*/
public static void main(String[] args) {

while(true){

// 打印出主菜单
Menu.print01();
Scanner input = new Scanner(System.in);
String flag = input.nextLine();

if(flag.equals("1")){
//1是注册
reg();
//注册结束==========
}else if (flag.equals("2")){
//2是登陆
log();
//登陆结束=============
main2();
}else if (flag.equals("0")){
System.out.println("程序成功退出!");
return;
}else{
System.out.println("有错");
}
}
}

private static void main2() {
Scanner input = new Scanner(System.in);
while(true){
Menu.print02();
String inStr= input.next();
if(inStr.equals("1")){
Scanner input2 = new Scanner(System.in);
Menu.print02_1();
String in = input2.next();
//break;
}else if (inStr.equals("2")){
Scanner input3 = new Scanner(System.in);
Menu.print02_2();
String in = input3.next();
}else if (inStr.equals("3")){
Scanner input4 = new Scanner(System.in);
Menu.print02_3();
String in = input4.next();
}else if (inStr.equals("9")){
return;
}else if (inStr.equals("0")){
System.out.println("成功退出");
System.exit(0);
}else{
Scanner input5 = new Scanner(System.in);
System.out.println("输入有误,请重新输入");
if(inStr.equals("1")){
Scanner input2 = new Scanner(System.in);
Menu.print02_1();
String in = input2.next();
//break;
}else if (inStr.equals("2")){
Scanner input3 = new Scanner(System.in);
Menu.print02_2();
String in = input3.next();
}else if (inStr.equals("3")){
Scanner input4 = new Scanner(System.in);
Menu.print02_3();
String in = input4.next();
}else if (inStr.equals("9")){
return;
}else if (inStr.equals("0")){
System.out.println("成功退出");
System.exit(0);
continue;
}
}
}

}
private static void log() {
int logCount=0;
while(logCount<3){

Scanner input1 = new Scanner(System.in);
System.out.print("请输入用户名:");
String userName = input1.next();
System.out.print("请输入密码:");
String passworld = input1.next();
//1.1.1去前后空格
userName = userName.trim();
passworld = passworld.trim();
if(userName.length()<2 || passworld.length()<2)
{
System.out.println("用户名或密码长度不符合要求");
System.out.println("请重输:");
continue;
}
//去数据库检测是否能登陆成功
sql.open();
ResultSet re = sql.select("select count(*) as rcount from e_user where name='"+userName+"' and pwd='"+Md5.getMD5(passworld)+"'");
int rows = 0;
try {
while(re.next()){
rows=re.getInt("rcount");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sql.close();
if(rows!=1){
System.out.println("登陆不成功!你还有"+(3-logCount-1)+"次机会");
}else{
System.out.println("登录成功");
logUser=userName;
break;
}
logCount++;
}//登录三次循环到这里
if(logCount >=3){
System.out.println("登陆错误超过三次登陆程序退出");
System.exit(0);
}
}
private static void reg() {
Scanner input = new Scanner(System.in);
while(true){
System.out.print("请输入用户名:");
String userName = input.next();
System.out.print("请输入密码:");
String passworld = input.next();

//处理及判断是否合法
//1.1.1去前后空格
userName = userName.trim();
passworld = passworld.trim();
//1.1.2.判断是否合法,我们没学正则,暂时判断他的长度
if(userName.length()<2 || passworld.length()<2)
{
System.out.println("用户名或密码长度不符合要求");
System.out.println("请重输:");
continue;//跳出本次循环,继续下一次
}
//1.1.3判断用户名是否存在
sql.open();
ResultSet re = sql.select("select count(*) as rcount from e_user where name='"+userName+"'");
int rows = 0;
try {
while(re.next()){
rows=re.getInt("rcount");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sql.close();
if(rows>0){
System.out.println("用户名已存在!请重输");
continue;
}
//1.2把密码进行加密
passworld = Md5.getMD5(passworld);
//1.3把用户与密码写入数据库并提示成功.

sql.dataName = "e_shop";
sql.open();
sql.update("insert into e_user(name,pwd) values('"+userName+"','"+passworld+"')");
sql.close();
System.out.println("注册成功");
break;
}
}

private static void SQLHelper() {
// TODO Auto-generated method stub

}

}

===============================================

Menu.java

package com.sykp.eshop;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import com.sykp.jdbc.SQLHelper;
import com.sykp.md5.Md5;

public class Eshop {
static SQLHelper sql = new SQLHelper();
static String logUser="未登录";

/**
* @param input
*/
public static void main(String[] args) {

while(true){

// 打印出主菜单
Menu.print01();
Scanner input = new Scanner(System.in);
String flag = input.nextLine();

if(flag.equals("1")){
//1是注册
reg();
//注册结束==========
}else if (flag.equals("2")){
//2是登陆
log();
//登陆结束=============
main2();
}else if (flag.equals("0")){
System.out.println("程序成功退出!");
return;
}else{
System.out.println("有错");
}
}
}

private static void main2() {
Scanner input = new Scanner(System.in);
while(true){
Menu.print02();
String inStr= input.next();
if(inStr.equals("1")){
Scanner input2 = new Scanner(System.in);
Menu.print02_1();
String in = input2.next();
//break;
}else if (inStr.equals("2")){
Scanner input3 = new Scanner(System.in);
Menu.print02_2();
String in = input3.next();
}else if (inStr.equals("3")){
Scanner input4 = new Scanner(System.in);
Menu.print02_3();
String in = input4.next();
}else if (inStr.equals("9")){
return;
}else if (inStr.equals("0")){
System.out.println("成功退出");
System.exit(0);
}else{
Scanner input5 = new Scanner(System.in);
System.out.println("输入有误,请重新输入");
if(inStr.equals("1")){
Scanner input2 = new Scanner(System.in);
Menu.print02_1();
String in = input2.next();
//break;
}else if (inStr.equals("2")){
Scanner input3 = new Scanner(System.in);
Menu.print02_2();
String in = input3.next();
}else if (inStr.equals("3")){
Scanner input4 = new Scanner(System.in);
Menu.print02_3();
String in = input4.next();
}else if (inStr.equals("9")){
return;
}else if (inStr.equals("0")){
System.out.println("成功退出");
System.exit(0);
continue;
}
}
}

}
private static void log() {
int logCount=0;
while(logCount<3){

Scanner input1 = new Scanner(System.in);
System.out.print("请输入用户名:");
String userName = input1.next();
System.out.print("请输入密码:");
String passworld = input1.next();
//1.1.1去前后空格
userName = userName.trim();
passworld = passworld.trim();
if(userName.length()<2 || passworld.length()<2)
{
System.out.println("用户名或密码长度不符合要求");
System.out.println("请重输:");
continue;
}
//去数据库检测是否能登陆成功
sql.open();
ResultSet re = sql.select("select count(*) as rcount from e_user where name='"+userName+"' and pwd='"+Md5.getMD5(passworld)+"'");
int rows = 0;
try {
while(re.next()){
rows=re.getInt("rcount");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sql.close();
if(rows!=1){
System.out.println("登陆不成功!你还有"+(3-logCount-1)+"次机会");
}else{
System.out.println("登录成功");
logUser=userName;
break;
}
logCount++;
}//登录三次循环到这里
if(logCount >=3){
System.out.println("登陆错误超过三次登陆程序退出");
System.exit(0);
}
}
private static void reg() {
Scanner input = new Scanner(System.in);
while(true){
System.out.print("请输入用户名:");
String userName = input.next();
System.out.print("请输入密码:");
String passworld = input.next();

//处理及判断是否合法
//1.1.1去前后空格
userName = userName.trim();
passworld = passworld.trim();
//1.1.2.判断是否合法,我们没学正则,暂时判断他的长度
if(userName.length()<2 || passworld.length()<2)
{
System.out.println("用户名或密码长度不符合要求");
System.out.println("请重输:");
continue;//跳出本次循环,继续下一次
}
//1.1.3判断用户名是否存在
sql.open();
ResultSet re = sql.select("select count(*) as rcount from e_user where name='"+userName+"'");
int rows = 0;
try {
while(re.next()){
rows=re.getInt("rcount");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sql.close();
if(rows>0){
System.out.println("用户名已存在!请重输");
continue;
}
//1.2把密码进行加密
passworld = Md5.getMD5(passworld);
//1.3把用户与密码写入数据库并提示成功.

sql.dataName = "e_shop";
sql.open();
sql.update("insert into e_user(name,pwd) values('"+userName+"','"+passworld+"')");
sql.close();
System.out.println("注册成功");
break;
}
}

private static void SQLHelper() {
// TODO Auto-generated method stub

}

}

===============================================

SQLHelper.java