欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何用idea数据库编写快递e站

程序员文章站 2022-03-06 15:22:03
用数据库编写快递e站(本文只写了idea方面的代码)随着快递业的不断发展,快递e站也越来越多,我们来编写一个简单的快递e站小程序,本文就介绍了如何用数据库知识编写快递e站。##成品如下:一、idea如...

用数据库编写快递e站(本文只写了idea方面的代码)

随着快递业的不断发展,快递e站也越来越多,我们来编写一个简单的快递e站小程序,本文就介绍了如何用数据库知识编写快递e站。

##成品如下:

如何用idea数据库编写快递e站

一、idea如何连接数据库

第一种方法:直接在方法体中增加连接信息

优点:如果仅使用一次数据库操作可选择
缺点:多次数据库操作每次都需要写,麻烦

public void select() {

  connection conn = null;
  statement stmt = null;
  resultset rs = null;
  try {
   //1.注册驱动
   class.forname("com.mysql.jdbc.driver");
   //2.定义sql
   string sql = "select * from kuaidi";
   //3.获取conn
   conn = drivermanager.getconnection("jdbc:mysql:///kuaidi", "root", "123");
   //4.获取执行sql对象statement
   stmt = conn.createstatement();
   //5.执行sql
   rs = stmt.executequery(sql);
   //6处理结果
   while (rs.next()) {
    int danhao = rs.getint(1);
    int qujianma = rs.getint(2);
    string gongsi = rs.getstring("gongsi");
    string guizi = rs.getstring(4);
    system.out.println("单号为:" + danhao + " " + "取件码为:" + qujianma + " " + "公司是:" + gongsi + " " + "柜子在第" + guizi + "个");
   }

  } catch (classnotfoundexception e) {
   e.printstacktrace();
  } catch (sqlexception throwables) {
   throwables.printstacktrace();
  } finally {
   if (stmt != null) {
    try {
     stmt.close();
    } catch (sqlexception throwables) {
     throwables.printstacktrace();
    }
   }

   if (conn != null) {
    try {
     conn.close();
    } catch (sqlexception throwables) {
     throwables.printstacktrace();
    }
   }

   if (rs != null) {
    try {
     rs.close();
    } catch (sqlexception throwables) {
     throwables.printstacktrace();
    }
   }
  }
 }

方法二:

建立一个jdbchelper和一个存储数据库账号密码的properties,来帮助快速加载驱动以及释放内存
优点:只需要写一次,用的时候调用即可
缺点:一次要写很多

如何用idea数据库编写快递e站

释放内存的时候可能传入两个或者三个参数需要释放,所以用重载形式来解决

 private static string url;
 private static string user;
 private static string password;
 private static string driver;

 /**
  * 文件的读取,只需要读取一次即可
  */
 static {
  //读取资源文件,并获取值
  try {
   //1.创建properties集合类
   properties pro = new properties();
   //2.加载文件
   //获取src路径下的文件的方式--->classloader类加载器
   classloader classloader = jdbchelper.class.getclassloader();
   url res = classloader.getresource("jdbc.properties");
   string path = res.getpath();
   
   //pro.load(new filereader("src/jdbc.properties"));
   pro.load(new filereader(path));

   url = pro.getproperty("url");
   user = pro.getproperty("user");
   password = pro.getproperty("password");
   driver = pro.getproperty("driver");

   class.forname(driver);
  } catch (ioexception e) {
   e.printstacktrace();
  } catch (classnotfoundexception e) {
   e.printstacktrace();
  }
 }
 /**
  * 获取连接
  *
  * @return连接对象
  */
 public static connection getconnection() throws sqlexception {
  return drivermanager.getconnection(url, user, password);
 }
 /**
  * 释放资源
  *
  * @param stmt
  * @param conn
  */
 public static void close(statement stmt, connection conn) {
  if (stmt != null) {
   try {
    stmt.close();
   } catch (sqlexception throwables) {
    throwables.printstacktrace();
   }
  }
  if (conn != null) {
   try {
    conn.close();
   } catch (sqlexception throwables) {
    throwables.printstacktrace();
   }
  }
 }
//释放内存
 public static void close(resultset rs, statement stmt, connection conn) {
  if (stmt != null) {
   try {
    stmt.close();
   } catch (sqlexception throwables) {
    throwables.printstacktrace();
   }
  }
  if (rs != null) {
   try {
    rs.close();
   } catch (sqlexception throwables) {
    throwables.printstacktrace();
   }
  }
  if (conn != null) {
   try {
    conn.close();
   } catch (sqlexception throwables) {
    throwables.printstacktrace();
   }
  }

 }

二、方法代码的实现

1.快递员增加快递

代码如下:

public class add {
 connection conn = null;
 statement stmt = null;
 random r= new random();
 public void addq(int danhao,string gongsi){
  try {
   conn = jdbchelper.getconnection();
   int qujianma = r.nextint((999999)+1);
   string guizi = integer.tostring(r.nextint(100)+1);
   string sql = "insert into kuaidi values(" + danhao + "," + qujianma+",'"+gongsi+"','"+guizi+"')";
   stmt = conn.createstatement();
   int a = stmt.executeupdate(sql);
   system.out.println("取件码为:"+qujianma);
   if(a>0){
    system.out.println("添加成功");
   }
   else {
    system.out.println("添加失败");
   }
  } catch (sqlexception throwables) {
   throwables.printstacktrace();
  }
 }
 }

2.快递员删除快递

代码如下:

public class delete {

 connection conn = null;
 statement stmt = null;

 public void delete(int danhao) {

  try {
   conn = jdbchelper.getconnection();
   string sql = "delete from kuaidi where danhao = "+danhao;

   stmt = conn.createstatement();
   int a = stmt.executeupdate(sql);
   if(a>0){
    system.out.println("删除成功");
   }
   else {
    system.out.println("删除失败");
   }

  } catch (sqlexception throwables) {
   throwables.printstacktrace();
  }
 finally {
  jdbchelper.close(stmt,conn);
 }
}

主要内容代码:

public class imp {
public static void main(string[] args) {
kuaidi();
}
public static void kuaidi() {
 system.out.println("====欢迎使用新职课快递柜====");
 scanner sc = new scanner(system.in);
 random r = new random();
 while (true) {

  system.out.println("请输入你的身份:1-快递员,2-用户");
  try {
   int a = sc.nextint();

   if (a < 1 || a > 2) {
    system.out.println("输入有误,请重新输入");
   } else if (a == 1) {
    system.out.println("====欢迎使用新职课快递柜====");
    system.out.println("请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递");
    int b = sc.nextint();
    switch (b) {
     case 1: {
      system.out.println("====欢迎使用新职课快递柜====");
      system.out.println("请输入快递单号:");
      int danhao = sc.nextint();
      system.out.println("请输入公司名称:");
      string gongsi = sc.next();
      add add = new add();
      add.addq(danhao, gongsi);

      break;
     }
     case 2: {
      system.out.println("====欢迎使用新职课快递柜====");
      system.out.print("请输入要删除的订单号:");
      int dingdan = sc.nextint();
      delete delete = new delete();
      delete.delete(dingdan);
      break;
     }
     case 3: {
      system.out.println("====欢迎使用新职课快递柜====");
      system.out.print("请输入要修改的订单号:");
      int danhao = sc.nextint();
      alter al = new alter();
      boolean q = al.select(danhao);
      if (q = true) {
       system.out.println("请输入更改后的单号");
       int afdanhao = sc.nextint();
       al.alter(afdanhao, danhao);
      } else {
       system.out.println("请核实订单号");
      }

      break;
     }
     case 4: {
      system.out.println("====欢迎使用新职课快递柜====");
      selectall s = new selectall();
      s.select();
     }
    }

   } else if (a == 2) {
    system.out.println("====欢迎使用新职课快递柜====");
    system.out.println("请输入取件码");
    int qujianma = sc.nextint();
    take t = new take();
    t.take(qujianma);

   }


  } catch (inputmismatchexception e) {
   system.out.println();
   system.out.println("请输入数字序号!!!!!!!!");
   system.out.println();
   kuaidi();
  }
 }

}
}

别的几个代码块都大同小异,只需要修改sql语句即可

如有需要全部代码,或者有疑问的同学私信我就可以了

到此这篇关于如何用idea数据库编写快递e站的文章就介绍到这了,更多相关idea数据库编写快递e站内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!