




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、前30天常用知識點總結: 1. XML的幾種讀取方式: 方式一: /目標:jaxp如何讀取xml文件 P ublic class Demol /創(chuàng)建對象,讀取 xml內(nèi)容 P ublic static void main (Stri ng args) throws Exce pti on / 1)創(chuàng)建 DocumentBuilderFactory 實例 /ctrl+2 松開 l 或者 alt+shift+l Docume ntBuilderFactory factroy = Docume ntBuilderFactor y.newin sta nce(); /2)通過 DocumentBuil
2、derFactory 實例創(chuàng)建 DocumentBuilder Docume ntBuilder builder = factroy. newDocume ntBuilder(); /3)通過 DocumentBuilder 的parse方法讀取 xml文件,返回一個Document對象 Docume nt doc = builder. parse(src/Locale.xmF); /讀岀到xml中 p ublic static void write2xml(Docume nt doc) throws Tran sformerExce pti on Tra nsformerFactory fa
3、ctory = Tra nsformerFactor y.newIn sta nce(); Tran sformer ts = factory. newTra nsformer(); ts.tra nsform( new DOMSource(doc), new StreamResult(src/exam.xml); 讀取方式: /1)讀取exam.xml文件 Docume nt doc = XMLUtil.getDocume nt(); /2)創(chuàng)建 元素,添加 idcard,examid 屬性 Eleme nt stuElem = doc.createEleme nt(stude nt); s
4、tuElem.setAttribute(idcard, stude nt.getIdcard(); stuElem.setAttribute(examid, stude nt.getExamid(); /3)倉建 元素 Eleme nt n ameElem = doc.createEleme nt( n ame); Eleme nt locElem = doc.createEleme nt(locati on ”); Eleme nt graElem = doc.createEleme nt(grade); n ameElem.setTextCo nten t(stude nt.getName
5、(); locElem.setTextCo nten t(stude nt.getLocatio n(); graElem.setTextC onten t(stude nt.getGrade()+); /4)建立 與 元素關系 stuElem.a ppen dChild (n ameElem); stuElem.a ppen dChild(locElem); stuElem.a ppen dChild(graElem); /5)建立 vstudent與 的關系 Node root = doc.getEleme ntsByTagName(exam).item(O); root.a ppen d
6、Child(stuElem); /6)寫岀到exam.xml文件 XMLUtil.write2xml(doc); 方式二: /dom4j方式讀取 /創(chuàng)建Document對象 P ublic static Docume nt getDocume nt() throws Docume ntExce pti on SAXReader reader = new SAXReader(); /2)讀取xml文件 org.dom4j.Docume nt doc = reader.read(F:/chua nzhikaifa/day13demo/src/db.xmF); retu rn (Docume nt)
7、 doc; /寫岀 p ublic static void write2xml(Docume nt doc) throws IOExce pti on XMLWriter writer = new XMLWriter( new FileOut pu tStream(F:/chua nzhikaifa/day13demo/src/db.xmF); /2)寫出xml文件 writer.write(doc); /3)關閉流 writer.close(); Xpth 方式讀取 Docume nt doc = XMLUtil1.getDocume nt(); List list1 = doc.selec
8、tNodes(customer); /查詢多個節(jié)點 添加: Eleme nt root二doc.getRootEleme nt(); Eleme nt cuElem = root.addEleme nt(customer); /添加元素 jaxp核心dom解析的api: 讀取xml文件: 1) DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance(); 2) DocumentBuilder builder =factory.newDocumentBuilder(); 3) Document doc = builde
9、r.parse(xml 路徑); 寫岀xml文件: 1) Tran sformerFactory factory = Tra nsformerFactor y.newin sta nce(); 2) Tran sformer ts = factor y.n ewTra nsformer(); 3) ts.transform(new DOMSource(document),new StreamResult(xml 路徑); 查詢: docume nt.getEleme ntsByTagName(” name)讀當前文檔下的子元素 docume nt.getEleme ntById(id) ele
10、me nt.getEleme ntsByTagName(” name)讀當前元素下的子元素 eleme nt.getAttribute(” name);讀取屬性 eleme nt.getTextCo nten t()讀取文本 創(chuàng)建或修改: docume nt.createEleme nt( n ame);倉 U建元素 修改元素的文本內(nèi)容 eleme nt.setAttribute(” name,value)創(chuàng)建或修改屬性 eleme nt.setTextC onten t(value) 插入和刪除: eleme nt.a ppen dChild(eleme nt) 把元素放入指定元素的子元素下
11、 eleme nt.removeChild(eleme nt) 把元素從指定元素的子元素下刪除 刪除某個元素內(nèi)的屬性 3306,也可以下面的簡寫 ) eleme nt.removeAttribute(” name) 2. 數(shù)據(jù)庫的幾種連接方式 方式一: 數(shù)據(jù)庫傳統(tǒng)的連接方法: P ublic void testQuery() Stri ng sql = SELECT id,stuName,age FROM stude nt; 連接對象 Conn ecti on con = n ull; 執(zhí)行命令的對象 Stateme nt stmt = n ull; 這個是進行查詢操作后返回的值的對象(結果集
12、) ResultSet rs = null; try / 1.加載驅動 Class.forNameCcom.mysql.jdbc.Driver); / 2.創(chuàng)建連接(如果連接數(shù)據(jù)庫是本機,且端口默認是 con = DriverMa nager.getCo nn ectio n( jdbc:mysql:/day16, root, root); / 3.創(chuàng)建執(zhí)行命令的對象 方式一(不常用因為不安全) stmt = con .createStateme nt(); 方式二(常用) /創(chuàng)建預編譯sql的stmt對象(可以對 SQL語句進行預編譯,可以用占位符) P stmt = con.prep ar
13、eStateme nt(sql);/alt + shift +z /獲取第一行 while (rs .n ext() /推薦: 根據(jù)列名稱獲取列的值(便于后期維護) /獲取當前行的id列值 System.out. prin tl n(rs.getl nt(id); /獲取當前行的 stuName列值 System.out. prin tl n(rs.getStri ng(stuName); /獲取當前行的age列值 System.out. prin tl n(rs.getDouble(age); System.out.printin(- 根據(jù)列的索引獲取-); System.out .prin
14、 tl n(rs.getObject(1); System.out .prin tl n(rs.getObject(2); System.out. pnntln( rs.getObject(3); catch (Exce pti on e) e.prin tStackTrace(); fin ally try /關閉資源(關閉的順序要從小到大) if (rs != null) rs.close(); rs=null; /加這個可以使java的垃圾回收機構優(yōu)先回收,可以提交數(shù)據(jù)的性能 if (stmt != n ull) stmt.close(); rs=stmt; if (con 匸 n ul
15、l) con .close(); con二nu ll; catch (SQLExce pti on e) e.prin tStackTrace(); 方式二: 使用連接池技術 DBCP連接池 /創(chuàng)建DBCP連接池對象 BasicDataSource dataSource = new BasicDataSource(); 方式三: C3P0連接池, 使用比較多! Hibernate框架,對 C3P0連接池有支持! Spring框架 對C3P0連接池有很好的支持! / C3P0核心工具類 ComboPooledDataSource dataSource = new ComboPooledDataS
16、ource(); Jdbc操作總結: 1.創(chuàng)建連接對象 Connection 2.創(chuàng)建執(zhí)行命令的Statement 3. 執(zhí)行命令: 查詢:stmt.executeQueryO 更新 stmt.executeU pdate(); 4. 異常、關閉 3.跳轉的地址問題 客戶端跳轉: 需要寫項目名稱 / 相對于:E:a pache-tomcat-6.0.16weba pps 獲取項目名稱:$pageContext.request.contextPath (在jsp中提交到服務器的路徑) 服務器端轉發(fā): 不用寫項目名稱 /相對于:E:a pache-tomcat-6.0.16weba ppspij當
17、前項目路徑下資源 :request.getContextPath()(用于在servlet中重定向的方法中的路徑中) 項目中讀取資源文件 Servlet 讀?。河?ServletContext 對象! ServletC on text sc = 這個對象可以管理項目下( E:apache-tomcat-6.0.16webappsprj )的資源!獲取! this.getServletC on text(); sc.setAttribute(test, 應用程序域對象); sc.getRea IP ath(/); / E:a pache-tomcat-6.0.16weba pp sday23 s
18、c.getResourceAsStream(/image/a.j pg);/ 獲取項目的資源文件,返回的是輸入流 非Servlet讀取: this .getClass().getResource(/); / file:/F:/chua nzhikaifa/tb_sho p1/WebRoot/WEB-INF/classes/ 與 sc.getReal Path(/); 區(qū)別? this .getClass().getResourceAsStream(/) 相對于當前字節(jié)碼文件目錄! 如:E:a pache-tomcat-6.0.16weba pp sday23WEB-INFclasses thi
19、s .getClass().getResource(); 獲取當前字節(jié)碼文件所在目錄的資源! this .getClass().getResourceAsStream(); 4. 類型轉換 1)字符串轉換成日期 String format二 ”yyyy-MM-dd ”; Sim pl eDateFormat sdf二new Sim pl eDateFormat(format); /trim返回字符串的副本,忽略前導空白和尾部空白 if(source!二n ull 2)日期轉換成字符串 p ublic static Stri ng p arse2Stri ng(Date d) Simp leDa
20、teFormat sdf = new Simp leDateFormat(yyyy-MM-dd); return sdf.format(d); 2)數(shù)組轉換成字符串 方式一: Strin g hobby = customer.getHobby(); String hobby_1 二Arrays.toStri ng(hobby); 方式二: p ublic static Stri ng getAts(Stri ng hobby) Stri ng str=; for (int i = 0; i hobby.le ngth; i+) str=st r+hobbyi+,; return str.sub
21、stri ng(0, str.le ngth()-1); 4)數(shù)組轉換成字符串 p ublic static Stri ng getStl(Stri ng hobby) Stri ng str = hobby.s plit(T); return str; 5)字符串轉換成字符 Char sex=get Parameter(sex).charAt(0); 1,什么是參數(shù)化類型? Type類型,標記接口 Type是java編程語言中所有類型的公共高級接口。它們包括原始類型 、參數(shù)化類型、數(shù)組類型、類型變量和基本類型 參數(shù)化類型,用ParameterizedType表示 舉例: /獲取當前運行類的泛型父類信息,即“參數(shù)化類型” Type type = this.getClass().getGe nericS up erclass(); /轉換,獲取參數(shù)化類型對象! 即:class Dep tDao exte nds BaseDao P arameterizedT ype pt = (P arameterizedT ype) type; /獲取參數(shù)化類型中,實際類型定義的數(shù)組集合 Typ e typ e_class = p t.getActualT yp eArgume nts()
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權】 IEC PAS 61980-5:2024 EN Electric vehicle wireless power transfer (WPT) systems - Part 5: Interoperability and safety of dynamic wireless power transfer (D-WPT) for electric
- 保函擔保的合同
- 新能源儲能系統(tǒng)研發(fā)合作項目投資合同
- 產(chǎn)品運輸服務合作合同
- 微信在線簽署合同
- 貨物運輸合同與貨物運輸合同
- 綠化苗木購銷合同書
- 電子產(chǎn)品工程安裝合同
- 動物疫病防控技術服務合同
- 建筑安裝工程合同書
- 湖北省黃岡市2023-2024學年五年級上學期數(shù)學期中試卷(含答案)
- ××管業(yè)分銷市場操作方案
- 《ISO 41001-2018 設施管理- 管理體系 要求及使用指南》專業(yè)解讀與應用指導材料之15:“7支持-7.6 組織知識”(雷澤佳編制-2024)
- 2024年建設工程質量檢測人員-建設工程質量檢測人員(主體結構工程)考試近5年真題集錦(頻考類試題)帶答案
- 《向量共線定理》同步課件
- 小學數(shù)學學習經(jīng)驗交流課件
- 2024年第二批政府專職消防員招錄報名表
- 注塑模具基礎知識
- 2024年單招考試題
- 三年級數(shù)學下冊期末測試卷及答案【可打印】
- 蘇教版小學語文上冊教學研究論文
評論
0/150
提交評論