admin 管理员组文章数量: 1184232
2024年3月7日发(作者:菜鸟mysql模拟器)
Android通过jsp连接Oracle数据库 -- 实例
//首先写一个jsp后台服务连接到Oracle数据库的实例myOra1(此处只是测试,所以用system身份连接到该实例)
//然后再通过jsp把数据返回给Android手机客户端
//运行jsp代码之前必须导入Oracle数据库的jdbc驱动包(jar包),名字为:.这个包在Oracle的安装目录下可已
//找到,在浏览器中打开jsp网页前必须保证Tomcat已经正确启动。
//jsp取到的数据以xml格式展现在web页面中
//Oracle数据库中的表如下:
//jsp代码:
[html] view plaincopyprint?
1.
2.
3. <%@ page import=".*"%>
4.
5. <%@ page import=".*"%>
6.
7. <%@page contentType="text/html;charset=gb2312"%>
8.
9.
10.
11. <%
12.
13. String path = textPath();
14.
15. String basePath = eme()+"://"+verName()+":"+verPort()+path+"/";
16.
17. %>
18.
19.
20.
21. <%
22.
23. try
24.
25. {
26.
27. e("Driver").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 conn= nection(url,user,password);
36.
37. Statement stmt=Statement(_SCROLL_SENSITIVE,_UPDATABLE);
38.
39. String sql="select * from XUN";
40.
41. ResultSet rs=eQuery(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.
58.
59.
60.
61. <%
62.
63. while(())
64.
65. {
66.
67. strID=ing("ID");
68.
69. strNAME=ing("NAME");
70.
71. strAGE=ing("AGE");
72.
73. strSEX=ing("SEX");
74.
75.
76.
77. %>
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93. <% } %>
94.
95.
96.
97.
98.
99.
100.
101.
102.
103. <%
104.
105. if(rs!=null)
106.
107. {
108.
109. ();
110.
111. }
112.
113. if(stmt!=null)
114.
115. {
116.
117. ();
118.
119. }
120.
121. if(conn!=null)
122.
123. {
124.
125. ();
126.
127. }
128.
129. }
130.
131. catch(Exception e)
132.
133. {
134.
135. tackTrace();
136.
137. }
138.
139.
140.
141. %>
下图为jsp后台取出数据的结果:
//当jsp后台从Oracle数据库取到数据后就应该返回给Android,这样就实现了Android客户端间接获得Oracle中的数据
Android客户端代码:
1. (布局文件):
[html] view plaincopyprint?
1.
2.
3. 4. 5. android:orientation="vertical" 6. 7. android:layout_width="fill_parent" 8. 9. android:layout_height="fill_parent" 10. 11. > 12. 13.
2. (Activity):
[html] view plaincopyprint?
1. package dLinkToJsp;
2.
3. import ption;
4.
5. import Reader;
6.
7. import serFactory;
8.
9. import sponse;
10.
11. import ProtocolException;
12.
13. import t;
14.
15. import tHttpClient;
16.
17. import Utils;
18.
19. import ource;
20.
21. import der;
22.
23. import dLinkToJsp.R;
24.
25. import ty;
26.
27. import ;
28.
29. import ;
30.
31. import kListener;
32.
33. import ;
34.
35. import ew;
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="10.88.5.225:8080/test/";
48.
49. /** Called when the activity is first created. */
50.
51. @Override
52.
53. public void onCreate(Bundle savedInstanceState) {
54.
55. te(savedInstanceState);
56.
57. setContentView();
58.
59. myText = (TextView)findViewById();
60.
61. myButton = (Button)findViewById(on);
62.
63. lickListener(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. n("开始获得数据");
82.
83. String uriAPI ="10.88.5.225:8080/test/";
84.
85. /*建立HTTP Get联机*/
86.
87. HttpGet httpRequest = new HttpGet(uriAPI);
88.
89. try
90.
91. {
92.
93. /*发到HTTP request*/
94.
95. HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
96.
97. /*若状态码为200 ok*/
98.
99. if(tusLine().getStatusCode() == 200)
100.
101. {
102.
103. /*取叨并应?串*/
104.
105. String strResult = ng(ity());
106.
107. /*?除?余?元*/
108.
109. //strResult = eregi_replace("(rn|r|n|nr)","",strResult);
110.
111. try{
112.
113. SAXParserFactory factory=tance();
114.
115. XMLReader reader=Parser().getXMLReader();
116.
117. tentHandler(new ContentHandler());
118.
119. (new InputSource(new StringReader(strResult)));
120.
121. }
122.
123. catch (Exception e) {
124.
125. tackTrace();
126.
127. }
128.
129. n("成功获得数据");
130.
131. t(ng());
132.
133.
134.
135. }
136.
137.
138.
139. else
140.
141. {
142.
143. t("Error Response: "+tusLine().toString());
144.
145. }
146.
147. }
148.
149. catch (ClientProtocolException e)
150.
151. {
152.
153. // t(sage().toString());
154.
155. tackTrace();
156.
157. }
158.
159. catch (IOException e)
160.
161. {
162.
163. // t(sage().toString());
164.
165. tackTrace();
166.
167. }
168.
169. catch (Exception e)
170.
171. {
172.
173. //t(sage().toString());
174.
175. tackTrace();
176.
177. }
178.
179.
180.
181. /**
182.
183. * 创建一个SAXParserFactory,再用这个工厂来创建一个XMLReader,以此来读取XML
184.
185. */
186.
187. // SAXParserFactory factory = tance();
188.
189. // XMLReader reader = Parser().getXMLReader();
190.
191. //
192.
193. // //为XMLReader设置内容处理器,MyContentHandler()为具体处理的类
194.
195. // tentHandler(new ContentHandler());
196.
197. // //开始解析文件
198.
199. // (new InputSource(new StringReader(strResult)));
200.
201. //
202.
203. }
204.
205.
206.
207. }
208.
209.
210.
211. }
3 . :
[html] view plaincopyprint?
1. package dLinkToJsp;
2.
3.
4.
5. import utes;
6.
7. import eption;
8.
9. import tHandler;
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. n("--------begin--------");
32.
33. }
34.
35.
36.
37. /**
38.
39. * 结束解析xml
40.
41. * @throws SAXException
42.
43. */
44.
45. public void endDocument() throws SAXException{
46.
47. n("--------end-----------");
48.
49. }
50.
51.
52.
53. /**
54.
55. * 开始解析元素属性
56.
57. *
58.
59. * @param namespaceURI 命名空间,防止命名重复
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. //n("qName-------->"+qName);//调试用
76.
77. tagName = localName;//把当前正在解析的无前缀的名字传给tagName
78.
79. if(("TONGXIN081")){
80.
81. for (int i = 0; i < gth(); i++) {
82.
83. n(alName(i) + "=" + ue(i));//得到第i个属性的名字和值
84.
85. }
86.
87. }
88.
89.
90.
91. }
92.
93.
94.
95. /**
96.
97. * 结束元素解析
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标签解析完之后,会打印出所有得到的数据
114.
115. //tagName = "";
116.
117. if(("TONGXIN081"));
118.
119. ut();
120.
121. }
122.
123. /**
124.
125. * 具体解析标签里的内容
126.
127. *
128.
129. * @param ch 所有读取到的内容,都会放到char[]类型的数组里
130.
131. * @param start 读取的内容是从char[]数组的哪一位开始
132.
133. * @param length 从start开始,一共有多长的内容
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 (("ID"))
146.
147. ID = new String(ch, start, length);
148.
149. else if (("NAME"))
150.
151. NAME = new String(ch, start, length);
152.
153. else if (("AGE"))
154.
155. AGE = new String(ch, start, length);
156.
157. else if (("SEX"))
158.
159. SEX = new String(ch, start, length);
160.
161. }
162.
163. private void printout()
164.
165. {
166.
167. ("ID: ");
168.
169. n(ID);
170.
171. ("NAME: ");
172.
173. n(NAME);
174.
175. ("AGE: ");
176.
177. n(AGE);
178.
179. ("SEX: ");
180.
181. n(SEX);
182.
183. n();
184.
185. }
186.
187. }
//最后在Manifest文件中还必须声明访问英特网的权限(如下图所示):
//下图为Android取到jsp后台xml格式的数据后返回给Android客户端
ok,我们在Android手机上可以看到远程数据库的数据。这个Demo就是Android通过jsp连接远程Oracle数据库。
版权声明:本文标题:Android通过jsp连接Oracle数据库 -- 实例 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1709762241a545612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论