今天难得认真写了会代码,虽然还是有很多重用,以及巨多的逻辑不清楚,但是好歹基本的商城给我写出来了。而且还连接了数据库,各种操作也是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";
如果后没有&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),睡了睡了。再不睡会死!!!!
对了。代码大放送(丢一个文件夹下就好):
没辙,慕课手记没法超过长度限制,所以要看的去简书看:
【寒假学Java】第六天收获:构造器,String小品,JDBC小玩
名字自己看public的类名吧。我要躺了。再不上床真的会死!!
正文之后只好放张图结尾:
共同学习,写下你的评论
评论加载中...
作者其他优质文章