<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 89, comments - 241, trackbacks - 0, articles - 1
       :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

         摘要: 源代碼以及下載  閱讀全文

    posted @ 2009-06-08 09:13 saobaolu| 編輯 收藏

         摘要: 小省小市小學校 大一小菜鳥的一個小小的課程設計  閱讀全文

    posted @ 2009-06-08 08:58 saobaolu| 編輯 收藏










     

    posted @ 2009-05-30 21:40 saobaolu| 編輯 收藏

    今日研究NetBeans欲完成課程設計學生管理系統,

    突然發現不會連接數據庫了……

    思索中,用jsp的方法去test了一下,感覺……對啊,老師曾經講過這個跟jsp鏈接數據庫是一樣的,巨汗…… =_=!

    Class.forName………

    con………..

    stm………..

    ——運行——報錯(ClassNotFoundException)——添加庫MySQL-JDBC

    應該沒錯了吧,但是……

    SQLException: No suitable driver for mysql://localhost:3306/stu

    而后發現了一個很恥辱的問題:

    con = DriverManager.getConnection("mysql://localhost:3306/stu","×","×");

    天啊,mysql://localhost:3306/stu,我竟然忘記寫 jdbc在mysql前面!~~

     --------------------------------------------------------

    中國移動老總上公廁,守門大爺說:進去3毛,出來2毛。
       老總一愣說:出來還收費?大爺說:學習移動,雙向收費。
       老總從廁所出來又被攔住:你蹲的是8號坑,交1元錢的選號費,放了一個屁,交1元的漫游費,超過3分鐘,再交1元的超時費。
       廁所有背景音樂,收彩鈴費2毛。
       大爺勸老總,如果你經常光顧,還是辦個廁所套餐比較合算。
       老總大怒:這是那家的王法!大爺一擺手:動感地帶,我的地盤我做主!
       祝大家端午節開心

    posted @ 2009-05-26 13:37 saobaolu| 編輯 收藏

    Problem A: University

    Time Limit: 1000MS
    Memory Limit: 65536K

    Total Submissions: 726
    Accepted: 422

    Description

    企鵝大學有很多很多名學生,課程也很多。譬如企鵝語,北極熊語,企鵝初級數學,企鵝高級數學等等等等…… 現在,企鵝大學的校長想請你為他們寫一個程序:計算總分的程序。請你由他們輸入課程的數量以及一個學生每門課程的分數,輸出該學生的總分。

    Input

    第一行:一個整數n(1 ≤ n ≤ 5000),代表企鵝大學的課程數目。
    第2至第n+1行:每行有一個1整數,第i+1行代表某一位學生在第i門課上取得的分數。分數為0到100的整數。

    Output

    一個整數。代表該學生在n門課中所得的總分。

    Sample Input

    3
    100
    99
    100
    

    Sample Output

    299
     

    Problem B: Doudou

    Time Limit: 1000MS
    Memory Limit: 65536K

    Total Submissions: 323
    Accepted: 75

    Description

    有只企鵝叫豆豆,總是被別的企鵝欺負。豆豆在長期的隱忍之后,掌握了所有企鵝的高度和攻擊力強度,還得到了一把黃金劍。在擁有了黃金劍以后,豆豆終于可以展開絕地大反擊。但這把黃金劍的用法卻很奇怪。

    首先,豆豆第一次可以選擇任何一只企鵝開始挑戰。豆豆這一次必勝。

    再次,當豆豆已經挑戰過某一只企鵝后,再下一次的挑戰對象只能是比上一名對手高,且比上一名對手攻擊力強的企鵝。這樣豆豆必勝。否則黃金劍會覺得打的沒意思而故意發脾氣輸掉。豆豆還會被大家集體暴打。

    面對著這把脾氣很大的黃金劍,豆豆想請你幫助他計算一下,他最多可以連續擊敗多少只企鵝?

    Input

    第一行:一個數據n,代表企鵝群里除了豆豆一共有n(1 ≤ n ≤ 1000)只企鵝。
    第2至第n+1行:每行2個數字。第i+1行的第一個數字為企鵝i的高度。第i+1行的第二個數字為企鵝i的攻擊力。0 ≤ 高度,攻擊力 ≤ 1,000,000。

    Output

    一個數。代表豆豆最多可以連續擊敗的企鵝數。

    Sample Input


    Sample Input #1
    Sample Input #2


    3
    1 3
    3 2
    2 4
    5
    10 1
    9 2
    7 3
    6 4
    5 5

    Sample Output


    Sample Output #1
    Sample Output #2


    2
    1

     

    Problem C: Ball

    Time Limit: 1000MS
    Memory Limit: 65536K

    Total Submissions: 6
    Accepted: 0

    Description

    給出空間上兩個運動的小球,球心坐標在分別是A(xa , ya , 0),B(xb , yb , 0),半徑分別為Ra , Rb,速度分別為Va( vax, vay , 0), Vb( vbx , vby, 0)。判斷兩個小球是否會碰撞,若會碰撞, 輸出首次碰撞時的時刻和兩個小球的坐標;若不會碰撞,輸出“Impossible”(球心和速度的z坐標恒為0,可將本題視為只是平面上的運動。初始時刻為0,若初始時刻小球貼在一起,視為首次碰撞)。

    Input

    第一行:一個整數T,(T ≤ 30),表示下面有T組數據。
    接下來,每兩行組成一組數據,首行包含5個實數,用空格隔開,依次是xa , ya , vax, vay , Ra,下面一行也包含5個實數,依次是xa , ya , vbx , vby, Rb。每組數據之間有一個空行。

    Output

    對于每組數據,如果兩個小球會碰撞,輸出首次碰撞時的時刻t,和兩個小球的坐標xap , yap , xbp , ybp ,用空格隔開,保留三位小數。如果不能,輸出“Impossible”。

    Sample Input

    3
    100 200 0 0 55
    100 100 0 0 45
    
    131 123 45 2 43
    454 230 0 -5 35
    
    100 100 1 1 31
    200 200 2 2 23
    

    Sample Output

    0.000 100.000 200.000 100.000 100.000
    6.179 409.053 135.358 454.000 199.105
    Impossible
    

     

    Problem D: String

    Time Limit: 1000MS
    Memory Limit: 65536K

    Total Submissions: 14
    Accepted: 0

    Description

    給定一個字符串S[1..n]和一個整數T,現在需要在字符串S中找出長度不小于T的一個子串,使得其在原串中不重疊出現的次數最多,求這個次數。

    Input

    第一行:一個整數T(T > 1)
    第二行:一個字符串S,且僅包含小寫字母,字符串長度不超過10000

    Output

    一個整數。代表出現最多的次數

    Sample Input

    2
    ababab
    

    Sample Output

    3

     

    Problem E: Papercut

    Time Limit: 1000MS
    Memory Limit: 65536K

    Total Submissions: 3
    Accepted: 0

    Description

    現在桌面上有一張矩形紙,上邊有n×m個格子,每個格子有一個數字。 每張矩形紙可以算出一個數值F,F是由紙張里任意兩個不同的格子里的數字相乘之和。如果該紙只有一個格子,那么F=0。

    剪紙規則是:
    1、沿格子邊緣一直剪成兩個矩形紙,每張紙里必須有數字。
    2、每次剪紙在桌面上任意選一張矩形紙,進行1操作,再把剪出來的兩張紙放到桌面。

    現在你可以對桌面上的紙最多剪k次,問最后桌面上所有矩形紙的F值之和最小是多少?

    1 ≤ n ≤ 10
    1 ≤ m ≤ 10
    1 ≤ k ≤ 50

    Input

    第一行:3個整數n, m, k
    接下來n行:每行m個正整數,范圍在[1,10],第i行第j個數表示當前桌面那張矩形紙里邊第i行第j個格子里的數字。

    Output

    一個整數。代表最小F值和。

    Sample Input


    Sample Input #1
    Sample Input #2


    4 4 4
    1 1 1 1
    1 1 1 1
    1 1 1 1
    1 1 1 1
     
    10 10 5
    4 2 3 5 6 10 1 6 5 8
    3 6 9 1 7 10 7 10 8 1
    7 8 3 3 2 5 9 9 8 2
    5 5 9 9 3 10 2 9 10 2
    1 1 6 7 6 8 3 9 6 8
    7 1 2 5 3 2 3 7 8 10
    10 9 8 9 7 8 10 7 3 9
    6 3 6 2 1 7 10 6 7 2
    2 4 8 4 5 9 10 5 9 10
    7 4 3 2 4 9 9 9 8 1

    Sample Output


    Sample Output #1
    Sample Output #2


    18
    26612

    Hint

    Sample 1說明: 按照下面方式剪紙4次
    1   1   1   1
    -------------
    1 | 1 | 1 | 1
       |   |   |
    1 | 1 | 1 | 1
       |   |   |
    1 | 1 | 1 | 1
    得到6+3+3+3+3=18

    posted @ 2009-05-09 14:31 saobaolu| 編輯 收藏

    <script language="javascript">
    function checkForm(form){
        if(isEmpty(form.uid.value)||isEmpty(form.password.value)||isEmpty(form.confirmPassword.value)||isEmpty(form.email.value)||isEmpty(form.quesstion.value)||isEmpty(form.answer.value)||){
            alert("請將表單信息填寫完整!");
            return false;
        }
        if(form.password.value!=form.confirmPassword.value){
            alert("兩次密碼不相同!");
            return false;
        }
        if(form.password.value.length<6||form.password.value.length>16){
            alert("密碼長度不合法!");
            return false;
        }
        return true;
    }
    function isEmpty(str){
    if(str=null||str.length==0)return true;
    else return false;
    }

    </script>

    扣錯!

    錯在

    if(isEmpty(form.uid.value)||isEmpty(form.password.value)||isEmpty(form.confirmPassword.value)||isEmpty(form.email.value)||isEmpty(form.quesstion.value)||isEmpty(form.answer.value)||){
    這里錯了:isEmpty(form.answer.value)||){

    意思就是無論如何都是會返回一個true的!

    怪不得我在form表單里添加這個函數沒作用,哎,以后還是小心為好啊!

    posted @ 2009-05-09 10:22 saobaolu| 編輯 收藏

    ubuntu裝好之后, 為瀏覽器firefox安裝flash插件, 后來發現中文會變成方框。

    如何解決?

    輸入:
    cd /etc/fonts/conf.d/

    為了安全,備份一下:

    sudo cp 49-sansserif.conf 49-sansserif.conf_backup

    輸入如下指令:

    sudo gedit ./49-sansserif.conf

    此時文件顯示內容。

    將其中的第1、2、4個后面的sans-serif或者serif用你自己系統中支持中文的字體的名字代替,注意字體名字的大小寫

    比如:我的系統中安裝了微軟雅黑,我則用微軟雅黑代替上述所說的字段,結果如下:

        <match target="pattern">
            <test qual="all" name="family" compare="not_eq">
                <string>宋體</string>
            </test>
            <test qual="all" name="family" compare="not_eq">
                <string>宋體</string>
            </test>
            <test qual="all" name="family" compare="not_eq">
                <string>monospace</string>
            </test>
            <edit name="family" mode="append_last">
                <string>宋體</string>
            </edit>
        </match>

    posted @ 2009-05-06 07:28 saobaolu| 編輯 收藏

    文件上傳的時候,報錯

     1Status 500 - 
     2
     3--------------------------------------------------------------------------------
     4
     5type Exception report
     6
     7message 
     8
     9description The server encountered an internal error () that prevented it from fulfilling this request.
    10
    11exception 
    12
    13org.apache.jasper.JasperException: An exception occurred processing JSP page /upload.jsp at line 302
    14
    15299:         if (!myFile.isMissing()) {
    16300:             FileName=(String)date.Time_Stamp();
    17301:             sOriginalFileName=myFile.getFileName();
    18302:             myFile.saveAs("/"+sUploadDir+FileName+"."+myFile.getFileExt());
    19303:             sSaveFileName=FileName+"."+myFile.getFileExt();
    20304:             sPathFileName=sUploadDir+sSaveFileName;
    21305:             System.out.println(sPathFileName);
    22
    23
    24Stacktrace:
    25    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
    26    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
    27    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    28    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    29    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    30    sun.reflect.GeneratedMethodAccessor18.invoke(Unknown Source)
    31    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    32    java.lang.reflect.Method.invoke(Method.java:597)
    33    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
    34    java.security.AccessController.doPrivileged(Native Method)
    35    javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
    36    org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
    37    org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
    38
    39
    40root cause 
    41
    42java.security.AccessControlException: access denied (java.io.FilePermission \UploadFile read)
    43    java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
    44    java.security.AccessController.checkPermission(AccessController.java:546)
    45    java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
    46    java.lang.SecurityManager.checkRead(SecurityManager.java:871)
    47    java.io.File.exists(File.java:731)
    48    com.jspsmart.upload.SmartUpload.getPhysicalPath(SmartUpload.java:1067)
    49    com.jspsmart.upload.File.saveAs(File.java:91)
    50    com.jspsmart.upload.File.saveAs(File.java:65)
    51    org.apache.jsp.upload_jsp._jspService(upload_jsp.java:376)
    52    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    53    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    54    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    55    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    56    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    57    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    58    sun.reflect.GeneratedMethodAccessor18.invoke(Unknown Source)
    59    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    60    java.lang.reflect.Method.invoke(Method.java:597)
    61    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
    62    java.security.AccessController.doPrivileged(Native Method)
    63    javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
    64    org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
    65    org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
    66
    67
    68note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
    69
    70
    71--------------------------------------------------------------------------------
    72
    73Apache Tomcat/6.0.18

    正常上傳tomcat的提示是:
    gif|jpg|jpeg|bmp|GIF|JPG|JPEG|BMP500
    UploadFile/20090505063838906.jpg
    異常的時候是:
    gif|jpg|jpeg|bmp|GIF|JPG|JPEG|BMP500
    UploadFile/
    苦惱中…………

    posted @ 2009-05-05 19:13 saobaolu| 編輯 收藏

    哎,說來真是點背,8.10升級到9.04之后因為顯卡原因不能用,奈何從網上又down一個ubuntu9.04

    運行wubi.exe,沒有反應,運行,還沒有反應,

    while(see the article in cnbeta)

    {

    success;

    break;

    }

    失敗原因:盤符名中含有中文。
    解決方法:將中文改為英文就可以了。如,原先為"系統盤(C:)",改為"system(C:)"。其他也是類似的改法。注意,所有的盤符都不可以含有中文!

    可能是做wubi的程序員考慮欠周全所致,希望以后的版本不會再這樣。
    如果你已經雙擊了wubi,應先清除臨時文件,路徑為C:\Documents and Settings\Administrator\Local Settings\Temp(假設你的系統盤是C盤,并且是Administrator)。清除后,為了保險起見,重啟后再運行wubi。

     

    via cnbeta~

    posted @ 2009-05-02 12:30 saobaolu| 編輯 收藏

    Redwood Shores, CA - April 20, 2009

    Oracle Corporation (NASDAQ: ORCL) and Sun Microsystems (NASDAQ: JAVA) announced today they have entered into a definitive agreement under which Oracle will acquire Sun common stock for $9.50 per share in cash. The transaction is valued at approximately $7.4 billion, or $5.6 billion net of Sun’s cash and debt. “We expect this acquisition to be accretive to Oracle’s earnings by at least 15 cents on a non-GAAP basis in the first full year after closing. We estimate that the acquired business will contribute over $1.5 billion to Oracle’s non-GAAP operating profit in the first year, increasing to over $2 billion in the second year. This would make the Sun acquisition more profitable in per share contribution in the first year than we had planned for the acquisitions of BEA, PeopleSoft and Siebel combined,” said Oracle President Safra Catz.

    “The acquisition of Sun transforms the IT industry, combining best-in-class enterprise software and mission-critical computing systems,” said Oracle CEO Larry Ellison. “Oracle will be the only company that can engineer an integrated system – applications to disk – where all the pieces fit and work together so customers do not have to do it themselves. Our customers benefit as their systems integration costs go down while system performance, reliability and security go up.”

    There are substantial long-term strategic customer advantages to Oracle owning two key Sun software assets: Java and Solaris. Java is one of the computer industry’s best-known brands and most widely deployed technologies, and it is the most important software Oracle has ever acquired. Oracle Fusion Middleware, Oracle’s fastest growing business, is built on top of Sun’s Java language and software. Oracle can now ensure continued innovation and investment in Java technology for the benefit of customers and the Java community.

    The Sun Solaris operating system is the leading platform for the Oracle database, Oracle’s largest business, and has been for a long time. With the acquisition of Sun, Oracle can optimize the Oracle database for some of the unique, high-end features of Solaris. Oracle is as committed as ever to Linux and other open platforms and will continue to support and enhance our strong industry partnerships.

    “Oracle and Sun have been industry pioneers and close partners for more than 20 years,” said Sun Chairman Scott McNealy. “This combination is a natural evolution of our relationship and will be an industry-defining event.”

    “This is a fantastic day for Sun’s customers, developers, partners and employees across the globe, joining forces with the global leader in enterprise software to drive innovation and value across every aspect of the technology marketplace,” said Jonathan Schwartz, Sun’s CEO, “From the Java platform touching nearly every business system on earth, powering billions of consumers on mobile handsets and consumer electronics, to the convergence of storage, networking and computing driven by the Solaris operating system and Sun’s SPARC and x64 systems. Together with Oracle, we’ll drive the innovation pipeline to create compelling value to our customer base and the marketplace.”

    “Sun is a pioneer in enterprise computing, and this combination recognizes the innovation and customer success the company has achieved. Our largest customers have been asking us to step up to a broader role to reduce complexity, risk and cost by delivering a highly optimized stack based on standards,” said Oracle President Charles Phillips. “This transaction will preserve and enhance investments made by our customers, while we continue to work with our partners to provide customers with choice.”

    The Board of Directors of Sun Microsystems has unanimously approved the transaction. It is anticipated to close this summer, subject to Sun stockholder approval, certain regulatory approvals and customary closing conditions.

    There will be a conference call today to discuss the transaction at 5:30 a.m. Pacific time. Investors can listen to the conference call by dialing (719) 234-7870, passcode 923645. A replay will be available for 24 hours after the call ends at (719) 884-8882, passcode: 923645. A live audio webcast of the call will be made available at www.oracle.com/investor and a replay will be available for seven days after the call ends.

    About Sun Microsystems

    Sun Microsystems, Inc. (NASDAQ: JAVA) develops the technologies that power the global marketplace. Guided by a singular vision -- "The Network is the Computer" -- Sun drives network participation through shared innovation, community development and open source leadership. Sun can be found in more than 100 countries and on the Web at http://www.sun.com.

    About Oracle

    Oracle (NASDAQ: ORCL) is the world's largest enterprise software company. For more information about Oracle, please visit our Web site at http://www.oracle.com.

    Trademarks

    Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

    Sun, Sun Microsystems, the Sun logo, Java, Solaris and the Network is the Computer are trademarks or registered trademarks of Sun Microsystems, Inc. or its subsidiaries in the United States and other countries

    Cautionary Statement Regarding Forward-Looking Statements

    This press release contains certain forward-looking statements about Oracle and Sun, including statements that involve risks and uncertainties concerning Oracle's proposed acquisition of Sun, anticipated product information, estimates of future results of operations and general business outlook. When used in this press release, the words "anticipates", “estimates”, "may", "can", “will”, "believes", "expects", "projects", "intends", "likely", similar expressions and any other statements that are not historical facts are intended to identify those assertions as forward-looking statements. Any such statement may be influenced by a variety of factors, many of which are beyond the control of Oracle or Sun, that could cause actual outcomes and results to be materially different from those projected, described, expressed or implied in this press release due to a number of risks and uncertainties. Potential risks and uncertainties include, among others, the possibility that the transaction will not close or that the closing may be delayed, the anticipated synergies of the combined companies may not be achieved after closing, the combined operations may not be successfully integrated in a timely manner, if at all, general economic conditions in regions in

    which either company does business, and the possibility that Oracle or Sun may be adversely affected by other economic, business, and/or competitive factors. Accordingly, no assurances can be given that any of the events anticipated by the forward-looking statements will transpire or occur, or if any of them do so, what impact they will have on the results of operations or financial condition of Oracle or Sun.

    In addition, please refer to the documents that Oracle and Sun, respectively, file with the Securities and Exchange Commission (the “SEC”) on Forms 10-K, 10-Q and 8-K. These filings identify and address other important factors that could cause Oracle's and Sun's respective financial and operational results to differ materially from those contained in the forward-looking statements set forth in this document. You are cautioned to not place undue reliance on forward-looking statements, which speak only as of the date of this report. Neither Oracle nor Sun is under any duty to update any of the information in this release.

    Additional Information about the Merger and Where to Find It

    In connection with the proposed merger, Sun will file a proxy statement with the SEC. Additionally, Sun and Oracle will file other relevant materials in connection with the proposed acquisition of Sun by Oracle pursuant to the terms of an Agreement and Plan of Merger by and among Oracle, Soda Acquisition Corporation, a wholly-owned subsidiary of Oracle, and Sun. The materials to be filed by Sun with the SEC may be obtained free of charge at the SEC's web site at www.sec.gov. Investors and security holders of Sun are urged to read the proxy statement and the other relevant materials when they become available before making any voting or investment decision with respect to the proposed merger because they will contain important information about the merger and the parties to the merger.

    Oracle, Sun and their respective directors, executive officers and other members of its management and employees, under SEC rules, may be deemed to be participants in the solicitation of proxies of Sun stockholders in connection with the proposed merger. Investors and security holders may obtain more detailed information regarding the names, affiliations and interests of certain of Oracle's executive officers and directors in the solicitation by reading the proxy statement and other relevant materials filed with the SEC when they become available. Information concerning the interests of Sun's participants in the solicitation, which may, in some cases, be different than those of Sun's stockholders generally, is set forth in the materials filed with the SEC on Form 10-K and will be set forth in the proxy statement relating to the merger when it becomes available.

    Contact Info

    Karen Tillman
    Oracle Corporate Communications
    +1.650.607.0326
    karen.tillman@oracle.com

    Roy Lobo
    Oracle Investor Relations
    +1.650.506.4073
    investor_us@oracle.com

    posted @ 2009-04-21 18:08 saobaolu| 編輯 收藏

         摘要:   閱讀全文

    posted @ 2009-04-17 03:13 saobaolu| 編輯 收藏

    http://mysql.ntu.edu.tw/Downloads/Connector-J/mysql-connector-java-5.0.8.zip

    posted @ 2009-04-05 14:06 saobaolu| 編輯 收藏

      應用J2EE平臺開發的系統的性能是系統使用者和開發者都關注的問題,本文從服務器端編程時應注意的幾個方面討論代碼對性能的影響,并總結一些解決的建議。關鍵詞:性能,Java,J2EE,EJB,Servlet,JDBC

    一、概要

    Java 2 Platform, Enterprise Edition (J2EE)是當前很多商業應用系統使用的開發平臺,該技術提供了一個基于組件的方法來設計、開發、裝配和部署企業級應用程序。J2EE平臺提供了一個多層結構的分布式的應用程序模型,可以更快地開發和發布的新的應用解決方案。 

    J2EE是一種技術規范,定義了整個標準的應用開發體系結構和一個部署環境,應用開發者開發時只要專注于具體商業邏輯和商業業務規則的實現上,而其他的諸如事務、持久化、安全等系統開發問題可以由應用程序容器或者服務器處理,開發完成后,就可以方便地部署到實現規范的應用服務器中。 

    作為網絡上的商業應用系統,同時訪問的人數是很多的,在大量訪問的情況下,過多的資源請求和有限的服務器資源(內存、CPU時間、網絡帶寬等)之間就會出現矛盾,應用系統的性能就顯得很重要了,有時正確的代碼并不能保證項目的成功,性能往往是最后決定一個項目是否成功關鍵。

    本文主要從性能的角度出發,討論J2EE服務器端的代碼性能優化和提升。 

    二、常見的Java 編程 J2EE語言基礎是Java,常用的Java代碼問題對應用系統的性能影響,

    下面討論了一些應該注意方面。 

    使用StringBuffer代替String 當處理字符串的相加時,常見的寫法是:

    String str1 = "Hello";
    String str2 = "welcome to world";
    String str3 = str1 + ", " + str2 +"!";
    System.out.println(str3);

    很多人都知道,這樣的代碼效率是很低的,因為String是用來存儲字符串常量的,如果要執行“+”的操作,系統會生成一些臨時的對象,并對這些對象進行管理,造成不必要的開銷。

      如果字符串有連接的操作,替代的做法是用StringBuffer類的append方法,它的缺省構造函數和append的實現是:

    public StringBuffer() { // 構造函數
    this(16); // 缺省容量16}

    public synchronized StringBuffer append(String str) {
     if (str == null) {
      str = String.valueOf(str);
     }

     int len =str.length();
     int newcount = count + len;
     if(newcount > value.length)

     expandCapacity(newcount);

     // 擴充容量
     str.getChars(0, len, value, count);
     count = newcount;
     return this;
    }

      當字符串的大小超過缺省16時,代碼實現了容量的擴充,為了避免對象的重新擴展其容量,更好的寫法為:

    StringBuffer buffer = new StringBuffer(30);
    // 分配指定的大小。
    buffer.append("hello");
    buffer.append(",");
    buffer.append("welcometo world!");
    String str = buffer.toString();

      ·生成對象時,分配合理的空間和大小

      Java中的很多類都有它的默認的空間分配大小,對于一些有大小的對象的初始化,應該預計對象的大小,然后使用進行初始化,上面的例子也說明了這個問題,StringBuffer創建時,我們指定了它的大小。

      另外的一個例子是Vector,當聲明Vector vect=new Vector()時,系統調用:

    public Vector() {// 缺省構造函數
     this(10); // 容量是 10;
    }

      缺省分配10個對象大小容量。當執行add方法時,可以看到具體實現為:..

    public synchronized boolean add(Object o) {
     modCount++;
     ensureCapacityHelper(elementCount+1);
     elementData[elementCount++] =o;

     return true;
    }

    private void ensureCapacityHelper(int minCapacity) {
     int oldCapacity = elementData.length;
     if (minCapacity > oldCapacity) {
      Object oldData[] = elementData;
      int newCapacity = (capacityIncrement > 0) ? (oldCapacity + capacityIncrement) :
    (oldCapacity * 2);
      if (newCapacity < minCapacity) {
       newCapacity = minCapacity;
      }
      elementData = new Object[newCapacity];
      System.arraycopy(oldData, 0, elementData, 0, elementCount);
     }
    }

      我們可以看到,當Vector大小超過原來的大小時,一些代碼的目的就是為了做容量的擴充,在預先知道該Vector大小的話,可以指定其大小,避免容量擴充的開銷,如知道Vector大小為100時,初始化是就可以象這樣。

    Vector vect =.. new Vector(100);

      ·優化循環體

      循環是比較重復運行的地方,如果循環次數很大,循環體內不好的代碼對效率的影響就會被放大而變的突出。考慮下面的代碼片:..

    Vector vect = new Vector(1000);
    ...
    for( inti=0; i<vect.size(); i++){
     ...
    }

      for循環部分改寫成:

    int size = vect.size();
    for( int i=0; i>size; i++){
     ...
    }

      如果size=1000,就可以減少1000次size()的系統調用開銷,避免了循環體重復調用。

      再看如下的代碼片:..

    for (int i = 0;i <100000;i++)
    if (i%10 == 9) {
     ... // 每十次執行一次
    }

      改寫成也可以提高效率:..

    for(inti =0,j =10; i<100000; i++,j--){
     if(j == 0){
      ... // 每十次執行一次
      j = 10;
     }
    }

      所以,當有較大的循環時,應該檢查循環內是否有效率不高的地方,尋找更優的方案加以改進。

      ·對象的創建

      盡量少用new來初始化一個類的實例,當一個對象是用new進行初始化時,其構造函數鏈的所有構造函數都被調用到,所以new操作符是很消耗系統資源的,new一個對象耗時往往是局部變量賦值耗時的上千倍。同時,當生成對象后,系統還要花時間進行垃圾回收和處理。

      當new創建對象不可避免時,注意避免多次的使用new初始化一個對象。

      盡量在使用時再創建該對象。如:

    NewObject object = new NewObject();
    int value;
    if(i>0 )
    {
     value =object.getValue();
    }

      可以修改為:

    int value;
    if(i>0 )
    {
     NewObject object = new NewObject();
     Value =object.getValue();
    }

      另外,應該盡量重復使用一個對象,而不是聲明新的同類對象。一個重用對象的方法是改變對象的值,如可以通過setValue之類的方法改變對象的變量達到重用的目的。

      ·變量的注意事項

      盡量使用局部變量,調用方法時傳遞的參數以及在調用中創建的臨時變量都保存在棧(Stack) 中,速度較快。其他變量,如靜態變量、實例變量等,都在堆(Heap)中創建,速度較慢。

      盡量使用靜態變量,即加修飾符static,如果類中的變量不會隨他的實例而變化,就可以定義為靜態變量,從而使他所有的實例都共享這個變量。

      ·方法(Method)調用

      在Java中,一切都是對象,如果有方法(Method)調用,處理器先要檢查該方法是屬于哪個對象,該對象是否有效,對象屬于什么類型,然后選擇合適的方法并調用。

      可以減少方法的調用,同樣一個方法:

    public void CallMethod(int i ){
     if( i ==0 ){
      return;
     }
     ... // 其他處理
    }

      如果直接調用,

    int i = 0;
    ...
    CallMethod(i);

      就不如寫成:

    int i = 0;
    ...

    if( i ==0 ){
     CallMethod(i);
    }

      不影響可讀性等情況下,可以把幾個小的方法合成一個大的方法。

      另外,在方法前加上final,private關鍵字有利于編譯器的優化。

      ·慎用異常處理

      異常是Java的一種錯誤處理機制,對程序來說是非常有用的,但是異常對性能不利。拋出異常首先要創建一個新的對象,并進行相關的處理,造成系統的開銷,所以異常應該用在錯誤處理的情況,不應該用來控制程序流程,流程盡量用while,if等處理。

      在不是很影響代碼健壯性的前提下,可以把幾個try/catch塊合成一個。

      ·同步

      同步主要出現在多線程的情況,為多線程同時運行時提供對象數據安全的機制,多線程是比較復雜話題,應用多線程也是為了獲得性能的提升,應該盡可能減少同步。

      另外,如果需要同步的地方,可以減少同步的代碼段,如只同步某個方法或函數,而不是整個代碼。

      ·使用Java系統API

      Java的API一般都做了性能的考慮,如果完成相同的功能,優先使用API而不是自己寫的代碼,如數組復制通常的代碼如下:

    int size = 1000;
    String[] strArray1 = new String[size];
    String[] strArray2 = new String[size];
    for(inti=0;i<size;i++){ // 賦值
     strArray1[i] = (new String("Array: " + i));
    }

    for(inti=0;i<size;i++){ // 復制
     strArray2[i]=(new String((String)a[i]));
    }

      如果使用Java提供的API,就可以提高性能:

    int size = 1000;
    String[] strArray1 = new String[size];
    String[] strArray2 = new String[size];
    for(inti=0;i<size;i++){ // 賦值
    strArray1[i] = (new String("Array: " + i));
    }

    System.arraycopy(strArray1,0,strArray2,0,size); // 復制

      同樣的一個規則是,當有大量數據的復制時,應該使用System.arraycopy()。
      三、I/O 性能

      輸入/輸出(I/O)包括很多方面,我們知道,進行I/O操作是很費系統資源的。程序中應該盡量少用I/O操作。使用時可以注意: . 合理控制輸出函數System.out.println()對于大多時候是有用的,特別是系統調試的時候,但也會產生大量的信息出現在控制臺和日志上,同時輸出時,有序列化和同步的過程,造成了開銷。

      特別是在發行版中,要合理的控制輸出,可以在項目開發時,設計好一個Debug的工具類,在該類中可以實現輸出開關,輸出的級別,根據不同的情況進行不同的輸出的控制。

      ·使用緩存

      讀寫內存要比讀寫文件要快很多,應盡可能使用緩沖。

      盡可能使用帶有Buffer的類代替沒有Buffer的類,如可以用BufferedReader 代替Reader,用BufferedWriter代替Writer來進行處理I/O操作。

      同樣可以用BufferedInputStream代替InputStream都可以獲得性能的提高。

      四、Servlet

      Servlet采用請求——響應模式提供Web服務,通過ServletResponse以及ServletRequest這兩個對象來輸出和接收用戶傳遞的參數,在服務器端處理用戶的請求,根據請求訪問數據庫、訪問別的Servlet方法、調用EJB等等,然后將處理結果返回給客戶端。

      ·盡量不使用同步

      Servlet是多線程的,以處理不同的請求,基于前面同步的分析,如果有太多的同步就失去了多線程的優勢了。

      ·不用保存太多的信息在HttpSession中

      很多時候,存儲一些對象在HttpSession中是有必要的,可以加快系統的開發,如網上商店系統會把購物車信息保存在該用戶的Session中,但當存儲大量的信息或是大的對象在會話中是有害的,特別是當系統中用戶的訪問量很大,對內存的需求就會很高。

      具體開發時,在這兩者之間應作好權衡。

      ·清除Session

      通常情況,當達到設定的超時時間時,同時有些Session沒有了活動,服務器會釋放這些沒有活動的Session,.. 不過這種情況下,特別是多用戶并訪時,系統內存要維護多個的無效Session。

      當用戶退出時,應該手動釋放,回收資源,實現如下:..

    HttpSession theSession = request.getSession();
    // 獲取當前Session
    if(theSession != null){
     theSession.invalidate(); // 使該Session失效
    }

      五、EJB 問題

      EJB是Java服務器端服務框架的規范,軟件廠商根據它來實現EJB服務器。應用程序開發者可以專注于支持應用所需的商業邏輯,而不用擔心周圍框架的實現問題。EJB規范詳細地解釋了一些最小但是必須的服務,如事務,安全和名字等。

      ·緩存Home接口

      EJB庫使用Enterprise Bean 的客戶端通過它的Home接口創建它的實例。客戶端能通過JNDI訪問它。服務器通過Lookup方法來獲取。

      JNDI是個遠程對象,通過RMI方式調用,對它的訪問往往是比較費時的。所以,在設計時可以設計一個類專門用來緩存Home接口,在系統初始化時就獲得需要的Home接口并緩存,以后的引用只要引用緩存即可。

      ·封裝Entity Bean

      直接訪問Entity Bean是個不好的習慣,用會話Bean封裝對實體Bean的訪問能夠改進事務管理,因為每一個對get方法的直接調用將產生一個事務,容器將在每一個實體Bean的事務之后執行一個“Load-Store”.. 操作。

      最好在Session Bean中完成Entity Bean的封裝,減少容器的事務處理,并在Session Bean中實現一些具體的業務方法。

      ·釋放有狀態的Session Bean

      相當于HttpSession,當把一個Session Bean設為Stateful,即有狀態的Session Bean 后,應用容器(Container)就可能有“鈍化”(Passivate)和活化(Activate)過程,即在主存和二級緩存之間對SessionBean進行存儲位置的轉移,在這個過程中,存在序列化過程。

      通常有狀態Session Bean的釋放是在超時時發生,容器自動的清除該對象,但是如果交給容器管理,一方面可能產生對象鈍化,另一方面未超時期間,系統還要 維護一份該對象,所以如果我們確認使用完該StatefulSession Bean后不再需要時,可以顯式的將其釋放掉,方法是調用:

    theSesionBean.remove();

      六、數據庫訪問

      在J2EE開發的應用系統中,數據庫訪問一般是個必備的環節。數據庫用來存儲業務數據,供應用程序訪問。

      在Java技術的應用體系中,應用程序是通過JDBC(Java Database Connectivity)實現的接口來訪問數據庫的,JDBC支持“建立連接、SQL語句查詢、處理結果”等基本功能。在應用JDBC接口訪問數據庫的過程中,只要根據規范來實現,就可以達到要求的功能。

      但是,有些時候進行數據查詢的效率著實讓開發人員不如所愿,明明根據規范編寫的程序,運行效果卻很差,造成整個系統的執行效率不高。

      ·使用速度快的JDBC驅動

      JDBC API包括兩種實現接口形式,一種是純Java實現的驅動,一種利用ODBC驅動和數據庫客戶端實現,具體有四種驅動模式并各有不同的應用范圍,針對不同的應用開發要選擇合適的JDBC驅動,在同一個應用系統中,如果選擇不同的JDBC驅動,在效率上會有差別。

      例如,有一個企業應用系統,不要求支持不同廠商的數據庫,這時就可以選擇模式4的JDBC驅動,該驅動一般由數據庫廠商實現的基于本地協議的驅動,直接調用數據庫管理系統使用的協議,減少了模式3中的中間層。

      ·使用JDBC連接池

      為了提高訪問數據庫的性能,我們還可以使用JDBC 2.0的一些規范和特性,JDBC是占用資源的,在使用數據庫連接時可以使用連接池Connection Pooling,避免頻繁打開、關閉Connection。而我們知道,獲取Connection是比較消耗系統資源的。

      Connection緩沖池是這樣工作的:當一個應用程序關閉一個數據庫連接時,這個連接并不真正釋放而是被循環利用,建立連接是消耗較大的操作,循環利用連接可以顯著的提高性能,因為可以減少新連接的建立。

      一個通過DataSource獲取緩沖池獲得連接,并連接到一個CustomerDB數據源的代碼演示如下:

    Context ctx = new InitialContext();
    DataSource dataSource = (DataSource) ctx.lookup("jdbc/CustomerDB");
    Connection conn = dataSource.getConnection("password","username");

      ·緩存DataSource

      一個DataSource對象代表一個實際的數據源。這個數據源可以是從關系數據庫到表格形式的文件,完全依賴于它是怎樣實現的,一個數據源對象注冊到JNDI名字服務后,應用程序就可以從JNDI服務器上取得該對象,并使用之和數據源建立連接。

      通過上面的例子,我們知道DataSource是從連接池獲得連接的一種方式,通過JNDI方式獲得,是占用資源的。

      為了避免再次的JNDI調用,可以系統中緩存要使用的DataSource。

      ·關閉所有使用的資源

      系統一般是并發的系統,在每次申請和使用完資源后,應該釋放供別人使用,數據庫資源每個模式的含義可以參考SUN JDBC的文檔,不同是比較寶貴的,使用完成后應該保證徹底的釋放。

      請看下面的代碼段:

    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
     DataSource dataSource = getDataSource();
     // 取的DataSource的方法,實現略。
     conn = datasource.getConnection();
     stmt = conn.createStatement();
     rs = stmt.executeQuery("SELECT * FROM ...");
     ... // 其他處理
     rs.close();
     stmt.close();
     conn.close();
    }catch (SQLException ex) {
     ... // 錯誤處理
    }

      粗看似乎沒有什么問題,也有關閉相關如Connection等系統資源的代碼,但當出現異常后,關閉資源的代碼可能并不被執行,為保證資源的確實已被關閉,應該把資源關閉的代碼放到finally塊:

    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
     DataSource dataSource = getDataSource();
     // 取的DataSource的方法,實現略。
     conn = datasource.getConnection();
     stmt = conn.createStatement();
     rs = stmt.executeQuery("SELECT * FROM ...");

     ... // 其他處理
    }catch (SQLException ex) {
     ... // 錯誤處理

    }finally{
     if (rs!=null) {
      try {
       rs.close(); // 關閉ResultSet}
      catch (SQLException ex) {
       ... // 錯誤處理
      }
     }

     if (stmt!=null){
      try {
       stmt.close(); // 關閉Statement}
      catch (SQLException ex) {
       ... // 錯誤處理
      }
     }
     if (conn!=null){
      try {
       conn.close(); // 關閉Connection}
      catch (SQLException ex) {
       ... // 錯誤處理
      }
     }
    }

      ·大型數據量處理

      當我們在讀取諸如數據列表、報表等大量數據時,可以發現使用EJB的方法是非常慢的,這時可以使用直接訪問數據庫的方法,用SQL直接存取數據,從而消除EJB的經常開支(例如遠程方法調用、事務管理和數據序列化,對象的構造等)。

      ·緩存經常使用的數據

      對于構建的業務系統,如果有些數據要經常要從數據庫中讀取,同時,這些數據又不經常變化,這些數據就可以在系統中緩存起來,使用時直接讀取緩存,而不用頻繁的訪問數據庫讀取數據。

      緩存工作可以在系統初始化時一次性讀取數據,特別是一些只讀的數據,當數據更新時更新數據庫內容,同時更新緩存的數據值。

      一個例子是,在一套企業應用系統中,企業的信息數據(如企業的名稱)在多個業務應用模塊中使用,這時就可以把這些數據緩存起來,需要時直接讀取緩存的企業信息數據。

      七、總結

      一般意義上說,參與系統運行的代碼都會對性能產生影響,實際應用中應該養成良好的編程規范、編寫高質量的代碼,當系統性能出現問題時,要找到主要影響性能的瓶頸所在,然后集中精力優化這些代碼,能達到事半功倍的效果。

      J2EE性能的優化包括很多方面的,要達到一個性能優良的系統,除了關注代碼之外,還應該根據系統實際的運行情況,從服務器軟硬件環境、集群技術、系統構架設計、系統部署環境、數據結構、算法設計等方面綜合考慮。

    posted @ 2009-04-04 09:16 saobaolu| 編輯 收藏

         摘要: 第二種,JavaBean的形式。 package conn;                            //導入包 ...  閱讀全文

    posted @ 2009-04-03 17:07 saobaolu| 編輯 收藏

    谷鴿鳥看,打造山寨信息網

    谷歌金山詞霸火星文版:http://g.iciba.com/mars/

    posted @ 2009-04-01 18:14 saobaolu| 編輯 收藏

    僅列出標題
    共6頁: 上一頁 1 2 3 4 5 6 下一頁 
    主站蜘蛛池模板: 日本免费网站视频www区| 国产人妖ts在线观看免费视频| 亚洲国产一区在线观看| 免费看片免费播放| 国产午夜无码片免费| 久久精品国产精品亚洲艾草网| 国产在线观看免费观看不卡| 人妻免费久久久久久久了| 亚洲国产成人久久综合一 | 在线免费观看伊人三级电影| 亚洲国产成人超福利久久精品| 国产v片免费播放| 日本免费在线中文字幕| 亚洲爆乳成av人在线视菜奈实| 亚洲va无码va在线va天堂| 成人毛片18女人毛片免费96| a毛片免费全部播放完整成| 中国china体内裑精亚洲日本| 亚洲精品无码久久久影院相关影片| 国产99视频精品免费观看7| 中国人免费观看高清在线观看二区| 亚洲成_人网站图片| 亚洲成AV人片在线观看无| 国产成人免费片在线视频观看| 久久ww精品w免费人成| 成人免费网站视频www| 在线观看亚洲AV每日更新无码| 国精无码欧精品亚洲一区| 国产91久久久久久久免费| 国产免费毛不卡片| 一级成人a毛片免费播放| 一级黄色片免费观看| 亚洲国产精品美女久久久久| 亚洲成av人片在线看片| 亚洲产国偷V产偷V自拍色戒| 免费人成视频x8x8入口| 免费看美女被靠到爽| 成年人性生活免费视频| 13一14周岁毛片免费| 国产好大好硬好爽免费不卡| 久久国产美女免费观看精品 |