日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Android通過jsp連接Oracle數(shù)據(jù)庫 -- 實例

 香山早秋 2012-02-28

//首先寫一個jsp后臺服務(wù)連接到Oracle數(shù)據(jù)庫的實例myOra1(此處只是測試,所以用system身份連接到該實例)

 

 

//然后再通過jsp把數(shù)據(jù)返回給Android手機客戶端

//運行jsp代碼之前必須導(dǎo)入Oracle數(shù)據(jù)庫的jdbc驅(qū)動包(jar包),名字為:classes12.jar.這個包在Oracle的安裝目錄下可已

//找到,在瀏覽器中打開jsp網(wǎng)頁前必須保證Tomcat已經(jīng)正確啟動。

 

//jsp取到的數(shù)據(jù)以xml格式展現(xiàn)在web頁面中

//Oracle數(shù)據(jù)庫中的表如下:

//jsp代碼:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <%@ page import="java.util.*"%>  
  4.   
  5. <%@ page import="java.sql.*"%>   
  6.   
  7. <%@page contentType="text/html;charset=gb2312"%>  
  8.   
  9.    
  10.   
  11. <%  
  12.   
  13. String path = request.getContextPath();  
  14.   
  15. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  16.   
  17. %>  
  18.   
  19.    
  20.   
  21. <%  
  22.   
  23.     try  
  24.   
  25.     {  
  26.   
  27.     Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();   
  28.   
  29.     String url="jdbc:oracle:thin:@10.88.5.117:1521:myOra1";  
  30.   
  31.     String user="system";   
  32.   
  33.     String password="manager";   
  34.   
  35.     Connection connDriverManager.getConnection(url,user,password);   
  36.   
  37.     Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   
  38.   
  39.     String sql="select * from system.SAZHAOXUN";   
  40.   
  41.     ResultSet rs=stmt.executeQuery(sql);  
  42.   
  43.       
  44.   
  45.     String strID;  
  46.   
  47.     String strNAME;  
  48.   
  49.     String strAGE;  
  50.   
  51.     String strSEX;    
  52.   
  53.    
  54.   
  55. %>  
  56.   
  57.     <INFO>  
  58.   
  59.             
  60.   
  61. <%  
  62.   
  63.     while(rs.next())   
  64.   
  65.     {  
  66.   
  67.     strID=rs.getString("ID");  
  68.   
  69.     strNAME=rs.getString("NAME");  
  70.   
  71.     strAGE=rs.getString("AGE");  
  72.   
  73.     strSEX=rs.getString("SEX");  
  74.   
  75.    
  76.   
  77. %>             
  78.   
  79.              <TONGXIN081>  
  80.   
  81.                 <ID><%=strID%></ID>  
  82.   
  83.                 <NAME><%=strNAME%></NAME>  
  84.   
  85.                 <AGE><%=strAGE%></AGE>  
  86.   
  87.                 <SEX><%=strSEX%></SEX>     
  88.   
  89.              </TONGXIN081>                  
  90.   
  91.            
  92.   
  93.     <% } %>  
  94.   
  95.       
  96.   
  97.              
  98.   
  99.      </INFO>   
  100.   
  101.         
  102.   
  103.     <%  
  104.   
  105.     if(rs!=null)  
  106.   
  107.     {  
  108.   
  109.          rs.close();  
  110.   
  111.     }  
  112.   
  113.     if(stmt!=null)  
  114.   
  115.     {  
  116.   
  117.          stmt.close();  
  118.   
  119.     }  
  120.   
  121.     if(conn!=null)  
  122.   
  123.     {  
  124.   
  125.          conn.close();  
  126.   
  127.     }  
  128.   
  129.     }  
  130.   
  131.     catch(Exception e)  
  132.   
  133.       {  
  134.   
  135.         e.printStackTrace();  
  136.   
  137.       }  
  138.   
  139.        
  140.   
  141.     %>     


 

下圖為jsp后臺取出數(shù)據(jù)的結(jié)果:

 

 

//當(dāng)jsp后臺從Oracle數(shù)據(jù)庫取到數(shù)據(jù)后就應(yīng)該返回給Android,這樣就實現(xiàn)了Android客戶端間接獲得Oracle中的數(shù)據(jù)

Android客戶端代碼:

1.      main.xml(布局文件):

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas./apk/res/android"  
  4.   
  5.     android:orientation="vertical"  
  6.   
  7.     android:layout_width="fill_parent"  
  8.   
  9.     android:layout_height="fill_parent"  
  10.   
  11.     >  
  12.   
  13. <Button  
  14.   
  15.     android:id="@+id/myButton"  
  16.   
  17.     android:layout_width="wrap_content"  
  18.   
  19.     android:layout_height="wrap_content"  
  20.   
  21.     android:text="獲取Oracle數(shù)據(jù) "  
  22.   
  23.     />  
  24.   
  25. <TextView    
  26.   
  27.     android:id="@+id/myText"  
  28.   
  29.     android:layout_width="fill_parent"   
  30.   
  31.     android:layout_height="fill_parent"  
  32.   
  33.     />  
  34.   
  35. </LinearLayout>  


 

2.   main.java(Activity):

  1. package com.AndroidLinkToJsp;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import java.io.StringReader;  
  6.   
  7. import javax.xml.parsers.SAXParserFactory;  
  8.   
  9. import org.apache.http.HttpResponse;  
  10.   
  11. import org.apache.http.client.ClientProtocolException;  
  12.   
  13. import org.apache.http.client.methods.HttpGet;  
  14.   
  15. import org.apache.http.impl.client.DefaultHttpClient;  
  16.   
  17. import org.apache.http.util.EntityUtils;  
  18.   
  19. import org.xml.sax.InputSource;  
  20.   
  21. import org.xml.sax.XMLReader;  
  22.   
  23. import com.sazhaoxun.AndroidLinkToJsp.R;  
  24.   
  25. import android.app.Activity;  
  26.   
  27. import android.os.Bundle;  
  28.   
  29. import android.view.View;  
  30.   
  31. import android.view.View.OnClickListener;  
  32.   
  33. import android.widget.Button;  
  34.   
  35. import android.widget.TextView;  
  36.   
  37.    
  38.   
  39. public class main extends Activity {  
  40.   
  41.     private TextView myText;  
  42.   
  43.     private Button myButton;  
  44.   
  45.     //命名空間  
  46.   
  47. //  private static final String urlStr="http://10.88.5.225:8080/test/MyJsp.jsp";  
  48.   
  49.     /** Called when the activity is first created. */  
  50.   
  51.     @Override  
  52.   
  53.     public void onCreate(Bundle savedInstanceState) {  
  54.   
  55.         super.onCreate(savedInstanceState);  
  56.   
  57.         setContentView(R.layout.main);  
  58.   
  59.         myText = (TextView)findViewById(R.id.myText);  
  60.   
  61.         myButton = (Button)findViewById(R.id.myButton);  
  62.   
  63.         myButton.setOnClickListener(new showButton());          
  64.   
  65.     }  
  66.   
  67.           
  68.   
  69.     class showButton implements OnClickListener{  
  70.   
  71.        @Override  
  72.   
  73.        public void onClick(View v)  
  74.   
  75.        {  
  76.   
  77.              
  78.   
  79.            // TODO Auto-generated method stub   
  80.   
  81.            System.out.println("開始獲得數(shù)據(jù)");  
  82.   
  83.            String uriAPI ="http://10.88.5.225:8080/test/MyJsp.jsp";  
  84.   
  85.            /*建立HTTP Get聯(lián)機*/  
  86.   
  87.            HttpGet httpRequest = new HttpGet(uriAPI);   
  88.   
  89.            try   
  90.   
  91.            {   
  92.   
  93.              /*發(fā)到HTTP request*/  
  94.   
  95.              HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);   
  96.   
  97.              /*若狀態(tài)碼為200 ok*/  
  98.   
  99.              if(httpResponse.getStatusLine().getStatusCode() == 200)    
  100.   
  101.              {   
  102.   
  103.                /*取叨并應(yīng)?串*/  
  104.   
  105.                String strResult = EntityUtils.toString(httpResponse.getEntity());  
  106.   
  107.                /*?除?余?元*/  
  108.   
  109.                //strResult = eregi_replace("(\r\n|\r|\n|\n\r)","",strResult);  
  110.   
  111.               try{  
  112.   
  113.                 SAXParserFactory factory=SAXParserFactory.newInstance();  
  114.   
  115.                 XMLReader reader=factory.newSAXParser().getXMLReader();  
  116.   
  117.                 reader.setContentHandler(new ContentHandler());  
  118.   
  119.                 reader.parse(new InputSource(new StringReader(strResult)));  
  120.   
  121.               }  
  122.   
  123.               catch (Exception e) {  
  124.   
  125.                e.printStackTrace();  
  126.   
  127.              }  
  128.   
  129.               System.out.println("成功獲得數(shù)據(jù)");  
  130.   
  131.               myText.setText(strResult.toString());  
  132.   
  133.                  
  134.   
  135.              }  
  136.   
  137.                
  138.   
  139.              else   
  140.   
  141.              {   
  142.   
  143.              myText.setText("Error Response: "+httpResponse.getStatusLine().toString());   
  144.   
  145.              }   
  146.   
  147.            }   
  148.   
  149.            catch (ClientProtocolException e)   
  150.   
  151.            {    
  152.   
  153.             // mTextView1.setText(e.getMessage().toString());   
  154.   
  155.              e.printStackTrace();   
  156.   
  157.            }   
  158.   
  159.            catch (IOException e)   
  160.   
  161.            {    
  162.   
  163.             // mTextView1.setText(e.getMessage().toString());   
  164.   
  165.              e.printStackTrace();   
  166.   
  167.            }   
  168.   
  169.          catch (Exception e)   
  170.   
  171.            {    
  172.   
  173.              //mTextView1.setText(e.getMessage().toString());   
  174.   
  175.              e.printStackTrace();    
  176.   
  177.            }   
  178.   
  179.               
  180.   
  181.             /**   
  182.   
  183.              * 創(chuàng)建一個SAXParserFactory,再用這個工廠來創(chuàng)建一個XMLReader,以此來讀取XML   
  184.   
  185.              */    
  186.   
  187. //            SAXParserFactory factory = SAXParserFactory.newInstance();    
  188.   
  189. //            XMLReader reader = factory.newSAXParser().getXMLReader();    
  190.   
  191. //  
  192.   
  193. //            //為XMLReader設(shè)置內(nèi)容處理器,MyContentHandler()為具體處理的類    
  194.   
  195. //            reader.setContentHandler(new ContentHandler());    
  196.   
  197. //            //開始解析文件    
  198.   
  199. //            reader.parse(new InputSource(new StringReader(strResult)));   
  200.   
  201. //              
  202.   
  203.        }  
  204.   
  205.       
  206.   
  207.     }  
  208.   
  209.    
  210.   
  211. }  


 

3 .    ContentHandler.java:

  1. package com.AndroidLinkToJsp;  
  2.   
  3.    
  4.   
  5. import org.xml.sax.Attributes;  
  6.   
  7. import org.xml.sax.SAXException;  
  8.   
  9. import org.xml.sax.helpers.DefaultHandler;  
  10.   
  11.    
  12.   
  13. public class ContentHandler extends DefaultHandler{  
  14.   
  15.     String ID,NAME,AGE,SEX;  
  16.   
  17.     String tagName;  
  18.   
  19.       
  20.   
  21.     /**   
  22.   
  23.      * 開始解析xml   
  24.   
  25.      * @throws SAXException   
  26.   
  27.      */   
  28.   
  29.     public void startDocument() throws SAXException{  
  30.   
  31.        System.out.println("--------begin--------");  
  32.   
  33.     }  
  34.   
  35.       
  36.   
  37.     /**   
  38.   
  39.      * 結(jié)束解析xml   
  40.   
  41.      * @throws SAXException   
  42.   
  43.      */   
  44.   
  45.     public void endDocument() throws SAXException{  
  46.   
  47.        System.out.println("--------end-----------");  
  48.   
  49.     }  
  50.   
  51.       
  52.   
  53.       /**   
  54.   
  55.      * 開始解析元素屬性   
  56.   
  57.      *    
  58.   
  59.      * @param namespaceURI 命名空間,防止命名重復(fù)   
  60.   
  61.      * @param localName         不帶前綴的名字   
  62.   
  63.      * @param qName        帶前綴的名字   
  64.   
  65.      * @param attr         代表標簽里所有的屬性   
  66.   
  67.      * @throws SAXException   
  68.   
  69.      */   
  70.   
  71.     public void startElement(String namespaceURI, String localName,    
  72.   
  73.             String qName, Attributes attr) throws SAXException{  
  74.   
  75.        //System.out.println("qName-------->"+qName);//調(diào)試用  
  76.   
  77.        tagName = localName;//把當(dāng)前正在解析的無前綴的名字傳給tagName  
  78.   
  79.        if(localName.equals("TONGXIN081")){  
  80.   
  81.            for (int i = 0; i < attr.getLength(); i++) {  
  82.   
  83.               System.out.println(attr.getLocalName(i) + "=" + attr.getValue(i));//得到第i個屬性的名字和值   
  84.   
  85.            }  
  86.   
  87.        }  
  88.   
  89.          
  90.   
  91.     }  
  92.   
  93.       
  94.   
  95.     /**   
  96.   
  97.      * 結(jié)束元素解析   
  98.   
  99.      *    
  100.   
  101.      * @param namespaceURI   
  102.   
  103.      * @param localName   
  104.   
  105.      * @param qName   
  106.   
  107.      * @throws SAXException   
  108.   
  109.      */    
  110.   
  111.     public void endElement(String namespaceURI, String localName, String qName) throws SAXException{  
  112.   
  113.        //在worker標簽解析完之后,會打印出所有得到的數(shù)據(jù)   
  114.   
  115.        //tagName = "";  
  116.   
  117.        if(localName.equals("TONGXIN081"));  
  118.   
  119.        this.printout();  
  120.   
  121.     }  
  122.   
  123.     /**   
  124.   
  125.      * 具體解析標簽里的內(nèi)容   
  126.   
  127.      *    
  128.   
  129.      * @param ch        所有讀取到的內(nèi)容,都會放到char[]類型的數(shù)組里   
  130.   
  131.      * @param start     讀取的內(nèi)容是從char[]數(shù)組的哪一位開始   
  132.   
  133.      * @param length    從start開始,一共有多長的內(nèi)容   
  134.   
  135.      * @throws SAXException   
  136.   
  137.      */  
  138.   
  139.     public void characters(char[] ch, int start, int length)    
  140.   
  141.     throws SAXException    
  142.   
  143. {    
  144.   
  145.        if (tagName.equals("ID"))    
  146.   
  147.            ID = new String(ch, start, length);    
  148.   
  149.        else if (tagName.equals("NAME"))    
  150.   
  151.            NAME = new String(ch, start, length);    
  152.   
  153.        else if (tagName.equals("AGE"))    
  154.   
  155.            AGE = new String(ch, start, length);    
  156.   
  157.        else if (tagName.equals("SEX"))    
  158.   
  159.            SEX = new String(ch, start, length);      
  160.   
  161.        }  
  162.   
  163.     private void printout()    
  164.   
  165.     {    
  166.   
  167.         System.out.print("ID: ");    
  168.   
  169.         System.out.println(ID);    
  170.   
  171.         System.out.print("NAME: ");    
  172.   
  173.         System.out.println(NAME);    
  174.   
  175.         System.out.print("AGE: ");    
  176.   
  177.         System.out.println(AGE);    
  178.   
  179.         System.out.print("SEX: ");    
  180.   
  181.         System.out.println(SEX);     
  182.   
  183.         System.out.println();    
  184.   
  185.     }   
  186.   
  187. }  


 

//最后在Manifest文件中還必須聲明訪問英特網(wǎng)的權(quán)限(如下圖所示):

//下圖為Android取到j(luò)sp后臺xml格式的數(shù)據(jù)后返回給Android客戶端

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多