查看: 1334|回复: 0

[Mysql数据库] 【寒假学Java】第六天收获:构造器,String小品,JDBC小玩

发表于 2018-2-15 08:00:00
正文之前

今天难得认真写了会代码,虽然还是有很多重用,以及巨多的逻辑不清楚,但是好歹基本的商城给我写出来了。而且还连接了数据库,各种操作也是6的飞起,整整690行代码,现在眼睛看屏幕都是双重的,看不清了。再不休息估计会猝死,可以预见未来自己的地中海了!!!今天换了个很清爽的背景,写这么勤快 杰西卡·阿尔芭功不可没!!

第一点:子类中调用基类的构造器

如果我们没有在子类中,给基类进行各类构造器的调用。那么它就会默认的调用基类的默认构造器。这很多时候会给我们带来困扰。因为我们从基类继承而来的子类。也希望能够调用基类的各类构造器而不仅仅是默认构造器。

下面的代码我自己定义了构造器, 所以应该是不存在默认构造器的。但是万一就存在了呢?现在看来把问题看严重了。其实没这么大的风险啊!!因为如果要传入参数的话,肯定就要调用基类的相应构造器啊!

  1. class dog1{
  2. private int x=0;
  3. dog1(int s){
  4. this.x=s;
  5. System.out.println("Dog1 初始化!"+x);
  6. }
  7. void getX(){
  8. System.out.println(x);
  9. }
  10. }
  11. class dog2 extends dog1{
  12. private int x1=1;
  13. dog2(int a,int b){
  14. super(a);
  15. this.x1=b;
  16. System.out.println("Dog2 初始化! "+x1);
  17. }
  18. void getX1(){
  19. System.out.println(x1);
  20. }
  21. }
  22. public class Puppy extends dog2{
  23. private int x2=2;
  24. Puppy(int a,int b,int c){
  25. super(a,b);
  26. this.x2=c;
  27. System.out.println("Puppy 初始化!"+x2);
  28. }
  29. void getX2(){
  30. System.out.println(x2);
  31. }
  32. public static void main(String[] args) {
  33. Puppy s=new Puppy(100,101,102);
  34. s.getX();
  35. }
  36. }
复制代码
第二点:继承的方式问题

第三点:String的比较不能用== 和 !=
  1. public void Change_the_Password(User The_user, String new_passwd) {
  2. try {
  3. Class.forName(driver);
  4. Connection conn = DriverManager.getConnection(url, user, password);
  5. if (conn.isClosed())
  6. System.out.println("Failed connecting to the Database!");
  7. Statement statement = conn.createStatement();
  8. String sql = "select id,name,password from User where name=' " + The_user.Return_User_Name() + " ' and password='" + new_passwd + " ' ";
  9. String Check = "select password from User where name=' " + The_user.Return_User_Name() + " ' and id= "+this.getUser_ID()+" ";
  10. ResultSet checkpasswd = statement.executeQuery(Check);
  11. //此处要求我先走一步next(),果然,从next出现开始才开始读数,不然会报错!!
  12. if(checkpasswd.next()) {
  13. String oldps = checkpasswd.getString("password");
  14. // System.out.println(oldps + "&&&&&&");
  15. //老问题:String要用equals()才能比较,不能用== 会报错!!
  16. if (oldps.equals(The_user.getPassword())) {
  17. System.out.println("The Old Password is wrong !!!");
  18. checkpasswd.close();
  19. conn.close();
  20. return;
  21. }
  22. setPassword(new_passwd);
  23. checkpasswd.close();
  24. String Change = "UPDATE User set password=' " + new_passwd + " ' where id="+The_user.getUser_ID()+"";
  25. boolean rs1=statement.execute(Change);
  26. if(!rs1) {
  27. ResultSet rs = statement.executeQuery(sql);
  28. System.out.println("\t\t----------------------------------");
  29. System.out.println("*****************执行结果:*****************");
  30. System.out.println("\t\t----------------------------------");
  31. System.out.println(" \tID" + "\t\t" + " NAME" + "\t\t" + "New_Password");
  32. System.out.println("\t\t----------------------------------");
  33. int user_id = 0;
  34. String name = " ";
  35. String passwd = " ";
  36. while (rs.next()) {
  37. user_id = rs.getInt("id");
  38. name = rs.getString("name");
  39. passwd = rs.getString("password");
  40. name = new String(name.getBytes("UTF-8"), "UTF-8");
  41. System.out.println(user_id + "\t" + name + "\t" + passwd);
  42. }
  43. System.out.println("\t\t----------------------------------\n\n");
  44. rs.close();
  45. }
  46. }
  47. conn.close();
  48. } catch (ClassNotFoundException e) {
  49. System.out.println("Sorry,can`t find the Driver!");
  50. e.printStackTrace();
  51. } catch (SQLException e) {
  52. e.printStackTrace();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
复制代码

String的比较体现在这一行:

  1. if (oldps.equals(The_user.getPassword())) {
复制代码

如果你给一个oldps.equals==The_user.getPassword() !!!相信我,会一直是true的, 受害人现身说法你还不信????

第四点: JDBC连接的数据库版本够的话:
  1. private static String driver = "com.mysql.jdbc.Driver";
  2. //此处查看网络才知道。要求SSL,所以就酱紫咯:https://zhidao.baidu.com/question/2056521203295428667.html
  3. 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,那就会每次连接数据库都有:

  1.  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!!!

第五点:字符串去掉首尾空格:

因为是数据库的出入,所以变得有些不受掌控了。这很烦!!所以有了一个补救措施:

  1. try {
  2. Class.forName(driver);
  3. Connection conn = DriverManager.getConnection(url, user, password);
  4. if (conn.isClosed())
  5. System.out.println("Failed connecting to the Database!");
  6. Statement statement = conn.createStatement();
  7. String Check = "select id,password from User where name=' " + User_name + " ' ";
  8. ResultSet checkpasswd = statement.executeQuery(Check);
  9. if(checkpasswd.next()) {
  10. id = checkpasswd.getInt("id");
  11. ps = checkpasswd.getString("password");
  12. }
  13. checkpasswd.close();
  14. conn.close();
  15. } catch (ClassNotFoundException e) {
  16. System.out.println("Sorry,can`t find the Driver!");
  17. e.printStackTrace();
  18. } catch (SQLException e) {
  19. e.printStackTrace();
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. //从数据库中取出来的字符串带空格?
  24. //******************************************
  25. ps=ps.trim();
  26. //******************************************
  27. if (!passwd.equals(ps)) {
  28. System.out.println("The Password is wrong !!!");
  29. return false;
  30. }
  31. System.out.println("\n\n\t\t*********Welcome to Here !!!*********");
  32. this.setUser_ID(id);
  33. return true;
复制代码

没错 ps=ps.trim();就是去除 首尾空格的,靠这个我才勉强苟活:D),睡了睡了。再不睡会死!!!!

对了。代码大放送(丢一个文件夹下就好):

  1. 没辙,慕课手记没法超过长度限制,所以要看的去简书看:
复制代码

【寒假学Java】第六天收获:构造器,String小品,JDBC小玩

名字自己看public的类名吧。我要躺了。再不上床真的会死!!

正文之后

只好放张图结尾:



回复

使用道具 举报