//首先写一个jsp后台服务连接到Oracle数据库的实例myOra1(此处只是测试,所以用system身份连接到该实例)
//然后再通过jsp把数据返回给Android手机客户端
//运行jsp代码之前必须导入Oracle数据库的jdbc驱动包(jar包),名字为:classes12.jar.这个包在Oracle的安装目录下可已
//找到,在浏览器中打开jsp网页前必须保证Tomcat已经正确启动。
//jsp取到的数据以xml格式展现在web页面中
//Oracle数据库中的表如下:

//jsp代码:
下图为jsp后台取出数据的结果:

//当jsp后台从Oracle数据库取到数据后就应该返回给Android,这样就实现了Android客户端间接获得Oracle中的数据
Android客户端代码:
1. main.xml(布局文件):
- <?xml version="1.0" encoding="utf-8"?>
-
- <LinearLayout xmlns:android="http://schemas./apk/res/android"
-
- android:orientation="vertical"
-
- android:layout_width="fill_parent"
-
- android:layout_height="fill_parent"
-
- >
-
- <Button
-
- android:id="@+id/myButton"
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:text="获取Oracle数据 "
-
- />
-
- <TextView
-
- android:id="@+id/myText"
-
- android:layout_width="fill_parent"
-
- android:layout_height="fill_parent"
-
- />
-
- </LinearLayout>
2. main.java(Activity):
3 . ContentHandler.java:
- package com.AndroidLinkToJsp;
-
-
-
- import org.xml.sax.Attributes;
-
- import org.xml.sax.SAXException;
-
- import org.xml.sax.helpers.DefaultHandler;
-
-
-
- public class ContentHandler extends DefaultHandler{
-
- String ID,NAME,AGE,SEX;
-
- String tagName;
-
-
-
- /**
-
- * 开始解析xml
-
- * @throws SAXException
-
- */
-
- public void startDocument() throws SAXException{
-
- System.out.println("--------begin--------");
-
- }
-
-
-
- /**
-
- * 结束解析xml
-
- * @throws SAXException
-
- */
-
- public void endDocument() throws SAXException{
-
- System.out.println("--------end-----------");
-
- }
-
-
-
- /**
-
- * 开始解析元素属性
-
- *
-
- * @param namespaceURI 命名空间,防止命名重复
-
- * @param localName 不带前缀的名字
-
- * @param qName 带前缀的名字
-
- * @param attr 代表标签里所有的属性
-
- * @throws SAXException
-
- */
-
- public void startElement(String namespaceURI, String localName,
-
- String qName, Attributes attr) throws SAXException{
-
- //System.out.println("qName-------->"+qName);//调试用
-
- tagName = localName;//把当前正在解析的无前缀的名字传给tagName
-
- if(localName.equals("TONGXIN081")){
-
- for (int i = 0; i < attr.getLength(); i++) {
-
- System.out.println(attr.getLocalName(i) + "=" + attr.getValue(i));//得到第i个属性的名字和值
-
- }
-
- }
-
-
-
- }
-
-
-
- /**
-
- * 结束元素解析
-
- *
-
- * @param namespaceURI
-
- * @param localName
-
- * @param qName
-
- * @throws SAXException
-
- */
-
- public void endElement(String namespaceURI, String localName, String qName) throws SAXException{
-
- //在worker标签解析完之后,会打印出所有得到的数据
-
- //tagName = "";
-
- if(localName.equals("TONGXIN081"));
-
- this.printout();
-
- }
-
- /**
-
- * 具体解析标签里的内容
-
- *
-
- * @param ch 所有读取到的内容,都会放到char[]类型的数组里
-
- * @param start 读取的内容是从char[]数组的哪一位开始
-
- * @param length 从start开始,一共有多长的内容
-
- * @throws SAXException
-
- */
-
- public void characters(char[] ch, int start, int length)
-
- throws SAXException
-
- {
-
- if (tagName.equals("ID"))
-
- ID = new String(ch, start, length);
-
- else if (tagName.equals("NAME"))
-
- NAME = new String(ch, start, length);
-
- else if (tagName.equals("AGE"))
-
- AGE = new String(ch, start, length);
-
- else if (tagName.equals("SEX"))
-
- SEX = new String(ch, start, length);
-
- }
-
- private void printout()
-
- {
-
- System.out.print("ID: ");
-
- System.out.println(ID);
-
- System.out.print("NAME: ");
-
- System.out.println(NAME);
-
- System.out.print("AGE: ");
-
- System.out.println(AGE);
-
- System.out.print("SEX: ");
-
- System.out.println(SEX);
-
- System.out.println();
-
- }
-
- }
//最后在Manifest文件中还必须声明访问英特网的权限(如下图所示):

//下图为Android取到jsp后台xml格式的数据后返回给Android客户端

|