如何用idea數(shù)據(jù)庫(kù)編寫(xiě)快遞e站
用數(shù)據(jù)庫(kù)編寫(xiě)快遞e站(本文只寫(xiě)了idea方面的代碼)
隨著快遞業(yè)的不斷發(fā)展,快遞e站也越來(lái)越多,我們來(lái)編寫(xiě)一個(gè)簡(jiǎn)單的快遞e站小程序,本文就介紹了如何用數(shù)據(jù)庫(kù)知識(shí)編寫(xiě)快遞e站。
##成品如下:
一、IDEA如何連接數(shù)據(jù)庫(kù)
第一種方法:直接在方法體中增加連接信息
優(yōu)點(diǎn):如果僅使用一次數(shù)據(jù)庫(kù)操作可選擇
缺點(diǎn):多次數(shù)據(jù)庫(kù)操作每次都需要寫(xiě),麻煩
public void select() { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { //1.注冊(cè)驅(qū)動(dòng) Class.forName("com.mysql.jdbc.Driver"); //2.定義sql String sql = "select * from kuaidi"; //3.獲取conn conn = DriverManager.getConnection("jdbc:mysql:///kuaidi", "root", "123"); //4.獲取執(zhí)行sql對(duì)象Statement stmt = conn.createStatement(); //5.執(zhí)行sql rs = stmt.executeQuery(sql); //6處理結(jié)果 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("單號(hào)為:" + danhao + " " + "取件碼為:" + qujianma + " " + "公司是:" + gongsi + " " + "柜子在第" + guizi + "個(gè)"); } } 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(); } } } }
方法二:
建立一個(gè)JDBCHelper和一個(gè)存儲(chǔ)數(shù)據(jù)庫(kù)賬號(hào)密碼的Properties,來(lái)幫助快速加載驅(qū)動(dòng)以及釋放內(nèi)存
優(yōu)點(diǎn):只需要寫(xiě)一次,用的時(shí)候調(diào)用即可
缺點(diǎn):一次要寫(xiě)很多
釋放內(nèi)存的時(shí)候可能傳入兩個(gè)或者三個(gè)參數(shù)需要釋放,所以用重載形式來(lái)解決
private static String url; private static String user; private static String password; private static String driver; /** * 文件的讀取,只需要讀取一次即可 */ static { //讀取資源文件,并獲取值 try { //1.創(chuàng)建properties集合類(lèi) Properties pro = new Properties(); //2.加載文件 //獲取src路徑下的文件的方式--->ClassLoader類(lèi)加載器 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連接對(duì)象 */ 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(); } } } //釋放內(nèi)存 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(); } } }
二、方法代碼的實(shí)現(xiàn)
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); } }
主要內(nèi)容代碼:
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("請(qǐng)輸入你的身份:1-快遞員,2-用戶"); try { int a = sc.nextInt(); if (a < 1 || a > 2) { System.out.println("輸入有誤,請(qǐng)重新輸入"); } else if (a == 1) { System.out.println("====歡迎使用新職課快遞柜===="); System.out.println("請(qǐng)選擇操作:1-存快遞 2-刪除快遞 3-修改快遞信息 4-查看所有快遞"); int b = sc.nextInt(); switch (b) { case 1: { System.out.println("====歡迎使用新職課快遞柜===="); System.out.println("請(qǐng)輸入快遞單號(hào):"); int danhao = sc.nextInt(); System.out.println("請(qǐng)輸入公司名稱:"); String gongsi = sc.next(); Add add = new Add(); add.addq(danhao, gongsi); break; } case 2: { System.out.println("====歡迎使用新職課快遞柜===="); System.out.print("請(qǐng)輸入要?jiǎng)h除的訂單號(hào):"); int dingdan = sc.nextInt(); Delete delete = new Delete(); delete.delete(dingdan); break; } case 3: { System.out.println("====歡迎使用新職課快遞柜===="); System.out.print("請(qǐng)輸入要修改的訂單號(hào):"); int danhao = sc.nextInt(); Alter al = new Alter(); boolean q = al.select(danhao); if (q = true) { System.out.println("請(qǐng)輸入更改后的單號(hào)"); int afdanhao = sc.nextInt(); al.alter(afdanhao, danhao); } else { System.out.println("請(qǐng)核實(shí)訂單號(hào)"); } break; } case 4: { System.out.println("====歡迎使用新職課快遞柜===="); SelectAll s = new SelectAll(); s.select(); } } } else if (a == 2) { System.out.println("====歡迎使用新職課快遞柜===="); System.out.println("請(qǐng)輸入取件碼"); int qujianma = sc.nextInt(); Take t = new Take(); t.take(qujianma); } } catch (InputMismatchException e) { System.out.println(); System.out.println("請(qǐng)輸入數(shù)字序號(hào)!!!!!!!!"); System.out.println(); kuaidi(); } } } }
別的幾個(gè)代碼塊都大同小異,只需要修改sql語(yǔ)句即可
如有需要全部代碼,或者有疑問(wèn)的同學(xué)私信我就可以了
到此這篇關(guān)于如何用idea數(shù)據(jù)庫(kù)編寫(xiě)快遞e站的文章就介紹到這了,更多相關(guān)idea數(shù)據(jù)庫(kù)編寫(xiě)快遞e站內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java防盜鏈在報(bào)表中的應(yīng)用實(shí)例(推薦)
下面小編就為大家?guī)?lái)一篇java防盜鏈在報(bào)表中的應(yīng)用實(shí)例(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09Java實(shí)現(xiàn)Treap樹(shù)的示例代碼
本文主要介紹了Java實(shí)現(xiàn)Treap樹(shù)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06mybatis-plus用insertBatchSomeColumn方法批量新增指定字段
mybatisPlus底層的新增方法是一條一條的新增的,下面這篇文章主要給大家介紹了關(guān)于mybatis-plus用insertBatchSomeColumn方法批量新增指定字段的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05SpringAOP中基于注解實(shí)現(xiàn)通用日志打印方法詳解
這篇文章主要介紹了SpringAOP中基于注解實(shí)現(xiàn)通用日志打印方法詳解,在日常開(kāi)發(fā)中,項(xiàng)目里日志是必不可少的,一般有業(yè)務(wù)日志,數(shù)據(jù)庫(kù)日志,異常日志等,主要用于幫助程序猿后期排查一些生產(chǎn)中的bug,需要的朋友可以參考下2023-12-12Java反轉(zhuǎn)鏈表測(cè)試過(guò)程介紹
這篇文章主要介紹了Java反轉(zhuǎn)鏈表測(cè)試過(guò)程,學(xué)習(xí)過(guò)數(shù)據(jù)結(jié)構(gòu)的小伙伴們,對(duì)鏈表想來(lái)是并不陌生。本篇文章將為大家介紹幾種在Java語(yǔ)言當(dāng)中,實(shí)現(xiàn)鏈表反轉(zhuǎn)的幾種方法,以下是具體內(nèi)容2023-04-04mybatis xml注釋sql的注意事項(xiàng)及說(shuō)明
這篇文章主要介紹了mybatis xml注釋sql的注意事項(xiàng)及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Java中的static關(guān)鍵字修飾屬性和方法(推薦)
這篇文章主要介紹了Java中的static關(guān)鍵字修飾屬性和方法,包括哪些成員屬性可以被static修飾,靜態(tài)屬性的訪問(wèn)方法示例詳解,需要的朋友可以參考下2022-04-04