您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)使用idea數(shù)據(jù)庫編寫一個(gè)快遞e站,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
一、IDEA如何連接數(shù)據(jù)庫
優(yōu)點(diǎn):如果僅使用一次數(shù)據(jù)庫操作可選擇
缺點(diǎn):多次數(shù)據(jù)庫操作每次都需要寫,麻煩
public void select() { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { //1.注冊驅(qū)動 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對象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("單號為:" + 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è)存儲數(shù)據(jù)庫賬號密碼的Properties,來幫助快速加載驅(qū)動以及釋放內(nèi)存
優(yōu)點(diǎn):只需要寫一次,用的時(shí)候調(diào)用即可
缺點(diǎn):一次要寫很多
釋放內(nèi)存的時(shí)候可能傳入兩個(gè)或者三個(gè)參數(shù)需要釋放,所以用重載形式來解決
private static String url; private static String user; private static String password; private static String driver; /** * 文件的讀取,只需要讀取一次即可 */ static { //讀取資源文件,并獲取值 try { //1.創(chuàng)建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(); } } } //釋放內(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(); } } }
代碼如下:
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(); } } }
代碼如下:
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("請輸入你的身份: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("請輸入要?jiǎng)h除的訂單號:"); 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("請核實(shí)訂單號"); } 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("請輸入數(shù)字序號!!!!!!!!"); System.out.println(); kuaidi(); } } } }
關(guān)于使用idea數(shù)據(jù)庫編寫一個(gè)快遞e站就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。