全國計算機二級JAVA學習資料大全_第1頁
全國計算機二級JAVA學習資料大全_第2頁
全國計算機二級JAVA學習資料大全_第3頁
全國計算機二級JAVA學習資料大全_第4頁
全國計算機二級JAVA學習資料大全_第5頁
已閱讀5頁,還剩72頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、 上學吧:eclipse+jboss+ejb3的sessionbean注釋方法有時需要在session bean中初始化和釋放一些資源。這些工作應(yīng)該在sessionbean的postconstruct和predestroy方法中進行。其中用 postconstruct注釋的方法在sessionbean的構(gòu)造方法調(diào)用之后以后ejb容器在處理完一些其他工作后調(diào)用。用 predestroy注釋的方法在sessionbean的對象實例被ejb容器銷毀之前調(diào)用。除此之外,當有狀態(tài)的sessionbean存在一定時間未被調(diào)用時,ejb容器會將該session

2、bean對象鈍化(passivate),也就是保存在硬盤中。當再次訪問時,ejb容器會激法該sessionbean。在這兩種情況下,ejb容器會分別調(diào)用 sessionbean的 prepassivate和postactivate方法??梢栽趐repassivate方法中將sessionbean中的資源保存或釋放,如打開的數(shù)據(jù)庫連接等。在postactivate方法中可以恢復相應(yīng)的資源。如下面的代碼所示:packageservice; importjava.util.arraylist; importjava.util.list; importjavax.annotation.postcons

3、truct; importjavax.annotation.predestroy; importjavax.annotation.resource; importjavax.ejb.postactivate; importjavax.ejb.prepassivate; importjavax.ejb.sessioncontext; importjavax.ejb.stateful; stateless publicclassshoppingcartbeanimplementsshoppingcart privatelistshoppingcart=newarraylist(); resourc

4、e privatesessioncontextsessioncontext; publicshoppingcartbean() system.out.println(constructor:+sessioncontext); prepassivate publicvoidmypassivate() system.out.println(passivate); postconstruct publicvoidinit() system.out.println(sessioncontext.getinvokedbusinessinterface(); predestroy publicvoidde

5、story() system.out.println(destory); postactivate publicvoidstart() system.out.println(start); override publicvoidaddcommodity(stringvalue) shoppingcart.add(value); override publiclistgetcommodity() returnshoppingcart; hibernate關(guān)系映射對象-關(guān)系 映射基礎(chǔ)1.對象間的基本關(guān)系關(guān)聯(lián)關(guān)系:關(guān)聯(lián)關(guān)系在設(shè)計模式中是被提倡優(yōu)先使用于繼承關(guān)系的。關(guān)聯(lián)關(guān)系就是將一個對象做為別一個對象

6、的成員,是一種包含的關(guān)系。依賴關(guān)系:對與對象之間的方法的調(diào)用,不存在包含的關(guān)系。聚集關(guān)系:這個關(guān)系比較有趣,比如人的手和身體。如果身體不存在了,手也就不存在了。是一種整個與部分的關(guān)系。一般關(guān)系:就是繼承關(guān)系。上邊的這四種關(guān)系是對前一天的補充。對象-關(guān)系的映射基礎(chǔ)沒有涉及這些,而是單一對象通過hibernate與數(shù)據(jù)庫的映射關(guān)系。2.持久化類的屬性及訪問方法首先回顧一下持久化,我們知道持久化層是從業(yè)務(wù)邏輯層中分離出來的專門用于數(shù)據(jù)庫操作的這些部分。持久化層中的持久化類,便是我們之前早已學習的domain類。1).持久化類的訪問者有兩個,一是java應(yīng)用程序,二是hibernate。寫:java應(yīng)

7、用程序通過setter設(shè)置持久化對象的屬性,hibernate通過getter獲取持久化對象的屬性并生成相應(yīng)的sql語句對表格進行操作。讀:hibernate通過setter設(shè)置持久化對象的屬性,java應(yīng)用程序通過getter獲取持久化對象的屬性。2).基本數(shù)據(jù)類型和包裝類型通過前天的日志,我們知道關(guān)聯(lián)對象的屬性與表格的字段是通過property元素節(jié)點設(shè)置的:基本的type是hibernate的類型,我們在持久化類中定義的gender屬性為int。定義為int類型會有什么弊端?比如,我們有個學生成績表。如果某個學生沒有參加某一學科的考試,但我們卻使用了int類型,它的默認值為0,當查看學生

8、成績時,他到底是考了0分還是沒有考試?所以最好將持久化類中的gender屬性定義為integer,它的默認值為null。查詢成績時看到的是null,那么他肯定是沒參加考試哦?。ㄗ⒁猓簲?shù)據(jù)庫中的對應(yīng)字段應(yīng)該為字符型)3).hibernate訪問持久化類屬性的策略hibernate通過name指定的值訪問持久化對象。hibernate通過name值,反射持久化對象的對方法。比如,name的值為 gender。hibernate會直接反射持久化對象的getgender和setgender方法。所以我們必須保證持久化對象中有對應(yīng)的方法。這是因為property有一個access屬性,它的默認值為pro

9、perty。如果指定access的值為field,則hibernate直接根據(jù)name值反射持久化對象的屬性。此時,我們必須保證持久化對象中有對應(yīng)的屬性。4).在持久化類的方法中加入程序邏輯通過3)我們知道,如果access的值為property,hibernate直接反射持久化對象的方法。在這個方法中我們就可以加入程序邏輯。老徐舉了一個生動的例子,比如customer類中有firstname和lastname兩個屬性。但我們只想讓hibernate通過 getname方法獲得一個firstname+lastname的字符串,此時我們就可以在getname方法中將firstname與 last

10、name兩個屬性值合并為一個中間使用 “.”連接的字符串返回。使用hibernate獲取數(shù)據(jù)表中的數(shù)據(jù)時,hibernate會調(diào)用持久化對象的setname方法。我們在這個方法中將傳遞進來的參數(shù)使用“.”分隔,然后分別設(shè)置到firestname和lastname屬性中。5).hibernate的hql語句我們在使用jdbc、dbutil時使用的都是sql語句。但hibernate比較特殊,它使用的是自己的一套東西叫hql語句。比如我們調(diào)用session.find方法,傳遞的hql語句為:from customer as c where =itcast其中的customer指向持久化

11、對象的映射文件,name指向持久化對象的映射文件中的property元素的name屬性。此時需要注意access屬性的值。6).設(shè)置派生屬性property元素中,有一個formula屬性。它的值是一個sql表達式,hibernate將根據(jù)此表達式計算的值設(shè)置到持久化對象的屬性上。比如,我們要統(tǒng)計訂單表中的總計:hibernate一對多單向關(guān)系1. 數(shù)據(jù)庫schemateachers表:create table teachers(id number(10) not null,teachername varchar2(15)alter table teachersadd constraint d

12、ere primary key (id)students表:create table students(id number(10) not null,studentname varchar2(15),teacher_id number(10)alter table studentsadd constraint rere primary key (id)alter table studentsadd constraint fff foreign key (teacher_id)references teachers (id);2. teacher.java和student.javateacher

13、.javapackage mypack;public class teacher /教師idprivate long id;/教師名稱private string teachername;/* 缺省構(gòu)造函數(shù)*/public teacher() /* 得到教師id* return long 教師id*/public long getid() return id;/* 設(shè)置教師id* param id long 教師id*/public void setid(long id) this.id = id;/* 得到教師名稱* return string 教師名稱*/public string get

14、teachername() return teachername;/* 設(shè)置教師名稱* param teachername string 教師名稱*/public void setteachername(string teachername) this.teachername = teachername;/* 構(gòu)造函數(shù)* param teachername string*/public teacher(string teachername) this.teachername = teachername;student.javapackage mypack;public class studen

15、t /學生idprivate long id;/學生名稱private string studentname;/教師類private teacher teacher;/* 缺省構(gòu)造函數(shù)*/public student() /* 得到學生id* return long 學生id*/public long getid() return id;/* 設(shè)置學生id* param id long 學生id*/public void setid(long id) this.id = id;/* 得到學生名稱* return string 學生名稱*/public string getstudentname

16、() return studentname;/* 設(shè)置學生名稱* param studentname string 學生名稱*/public void setstudentname(string studentname) this.studentname = studentname;/* 得到教師對象* return teacher 教師對象*/public teacher getteacher() return teacher;/* 設(shè)置教師對象* param teacher teacher 教師對象*/public void setteacher(teacher teacher) this

17、.teacher = teacher;/* 構(gòu)造函數(shù)* param string string* param teacher teacher*/public student(string studentname, teacher teacher) this.studentname = studentname;this.teacher = teacher;3. perties# oraclehibernate.dialect net.sf.hibernate.dialect.oracle9dialecthibernate.dialect net.sf.hibernate

18、.dialect.oracledialecthibernate.connection.driver_class oracle.jdbc.driver.oracledriverhibernate.connection.username jbcmhibernate.connection.password jbcmhibernate.connection.url jdbc:oracle:thin:localhost:1521:wsy4. teacher.hbm.xml和student.hbm.xmlteacher.hbm.xmlstudent.hbm.xml5. 數(shù)據(jù)庫操作類businessserv

19、ice.javapackage mypack;import net.sf.hibernate.*;import net.sf.hibernate.cfg.configuration;import java.util.*;public class businessservice/session工廠類public static sessionfactory sessionfactory;/實始化session工廠statictry/建立配置類,添加student類和teacher類configuration config = new configuration();config.addclass(

20、student.class).addclass(teacher.class);/得到sessionfactory對象sessionfactory = config.buildsessionfactory();catch(exception e)e.printstacktrace();/* 通過學生類,查找教師類* param student student* throws exception* return list*/public list findteacherbystudent(student student) throws exceptionsession session = sess

21、ionfactory.opensession();transaction tx = null;try tx = session.begintransaction();list orders=(list)session.find(from student as o where o.teacher.id=+student.getid();mit();return orders;catch (exception e) if (tx != null) tx.rollback();throw e; finally session.close();/* 查找指定id的學生類* param st

22、udent_id long* throws exception* return student*/public student findstudent(long student_id) throws exceptionsession session = sessionfactory.opensession();transaction tx = null;try tx = session.begintransaction();student student=(student)session.load(student.class,new long(student_id);mit();r

23、eturn student;catch (exception e) if (tx != null) /發(fā)生錯誤,回滾tx.rollback();throw e; finally /沒有錯誤,關(guān)閉sessionsession.close();/* 級連保存teacher對象和student對象* throws exception*/public void saveteacherandstudentwithcascade() throws exceptionsession session = sessionfactory.opensession();transaction tx = null;tr

24、y tx = session.begintransaction();teacher teacher=new teacher(myteacher);student student1=new student(student1,teacher);student student2=new student(student2,teacher);session.save(student1);session.save(student2);mit();catch (exception e) if (tx != null) /發(fā)生錯誤,回滾tx.rollback();e.printstacktrace

25、(); finally / 沒有錯誤,關(guān)閉sessionsession.close();/* 保存教師和學生對象* throws exception*/public void saveteacherandstudent() throws exceptionsession session = sessionfactory.opensession();transaction tx = null;try tx = session.begintransaction();teacher teacher=new teacher(teacher1);session.save(teacher);student

26、 student1=new student(student001,teacher);student student2=new student(student002,teacher);session.save(student1);session.save(student2);/提交事務(wù)mit();catch (exception e) if (tx != null) /發(fā)生錯誤,回滾tx.rollback();throw e; finally / 沒有錯誤,關(guān)閉sessionsession.close();/* 輸出學生對象集合* param students list*/publi

27、c void printstudents(list students)for (iterator it = students.iterator(); it.hasnext();) student student=(student)it.next();system.out.println(ordernumber of +student.getteacher().getteachername()+ :+student.getstudentname();/* 測試方法* throws exception*/public void test() throws exceptionsaveteachera

28、ndstudent();/ saveteacherandstudentwithcascade();/ student student=findstudent(1);/ list students=findteacherbystudent(student);/ printstudents(students);public static void main(string args) throws exception new businessservice().test();sessionfactory.close();目錄結(jié)構(gòu)示意:classeshperty/mypackt

29、eacher.javastudent.javabusinessservice.javateacher.hbm.xmlstudent.hbm.xmlj2me中實現(xiàn)stringtokenizer的功能由于javame 中沒有stringtokenizer,而我們又經(jīng)常使用stringtokenizer的功能!而事實上,在rms的讀取數(shù)據(jù)過程中經(jīng)常會用到字符串的分割。這樣沒有辦法!我們就只好編寫一個類,代碼如下:ps:編譯一下,然后引入編譯器!寫個實例!運行可見結(jié)果!但需注意此類中方法的使用!import java.util.*;public class stringtokenizer implem

30、ents enumerationprivate void setmaxdelimchar()if(delimiters = null)maxdelimchar = 0;return;char c = 0;for(int i = 0; i delimiters.length(); i+)char c1 = delimiters.charat(i);if(c c1)c = c1;maxdelimchar = c;public stringtokenizer(string s, string s1, boolean flag)currentposition = 0;newposition = -1;

31、delimschanged = false;str = s;maxposition = s.length();delimiters = s1;retdelims = flag;setmaxdelimchar();public stringtokenizer(string s, string s1)this(s, s1, false);public stringtokenizer(string s)this(s, tnrf, false);private int skipdelimiters(int i)if(delimiters = null)throw new nullpointerexce

32、ption();int j;for(j = i; !retdelims & j maxdelimchar | delimiters.indexof(c) 0)break;return j;private int scantoken(int i)int j;for(j = i; j maxposition; j+)char c = str.charat(j);if(c = 0)break;if(retdelims & i = j)char c1 = str.charat(j);if(c1 = 0)j+;return j;public boolean hasmoretokens()newposit

33、ion = skipdelimiters(currentposition);return newposition maxposition;public string nexttoken()currentposition = newposition = maxposition)throw new nosuchelementexception(); elseint i = currentposition;currentposition = scantoken(currentposition);return str.substring(i, currentposition);public strin

34、g nexttoken(string s)delimiters = s;delimschanged = true;setmaxdelimchar();return nexttoken();public boolean hasmoreelements()return hasmoretokens();public object nextelement()return nexttoken();public int counttokens()int i = 0;for(int j = currentposition; j = maxposition)break;j = scantoken(j);i+;

35、return i;private int currentposition;private int newposition;private int maxposition;private string str;private string delimiters;private boolean retdelims;private boolean delimschanged;private char maxdelimchar;javaio讀取文件中文亂碼問題1、java讀取文件,避免中文亂碼。/* 讀取文件內(nèi)容* param filepathandname string 讀取文件路徑* return

36、 string 文件中的內(nèi)容*/public static string readfile(string filepathandname) string filecontent = ;try file f = new file(filepathandname);if(f.isfile()&f.exists()inputstreamreader read = new inputstreamreader(new fileinputstream(f),utf-8);bufferedreader reader=new bufferedreader(read);string line;while (li

37、ne = reader.readline() != null) filecontent += line;read.close(); catch (exception e) system.out.println(讀取文件內(nèi)容操作出錯);e.printstacktrace();return filecontent;2、java寫入文件,避免中文亂碼。/* 寫文件* param filepathandname string 寫文件路徑* param filecontent string 需要寫入的內(nèi)容*/public static void writefile(string filepathandn

38、ame, string filecontent) try file f = new file(filepathandname);if (!f.exists() f.createnewfile();outputstreamwriter write = new outputstreamwriter(new fileoutputstream(f),utf-8);bufferedwriter writer=new bufferedwriter(write);/printwriter writer = new printwriter(new bufferedwriter(new filewriter(f

39、ilepathandname);/printwriter writer = new printwriter(new filewriter(filepathandname);writer.write(filecontent);writer.close(); catch (exception e) system.out.println(寫文件內(nèi)容操作出錯);e.printstacktrace();javamap遍歷速度最優(yōu)解第一種:map map = new hashmap();iterator iter = map.entryset().iterator();while (iter.hasnex

40、t() map.entry entry = (map.entry) iter.next(); object key = entry.getkey();object val = entry.getvalue();效率高,以后一定要使用此種方式!第二種:map map = new hashmap();iterator iter = map.keyset().iterator();while (iter.hasnext() object key = iter.next();object val = map.get(key);效率低,以后盡量少使用!hashmap的遍歷有兩種常用的方法,那就是使用ke

41、yset及entryset來進行遍歷,但兩者的遍歷速度是有差別的,下面請看實例:public class hashmaptest public static void main(string args) .hashmap hashmap = new hashmap();for (int i = 0; i 1000; i ) .hashmap.put( i, thanks);long bs = calendar.getinstance().gettimeinmillis();iterator iterator = hashmap.keyset().iterator();while (iterat

42、or.hasnext() .system.out.print(hashmap.get(iterator.next();system.out.println();system.out.println(calendar.getinstance().gettimeinmillis() - bs);listhashmap();public static void listhashmap() .java.util.hashmap hashmap = new java.util.hashmap();for (int i = 0; i 1000; i ) .hashmap.put( i, thanks);l

43、ong bs = calendar.getinstance().gettimeinmillis();java.util.iterator it = hashmap.entryset().iterator();while (it.hasnext() .java.util.map.entry entry = (java.util.map.entry) it.next();/ entry.getkey() 返回與此項對應(yīng)的鍵/ entry.getvalue() 返回與此項對應(yīng)的值system.out.print(entry.getvalue();system.out.println();system

44、.out.println(calendar.getinstance().gettimeinmillis() - bs);對于keyset其實是遍歷了2次,一次是轉(zhuǎn)為iterator,一次就從hashmap中取出key所對于的value。而entryset只是遍歷了第一次,他把key和value都放到了entry中,所以就快了。注:hashtable的遍歷方法和以上的差不多!javascript中修改css1.js修改單個元素的css屬性document.getelementbyidx(obj).classname=”,document.getelementbyidx(obj).style.backgroundcolor=”#003366 ,2.js修改整個頁面的css屬性 3.js和css的style屬性對照表盒子標簽和屬性對照css語法 (不區(qū)分大小寫) javascript語法 (區(qū)分大小寫)border borderborder-bottom borderbottomborder-bottom-c

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論