正文之前
今天难得认真写了会代码,虽然还是有很多重用,以及巨多的逻辑不清楚,但是好歹基本的商城给我写出来了。而且还连接了数据库,各种操作也是6的飞起,整整690行代码,现在眼睛看屏幕都是双重的,看不清了。再不休息估计会猝死,可以预见未来自己的地中海了!!!今天换了个很清爽的背景,写这么勤快 杰西卡·阿尔芭功不可没!!
第一点:子类中调用基类的构造器
如果我们没有在子类中,给基类进行各类构造器的调用。那么它就会默认的调用基类的默认构造器。这很多时候会给我们带来困扰。因为我们从基类继承而来的子类。也希望能够调用基类的各类构造器而不仅仅是默认构造器。
下面的代码我自己定义了构造器, 所以应该是不存在默认构造器的。但是万一就存在了呢?现在看来把问题看严重了。其实没这么大的风险啊!!因为如果要传入参数的话,肯定就要调用基类的相应构造器啊!
class dog1{
private int x=0;
dog1(int s){
this.x=s;
System.out.println("Dog1 初始化!"+x);
}
void getX(){
System.out.println(x);
}
}
class dog2 extends dog1{
private int x1=1;
dog2(int a,int b){
super(a);
this.x1=b;
System.out.println("Dog2 初始化! "+x1);
}
void getX1(){
System.out.println(x1);
}
}
public class Puppy extends dog2{
private int x2=2;
Puppy(int a,int b,int c){
super(a,b);
this.x2=c;
System.out.println("Puppy 初始化!"+x2);
}
void getX2(){
System.out.println(x2);
}
public static void main(String[] args) {
Puppy s=new Puppy(100,101,102);
s.getX();
}
}
第二点:继承的方式问题
第三点:String的比较不能用== 和 !=
public void Change_the_Password(User The_user, String new_passwd) {
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (conn.isClosed())
System.out.println("Failed connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select id,name,password from User where name=' " + The_user.Return_User_Name() + " ' and password='" + new_passwd + " ' ";
String Check = "select password from User where name=' " + The_user.Return_User_Name() + " ' and id= "+this.getUser_ID()+" ";
ResultSet checkpasswd = statement.executeQuery(Check);
//此处要求我先走一步next(),果然,从next出现开始才开始读数,不然会报错!!
if(checkpasswd.next()) {
String oldps = checkpasswd.getString("password");
// System.out.println(oldps + "&&&&&&");
//老问题:String要用equals()才能比较,不能用== 会报错!!
if (oldps.equals(The_user.getPassword())) {
System.out.println("The Old Password is wrong !!!");
checkpasswd.close();
conn.close();
return;
}
setPassword(new_passwd);
checkpasswd.close();
String Change = "UPDATE User set password=' " + new_passwd + " ' where id="+The_user.getUser_ID()+"";
boolean rs1=statement.execute(Change);
if(!rs1) {
ResultSet rs = statement.executeQuery(sql);
System.out.println("\t\t----------------------------------");
System.out.println("*****************执行结果:*****************");
System.out.println("\t\t----------------------------------");
System.out.println(" \tID" + "\t\t" + " NAME" + "\t\t" + "New_Password");
System.out.println("\t\t----------------------------------");
int user_id = 0;
String name = " ";
String passwd = " ";
while (rs.next()) {
user_id = rs.getInt("id");
name = rs.getString("name");
passwd = rs.getString("password");
name = new String(name.getBytes("UTF-8"), "UTF-8");
System.out.println(user_id + "\t" + name + "\t" + passwd);
}
System.out.println("\t\t----------------------------------\n\n");
rs.close();
}
}
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
String的比较体现在这一行:
if (oldps.equals(The_user.getPassword())) {
如果你给一个oldps.equals==The_user.getPassword() !!!相信我,会一直是true的, 受害人现身说法你还不信????
第四点: JDBC连接的数据库版本够的话:
private static String driver = "com.mysql.jdbc.Driver";
//此处查看网络才知道。要求SSL,所以就酱紫咯:https://zhidao.baidu.com/question/2056521203295428667.html
private static String url = "jdbc:mysql://127.0.0.1:3306/Shop_User?useUnicode=true&characterEncoding=GBK&useSSL=true";
https://zhidao.baidu.com/question/2056521203295428667.html
如果后没有&useSSL=true,那就会每次连接数据库都有:
Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
我目前只能每次定义一个方法就连接一次数据库,也曾想过把数据库的链接作为一个User的属性,但是每次如果不try-catch的话就会GG,所以就将就着先在每个方法中链接一次,明天看看能不能提炼出来,不然感觉太臃肿了,而且代码效率简直GG!!!
第五点:字符串去掉首尾空格:
因为是数据库的出入,所以变得有些不受掌控了。这很烦!!所以有了一个补救措施:
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (conn.isClosed())
System.out.println("Failed connecting to the Database!");
Statement statement = conn.createStatement();
String Check = "select id,password from User where name=' " + User_name + " ' ";
ResultSet checkpasswd = statement.executeQuery(Check);
if(checkpasswd.next()) {
id = checkpasswd.getInt("id");
ps = checkpasswd.getString("password");
}
checkpasswd.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//从数据库中取出来的字符串带空格?
//******************************************
ps=ps.trim();
//******************************************
if (!passwd.equals(ps)) {
System.out.println("The Password is wrong !!!");
return false;
}
System.out.println("\n\n\t\t*********Welcome to Here !!!*********");
this.setUser_ID(id);
return true;
没错 ps=ps.trim();就是去除 首尾空格的,靠这个我才勉强苟活:D),睡了睡了。再不睡会死!!!!
对了。代码大放送(丢一个文件夹下就好):
/*
* 显示系统初始界面菜单
*/
//输入输出
import java.util.Scanner;
public class Shop_System{
public static void main (String[] args){
//显示初始登录界面
Menu menu = new Menu();
menu.InitMenu();
Scanner First = new Scanner(System.in);
System.out.print("\t Please Input Your Action: ");
int in = First.nextInt();
User user;
user = new User();
Goods goods=new Goods();
//不管登陆没登陆,先定一个用户实例,用户实例的具体内容请见另外一个文件,也就是一开始导入的Customor类
if(in==2){
boolean yes=menu.SignupMenu(user);
while(!yes){
yes=menu.SignupMenu(user);
}
}
else if(in==3)
{
System.out.println("\n\n\t\t 感谢光临我们的Shop System,期待与您下次相遇\n\n");
return;
}
while(!menu.LoginMenu(user)){
// user.getUser_Name();
System.out.println("密码错误,请重来!");
}
Scanner Op = new Scanner(System.in);
//当确定用户登录之后,进入主界面
/*
* 因为嫌麻烦,所以我直接定义一个主界面的类。
* 而不是定义一个方法。
* 好吧,我承认那会儿是我还才开始写,理不太新思路瞎写的。
* 后来发现用着蛮方便就没改了。
*/
//定义一个主界面的实例。然后当需要回到主界面的时候,直接调用实例的方法就可以了
int op=0;
//对输入的数据进行识别,然后去相对应的界面
while (op!=4) {
// System.out.println("\t\t Please Input: ");
menu.MainMenu();
op=Op.nextInt();
switch (op) {
case 1:
menu.Customer(user,goods);
//当对用户的管理界面结束的时候回到主界面
/*
* 进入用户设置界面的话。通过调用面板实例的相应方法,可以对用户信息进行管理
* 注意当进入这一个子页面的时候,就已经进入了用户实例的管理界面。
* 当然还是在主函数中,只不过是调用了Menu的方法而已。
*/
break;
case 2:
menu.Buy_Goods(user);
//如果Buy不自己停止的话会一直循环的哦
//回到主界面
break;
case 3:
System.out.println("\t\t************* Show The Goods *************\n");
//其实我个人感觉好像用户信息类才是主体
/*
* 你看所有的选项都要用到它,当然这确实是废话
* 因为个性化的原因,所以必须对个人信息进行高度的集成化处理
* 所以才有了我们的用户类
*/
//当用户的购物车信息展示3秒钟后。
// 再默认回到主界面。
// 后期可以改为手动回到主界面。
user.Show_Goods(goods);
try {
Thread.sleep(3000);
}
catch (Exception e){
System.out.println(e);
}
break;
case 4:
/*
* 因为我在while循环里面设置:
* 如果操作数!=4,就会一直无限循环
* 那么当操作数为4的时候,就会打破整个循环,从而运行到main的后面直至结束。
*/
//哈,每次退出前算账!!还要记到数据库!
user.Change_the_Balance();
System.out.print("\t\tGoodBye!");
//自作孽!!!
user.getUser_Name();
System.out.print(" Thanks for your Shopping!");
System.out.print(" You almost Consume "+user.Cal_Balance()+" 元!");
break;
}
if(op!=4){
System.out.println("Please Input the Next Action: ");
}
}
}
}
import java.util.Scanner;
public class Menu{
public void MainMenu() {
System.out.println("\n\n\t\t\t 欢迎颜雨薇同学光临我们的Shop System" + "\n\n\n");
System.out.println("\t\t\t\t 1. 我的账号\n\n");
System.out.println("\t\t\t\t 2. 倾情购物\n\n");
System.out.println("\t\t\t\t 3. 购物清单\n\n");
System.out.println("\t\t\t\t 4. 退出系统\n\n");
}
public void Customer(User user,Goods goods){
Scanner Buying = new Scanner(System.in);
int opr=0;
while(opr!=4) {
System.out.println("\n\n\t\t\t 欢迎颜雨薇同学光临我们的Shop System" + "\n");
System.out.println("\t\t\t\t 1. 修改名字\n\n");
System.out.println("\t\t\t\t 2. 修改密码\n\n");
System.out.println("\t\t\t\t 3. 已购商品\n\n");
System.out.println("\t\t\t\t 4. 回到首页\n\n");
System.out.print("\n\t Please Input Your Next Action : ");
opr=Buying.nextInt();
Scanner Operate = new Scanner(System.in);
switch (opr){
case 1:
System.out.print("\t Please Input Your New Name : ");
String new_name=Operate.nextLine();
user.Change_the_User_Name(user,new_name);
break;
case 2:
System.out.print("\t Please Input Your New Password : ");
String Newpasswd=Operate.nextLine();
user.Change_the_Password(user,Newpasswd);
break;
case 3:
System.out.println("\n\n\n\t\t************* Show The Goods *************\n");
user.Show_Goods(goods);
try {
Thread.sleep(3000);
}
catch (Exception e){
System.out.println(e);
}
break;
case 4:
break;
}
}
}
public void InitMenu(){
System.out.println("\n\n\t\t\t 欢迎颜雨薇同学光临我们的Shop System"+"\n\n");
System.out.println("\t\t\t\t 1. Sign in the Shop System!\n\n");
System.out.println("\t\t\t\t 2. Sign up the Shop System!\n\n");
System.out.println("\t\t\t\t 3. Exit The Shop System!\n\n");
}
public boolean LoginMenu(User S){
System.out.println("\n\n\t\t\t 欢迎颜雨薇同学光临我们的Shop System"+"\n\n");
Scanner First = new Scanner(System.in);
System.out.print("\t\t 1. Please Input Your Account : ");
String in = First.nextLine();
System.out.print("\t\t 2. Please Input Your Password : ");
String in1 = First.nextLine();
if(S.Sign_in(in,in1)){
S.setUser_Name(in);
S.setPassword(in1);
return true;
}
return false;
}
public boolean SignupMenu(User S){
System.out.println("\n\n\t\t\t 欢迎颜雨薇同学光临我们的Shop System"+"\n\n");
Scanner First = new Scanner(System.in);
System.out.print("\t\t 1. Please Input Your New Account : ");
String in = First.next();
System.out.print("\t\t 2. Please Input Your New Password : ");
String in1 = First.next();
System.out.print("\t\t 2. Please Re_Input Your Password : ");
String in2=First.next();
if(in1.equals(in2)){
S.Sign_up(in,in1);
return true;
}
else{
System.out.println("\t\t前后密码不一致!\n\n\n\n");
return false;
}
}
public void Buy_Goods(User user){
Scanner Buying = new Scanner(System.in);
int opr=0;
int start=1;
int stop=start+5;
while(opr!=1000) {
Goods.Show_Goods_List(start, stop);
System.out.print("\n\n输入商品数字,即可购买,翻页输入0,退出输入1000:");
System.out.print("\n\n");
opr = Buying.nextInt();
if(opr==0){
start=stop+1;
stop=start+5;
}
else if(opr>0&& opr<1000){
user.Buy(opr);
// user.Insert_Good(opr);
}
else if(opr==1000){
break;
}
else {
opr=0;
}
}
}
}
import javafx.scene.paint.Stop;
import java.sql.*;
import java.util.*;
import java.text.*;
public class Goods{
private static String driver = "com.mysql.jdbc.Driver";
//此处查看网络才知道。要求SSL,所以就酱紫咯:https://zhidao.baidu.com/question/2056521203295428667.html
private static String url = "jdbc:mysql://127.0.0.1:3306/Shop_User?useUnicode=true&characterEncoding=GBK&useSSL=true";
private static String user = "root";
private static String password = "zzb1184827350";
public static String getName(int id){
String Name=" ";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (conn.isClosed())
System.out.println("Failed connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select good_name from Goods where good_id= " + id + " ";
ResultSet rs = statement.executeQuery(sql);
if(rs.next()) {
Name=rs.getString("good_name");
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return Name;
}
public static int getPrice(int id){
int price=0;
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (conn.isClosed())
System.out.println("Failed connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select price from Goods where good_id= " + id + " ";
ResultSet rs = statement.executeQuery(sql);
if(rs.next()) {
price=rs.getInt("price");
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return price;
}
public void Insert_Good(String name,int price){
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
// String sql = "select good_name,price from Goods where n";
String insert = "insert into Goods(good_name,price,Is_Empty) values(' "+name+" ' , "+price+" ,false)";
// String delete = "delete from tcount_tbl where runoob_author = \"RUNOOB.COM\" ";
boolean rs1 = statement.execute(insert);
// boolean rs1 = statement.execute(delete);
// ResultSet rs = statement.executeQuery(sql);
if(rs1) {
System.out.println("----------------------------------");
System.out.println("*************入库成功:*************");
System.out.println("----------------------------------");
}
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void Show_Good(int id){
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (conn.isClosed())
System.out.println("Failed connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select good_id,good_name,price,discount,Is_Empty from Goods where good_id="+id+" ";
ResultSet rs = statement.executeQuery(sql);
String good_name="";
int good_id=0,price=0;
float discount;
String Is_Empty=" ";
while(rs.next()) {
good_id=rs.getInt("good_id");
price=rs.getInt("price");
good_name=rs.getString("good_name");
if(rs.getBoolean("Is_Empty")){
Is_Empty="卖断";
}
else{
Is_Empty="还有";
}
discount=rs.getFloat("discount");
System.out.println("\t\t"+good_id+"\t\t "+good_name+"\t\t "+price+"\t\t "+discount+"\t\t "+Is_Empty);
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void Show_Goods_List(int Start_id,int Stop_id){
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (conn.isClosed())
System.out.println("Failed connecting to the Database!");
Statement statement = conn.createStatement();
if(Start_id>Stop_id){
int tmp=Start_id;
Start_id=Stop_id;
Stop_id=tmp;
}
String sql = "select good_id,good_name,price,discount,Is_Empty from Goods where good_id<="+Stop_id+" and good_id>="+Start_id+" ";
ResultSet rs;
rs = statement.executeQuery(sql);
String good_name="";
int good_id=0,price=0;
float discount;
String Is_Empty=" ";
System.out.println("\t 序号\t\t 商品\t\t 价格\t 折扣\t\t是否有货");
while(rs.next()) {
good_id=rs.getInt("good_id");
price=rs.getInt("price");
good_name=rs.getString("good_name");
if(rs.getBoolean("Is_Empty")){
Is_Empty="!卖断了!";
}
else{
Is_Empty="还有货!";
}
discount=rs.getFloat("discount");
System.out.println("\t\t"+good_id+"\t\t "+good_name+"\t\t "+price+"\t\t "+discount+"\t\t "+Is_Empty);
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.sql.*;
//import java.util.*;
import java.text.*;
public class User {
private String User_Name;
private String Password;
private int User_ID=0;
private int[] User_Goods=new int[10];
public int sum=0;
private static String driver = "com.mysql.jdbc.Driver";
//此处查看网络才知道。要求SSL,所以就酱紫咯:https://zhidao.baidu.com/question/2056521203295428667.html
private static String url = "jdbc:mysql://127.0.0.1:3306/Shop_User?useUnicode=true&characterEncoding=GBK&useSSL=true";
private static String user = "root";
private static String password = "zzb1184827350";
public void setUser_Name(String s){
this.User_Name=s;
}
public String Return_User_Name(){
return this.User_Name;
}
public void setUser_ID(int d){
this.User_ID=d;
}
public void setPassword(String p){
this.Password=p;
}
public void getUser_Name(){
System.out.print(User_Name);
}
public int getUser_ID(){
return this.User_ID;
}
public String getPassword(){
return this.Password;
}
public void Show_Goods(Goods goods){
System.out.println("\t 序号\t\t 商品\t\t 价格\t 折扣\t\t是否有货");
System.out.println("\t ------------------------------------------------\n");
for(int x:User_Goods){
goods.Show_Good(x);
}
}
public void Buy(int id){
int price=Goods.getPrice(id);
if(price!=0){
User_Goods[sum]=id;
sum++;
}
}
public int Cal_Balance(){
int balance=0;
for(int x:User_Goods){
balance+=Goods.getPrice(x);
}
return balance;
}
public void Insert_Good(int id){
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
// String sql = "select good_name,price from Goods where n";
String insert = "insert into User(id,name,good_id,good_name) values("+id+" , ' "+Goods.getName(id)+" ',false)";
boolean rs1;
if (statement.execute(insert)) rs1 = true;
else rs1 = false;
// boolean rs1 = statement.execute(delete);
// ResultSet rs = statement.executeQuery(sql);
if(rs1) {
System.out.println("----------------------------------");
System.out.println("*************收藏成功:*************");
System.out.println("----------------------------------");
}
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
public boolean Sign_in(String User_name, String passwd) {
String ps=" ";
int id=0;
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (conn.isClosed())
System.out.println("Failed connecting to the Database!");
Statement statement = conn.createStatement();
String Check = "select id,password from User where name=' " + User_name + " ' ";
ResultSet checkpasswd = statement.executeQuery(Check);
if(checkpasswd.next()) {
id = checkpasswd.getInt("id");
ps = checkpasswd.getString("password");
}
checkpasswd.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//从数据库中取出来的字符串带空格?******
ps=ps.trim();
// System.out.println(ps);
if (!passwd.equals(ps)) {
System.out.println("The Password is wrong !!!");
return false;
}
System.out.println("\n\n\t\t*********Welcome to Here !!!*********");
this.setUser_ID(id);
return true;
}
public void Sign_up(String User_Name, String passwd) {
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (conn.isClosed()) {
System.out.println("Failed connecting to the Database!");
}
Statement statement = conn.createStatement();
java.util.Date dNow = new java.util.Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
String Rdate = ft.format(dNow);
String sql = "select id,name,R_date from User where name=' " + User_Name + " ' and password='" + passwd + " ' ";
String insert = "insert into User(name,password,R_date) values(' " + User_Name + " ' ,' " + passwd + " ',' " + Rdate + " ')";
boolean rs1 = statement.execute(insert);
if(!rs1){
System.out.println("\t\t----------------------------------");
System.out.println("*****************注册成功!*****************");
System.out.println("\t\t----------------------------------");
System.out.println(" ID" + "\t\t" + " NAME" + "\t\t" + "Register_Date\n");
}
ResultSet rs = statement.executeQuery(sql);
int user_id = 0;
String name = " ";
String R_d = " ";
while (rs.next()) {
user_id = rs.getInt("id");
name = rs.getString("name");
R_d = rs.getString("R_date");
name = new String(name.getBytes("UTF-8"), "UTF-8");
System.out.println(user_id + "\t" + name + "\t" + R_d);
}
System.out.println("\t\t----------------------------------");
System.out.println("\t\t----------------------------------");
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void Change_the_Password(User The_user, String new_passwd) {
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (conn.isClosed())
System.out.println("Failed connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select id,name,password from User where name=' " + The_user.Return_User_Name() + " ' and password='" + new_passwd + " ' ";
String Check = "select password from User where name=' " + The_user.Return_User_Name() + " ' and id= "+this.getUser_ID()+" ";
ResultSet checkpasswd = statement.executeQuery(Check);
//此处要求我先走一步next(),果然,从next出现开始才开始读数,不然会报错!!
if(checkpasswd.next()) {
String oldps = checkpasswd.getString("password");
// System.out.println(oldps + "&&&&&&");
//老问题:String要用equals()才能比较,不能用== 会报错!!
if (oldps.equals(The_user.getPassword())) {
System.out.println("The Old Password is wrong !!!");
checkpasswd.close();
conn.close();
return;
}
setPassword(new_passwd);
checkpasswd.close();
String Change = "UPDATE User set password=' " + new_passwd + " ' where id="+The_user.getUser_ID()+"";
boolean rs1=statement.execute(Change);
if(!rs1) {
ResultSet rs = statement.executeQuery(sql);
System.out.println("\t\t----------------------------------");
System.out.println("*****************执行结果:*****************");
System.out.println("\t\t----------------------------------");
System.out.println(" \tID" + "\t\t" + " NAME" + "\t\t" + "New_Password");
System.out.println("\t\t----------------------------------");
int user_id = 0;
String name = " ";
String passwd = " ";
while (rs.next()) {
user_id = rs.getInt("id");
name = rs.getString("name");
passwd = rs.getString("password");
name = new String(name.getBytes("UTF-8"), "UTF-8");
System.out.println(user_id + "\t" + name + "\t" + passwd);
}
System.out.println("\t\t----------------------------------\n\n");
rs.close();
}
}
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void Change_the_User_Name(User The_user,String New_Name) {
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (conn.isClosed())
System.out.println("Failed connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select id,name from User where name=' " + The_user.Return_User_Name()+ " 'and id="+The_user.getUser_ID()+ " ";
String Change = "UPDATE User set name=' "+ New_Name +" ' where name=' " + The_user.Return_User_Name()+ " 'and id="+The_user.getUser_ID()+ " ";
boolean rs1=statement.execute(Change);
if (rs1) {
ResultSet rs = statement.executeQuery(sql);
System.out.println("\t\t----------------------------------");
System.out.println("*****************执行结果:*****************");
System.out.println("\t\t----------------------------------");
System.out.println(" ID" + "\t\t" + " NAME" + "\t\t");
System.out.println("----------------------------------");
int user_id = 0;
String name = " ";
while (rs.next()) {
user_id = rs.getInt("id");
name = rs.getString("name");
name = new String(name.getBytes("UTF-8"), "UTF-8");
System.out.println(user_id + "\t" + name + "\t");
}
rs.close();
System.out.println("----------------------------------\n\n");
}
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void Change_the_Balance() {
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (conn.isClosed())
System.out.println("Failed connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "update User set Balance= "+Cal_Balance()+" where name=' " + User_Name+ " 'and id="+User_ID+ " ";
boolean rs1=statement.execute(sql);
if (!rs1) {
}
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
名字自己看public的类名吧。我要躺了。再不上床真的会死!!
正文之后
只好放张图结尾: