李攀博客

          java夜未眠

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            3 Posts :: 0 Stories :: 3 Comments :: 0 Trackbacks

          2006年9月3日 #

          <DIV class=posttitle>java 中 string .split()使用方法 </DIV>

          在使用String.split方法分隔字符串時,分隔符如果用到一些特殊字符,可能會得不到我們預(yù)期的結(jié)果。

          我們看jdk doc中說明

          public String[] split(String regex)

          Splits this string around matches of the given regular expression.

          參數(shù)regex是一個 regular-expression的匹配模式而不是一個簡單的String,他對一些特殊的字符可能會出現(xiàn)你預(yù)想不到的結(jié)果,比如測試下面的代碼:

          用豎線 | 分隔字符串,你將得不到預(yù)期的結(jié)果

          ? String[] aa = "aaa|bbb|ccc".split("|");
          ? //String[] aa = "aaa|bbb|ccc".split("\\|"); 這樣才能得到正確的結(jié)果

          ? for (int i = 0 ; i <aa.length ; i++ ) {
          ? ? System.out.println("--"+aa);
          ? }

          用豎 * 分隔字符串運行將拋出java.util.regex.PatternSyntaxException異常,用加號 + 也是如此。

          ? String[] aa = "aaa*bbb*ccc".split("*");
          ? //String[] aa = "aaa|bbb|ccc".split("\\*"); 這樣才能得到正確的結(jié)果 ?

          ? for (int i = 0 ; i <aa.length ; i++ ) {
          ? ? System.out.println("--"+aa);
          ? }

          顯然,+ * 不是有效的模式匹配規(guī)則表達(dá)式,用"\\*" "\\+"轉(zhuǎn)義后即可得到正確的結(jié)果。

          "|" 分隔串時雖然能夠執(zhí)行,但是卻不是預(yù)期的目的,"\\|"轉(zhuǎn)義后即可得到正確的結(jié)果。

          還有如果想在串中使用"\"字符,則也需要轉(zhuǎn)義.首先要表達(dá)"aaaa\bbbb"這個串就應(yīng)該用"aaaa\\bbbb",如果要分隔就應(yīng)該這樣才能得到正確結(jié)果:

          String[] aa = "aaa\\bbb\\bccc".split(\\\\);

          JAVA中關(guān)于SPLIT方法的問題

          <DIV id=msgcns!94407A6582AF46B7!300><DIV>今天發(fā)現(xiàn)一個簡單的問題,就是用SPLIT方法獲取的數(shù)組元素個數(shù)與實際應(yīng)該得到的不相同,真是百思不其解。</DIV><DIV>? 經(jīng)過大量嘗試和猜想,終于得出問題出在方法本身。實例:</DIV><DIV>????? 由程序自動生成了一個中為:</DIV><DIV>????? String xx = "100000004768262367, 梁旭, 男, 2147, 0, 2147, 1745.00, 402.00,, 1000.00,,,,,";</DIV><DIV>????? 以上語句實際上應(yīng)該返回15個元素,但用SPLIT方法卻只返回了10元素(到1000.00為止)。</DIV><DIV>????? 照以上語句看就只能懷疑是方法本身的問題,但從何著手呢?</DIV><DIV>????? 呵呵,再一次證明了靈感是你無法去撲捉的,是無法勉強獲得的,它就是想來就來,想走就走的。</DIV><DIV>????? 逗號,就是它,不管了先試試。把后面的幾個全部去除,結(jié)果正確,再向前截取,結(jié)果還是正確,問題只會出現(xiàn)在后面的幾個逗號上。</DIV><DIV>???? 但這幾個簡單明了的逗號會出什么問題呢?難道是跟得太緊了呀?不會吧。這樣的話為什么1000.00前面的兩個能正確。</DIV><DIV>????? 看似找到問題所在,卻又好像是找了一個死胡同。</DIV><DIV>????? 經(jīng)過漫長的思考后還是一愁莫展。最后還是決定試試唯一想到的問題所在,把后面每個逗號都間隔一個空格試試,天啦,沒想到奇跡是這樣出現(xiàn)的,結(jié)果竟然正確了!!!。</DIV><DIV>????? 繼續(xù)試了幾種情況:</DIV><DIV>??????String xx = " , , , ,, ";
          ????? System.out.println(xx.split(",").length);</DIV><DIV>????? 以上語句輸出為6,與實際結(jié)果相符。</DIV><DIV><DIV>??????String xx = ",,,,, ";
          ????? System.out.println(xx.split(",").length);</DIV><DIV>????? 以上語句輸出為6,與實際結(jié)果相符。</DIV><DIV><DIV>??????String xx = " , , , ,,";
          ????? System.out.println(xx.split(",").length);</DIV><DIV>????? 以上語句輸出為4,與實際結(jié)果相符。</DIV><DIV><DIV>??????String xx = " , , , , ,";
          ????? System.out.println(xx.split(",").length);</DIV><DIV>????? 以上語句輸出為5,與實際結(jié)果相符。</DIV><DIV>經(jīng)上以上測試,發(fā)現(xiàn)了問題就在于最后一個逗號。</DIV><DIV>如果最后沒有間隔字符則不能正確的返回元素。</DIV></DIV></DIV></DIV></DIV>

          posted @ 2006-09-03 11:34 李攀 閱讀(2013) | 評論 (1)編輯 收藏

          一、jsp連接Oracle8/8i/9i數(shù)據(jù)庫(用thin模式)
          testoracle.jsp如下:
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.sql.*"%>
          <html>
          <body>
          <%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
          String url="jdbc:oracle:thin:@localhost:1521:orcl";
          //orcl為你的數(shù)據(jù)庫的SID
          String user="scott";
          String password="tiger";
          Connection conn= DriverManager.getConnection(url,user,password);
          Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          String sql="select * from test";
          ResultSet rs=stmt.executeQuery(sql);
          while(rs.next()) {%>
          您的第一個字段內(nèi)容為:<%=rs.getString(1)%>
          您的第二個字段內(nèi)容為:<%=rs.getString(2)%>
          <%}%>
          <%out.print("數(shù)據(jù)庫操作成功,恭喜你");%>
          <%rs.close();
          stmt.close();
          conn.close();
          %>
          </body>
          </html>


          二、jsp連接Sql Server7.0/2000數(shù)據(jù)庫
          testsqlserver.jsp如下:
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.sql.*"%>
          <html>
          <body>
          <%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
          String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
          //pubs為你的數(shù)據(jù)庫的
          String user="sa";
          String password="";
          Connection conn= DriverManager.getConnection(url,user,password);
          Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          String sql="select * from test";
          ResultSet rs=stmt.executeQuery(sql);
          while(rs.next()) {%>
          您的第一個字段內(nèi)容為:<%=rs.getString(1)%>
          您的第二個字段內(nèi)容為:<%=rs.getString(2)%>
          <%}%>
          <%out.print("數(shù)據(jù)庫操作成功,恭喜你");%>
          <%rs.close();
          stmt.close();
          conn.close();
          %>
          </body>
          </html>


          三、jsp連接DB2數(shù)據(jù)庫
          testdb2.jsp如下:
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.sql.*"%>
          <html>
          <body>
          <%Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
          String url="jdbc:db2://localhost:5000/sample";
          //sample為你的數(shù)據(jù)庫名
          String user="admin";
          String password="";
          Connection conn= DriverManager.getConnection(url,user,password);
          Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          String sql="select * from test";
          ResultSet rs=stmt.executeQuery(sql);
          while(rs.next()) {%>
          您的第一個字段內(nèi)容為:<%=rs.getString(1)%>
          您的第二個字段內(nèi)容為:<%=rs.getString(2)%>
          <%}%>
          <%out.print("數(shù)據(jù)庫操作成功,恭喜你");%>
          <%rs.close();
          stmt.close();
          conn.close();
          %>
          </body>
          </html>


          四、jsp連接Informix數(shù)據(jù)庫
          testinformix.jsp如下:
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.sql.*"%>
          <html>
          <body>
          <%Class.forName("com.informix.jdbc.IfxDriver").newInstance();
          String url =
          "jdbc:informix-sqli://123.45.67.89:1533/testDB:INformIXSERVER=myserver;
          user=testuser;password=testpassword";
          //testDB為你的數(shù)據(jù)庫名
          Connection conn= DriverManager.getConnection(url);
          Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          String sql="select * from test";
          ResultSet rs=stmt.executeQuery(sql);
          while(rs.next()) {%>
          您的第一個字段內(nèi)容為:<%=rs.getString(1)%>
          您的第二個字段內(nèi)容為:<%=rs.getString(2)%>
          <%}%>
          <%out.print("數(shù)據(jù)庫操作成功,恭喜你");%>
          <%rs.close();
          stmt.close();
          conn.close();
          %>
          </body>
          </html>


          五、jsp連接Sybase數(shù)據(jù)庫
          testmysql.jsp如下:
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.sql.*"%>
          <html>
          <body>
          <%Class.forName("com.sybase.jdbc.SybDriver").newInstance();
          String url =" jdbc:sybase:Tds:localhost:5007/tsdata";
          //tsdata為你的數(shù)據(jù)庫名
          Properties sysProps = System.getProperties();
          SysProps.put("user","userid");
          SysProps.put("password","user_password");
          Connection conn= DriverManager.getConnection(url, SysProps);
          Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          String sql="select * from test";
          ResultSet rs=stmt.executeQuery(sql);
          while(rs.next()) {%>
          您的第一個字段內(nèi)容為:<%=rs.getString(1)%>
          您的第二個字段內(nèi)容為:<%=rs.getString(2)%>
          <%}%>
          <%out.print("數(shù)據(jù)庫操作成功,恭喜你");%>
          <%rs.close();
          stmt.close();
          conn.close();
          %>
          </body>
          </html>


          六、jsp連接MySQL數(shù)據(jù)庫
          testmysql.jsp如下:
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.sql.*"%>
          <html>
          <body>
          <%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
          String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
          //testDB為你的數(shù)據(jù)庫名
          Connection conn= DriverManager.getConnection(url);
          Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          String sql="select * from test";
          ResultSet rs=stmt.executeQuery(sql);
          while(rs.next()) {%>
          您的第一個字段內(nèi)容為:<%=rs.getString(1)%>
          您的第二個字段內(nèi)容為:<%=rs.getString(2)%>
          <%}%>
          <%out.print("數(shù)據(jù)庫操作成功,恭喜你");%>
          <%rs.close();
          stmt.close();
          conn.close();
          %>
          </body>
          </html>


          七、jsp連接PostgreSQL數(shù)據(jù)庫
          testmysql.jsp如下:
          <%@ page contentType="text/html;charset=gb2312"%>
          <%@ page import="java.sql.*"%>
          <html>
          <body>
          <%Class.forName("org.postgresql.Driver").newInstance();
          String url ="jdbc:postgresql://localhost/soft"
          //soft為你的數(shù)據(jù)庫名
          String user="myuser";
          String password="mypassword";
          Connection conn= DriverManager.getConnection(url,user,password);
          Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          String sql="select * from test";
          ResultSet rs=stmt.executeQuery(sql);
          while(rs.next()) {%>
          您的第一個字段內(nèi)容為:<%=rs.getString(1)%>
          您的第二個字段內(nèi)容為:<%=rs.getString(2)%>
          <%}%>
          <%out.print("數(shù)據(jù)庫操作成功,恭喜你");%>
          <%rs.close();
          stmt.close();
          conn.close();
          %>
          </body>
          </html>

          posted @ 2006-09-03 11:30 李攀 閱讀(386) | 評論 (2)編輯 收藏

          所謂第一范式(1NF)是指數(shù)據(jù)庫表的每一列都是不可分割的基本數(shù)據(jù)項,同一列中不能有多個值,即實體中的某個屬性不能有多個值或者不能有重復(fù)的屬性。如果出現(xiàn)重復(fù)的屬性,就可能需要定義一個新的實體,新的實體由重復(fù)的屬性構(gòu)成,新實體與原實體之間為一對多關(guān)系。在第一范式(1NF)中表的每一行只包含一個實例的信息。例如,對于圖3-2 中的員工信息表,不能將員工信息都放在一列中顯示,也不能將其中的兩列或多列在一列中顯示;員工信息表的每一行只表示一個員工的信息,一個員工的信息在表中只出現(xiàn)一次。簡而言之,第一范式就是無重復(fù)的列。


          第二范式(2NF)要求數(shù)據(jù)庫表中的每個實例或行必須可以被惟一地區(qū)分。為實現(xiàn)區(qū)分通常需要為表加上一個列,以存儲各個實例的惟一標(biāo)識。如
          圖3-2 員工信息表中加上了員工編號(emp_id)列,因為每個員工的員工編號是惟一的,因此每個員工可以被惟一區(qū)分。這個惟一屬性列被稱為主關(guān)鍵字或主鍵、主碼。
          ??? 第二范式(2NF)要求實體的屬性完全依賴于主關(guān)鍵字。所謂完全依賴是指不能存在僅依賴主關(guān)鍵字一部分的屬性,如果存在,那么這個屬性和主關(guān)鍵字的這一部分應(yīng)該分離出來形成一個新的實體,新實體與原實體之間是一對多的關(guān)系。為實現(xiàn)區(qū)分通常需要為表加上一個列,以存儲各個實例的惟一標(biāo)識。簡而言之,第二范式就是非主屬性非部分依賴于主關(guān)鍵字。

          第三范式(3NF)要求一個數(shù)據(jù)庫表中不包含已在其它表中已包含的非主關(guān)鍵字信息。例如,存在一個部門信息表,其中每個部門有部門編號(dept_id)、部門名稱、部門簡介等信息。那么在圖3-2
          的員工信息表中列出部門編號后就不能再將部門名稱、部門簡介等與部門有關(guān)的信息再加入員工信息表中。如果不存在部門信息表,則根據(jù)第三范式(3NF)也應(yīng)該構(gòu)建它,否則就會有大量的數(shù)據(jù)冗余。簡而言之,第三范式就是屬性不依賴于其它非主屬性。

          posted @ 2006-09-03 11:28 李攀 閱讀(601) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 洮南市| 鲁甸县| 宁陕县| 莱西市| 北票市| 赣州市| 清苑县| 绍兴县| 开江县| 甘肃省| 凉山| 大庆市| 阿巴嘎旗| 闸北区| 高雄市| 平利县| 楚雄市| 乐至县| 济宁市| 桐乡市| 潼关县| 鲁山县| 梅河口市| 通州区| 洪江市| 庐江县| 文山县| 阿城市| 达州市| 东乡族自治县| 嘉义市| 长岭县| 新化县| 红河县| 葵青区| 张家口市| 红安县| 阳朔县| 水富县| 万州区| 丰都县|