Android通过jsp连接Oracle数据库_--_实例

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Android通过jsp连接Oracle数据库--实例//首先写一个jsp后台服务连接到Oracle数据库的实例myOra1(此处只是测试,所以用system身份连接到该实例)//然后再通过jsp把数据返回给Android手机客户端//运行jsp代码之前必须导入Oracle数据库的jdbc驱动包(jar包),名字为:classes12.jar.这个包在Oracle的安装目录下可已//找到,在浏览器中打开jsp网页前必须保证Tomcat已经正确启动。//jsp取到的数据以xml格式展现在web页面中//Oracle数据库中的表如下://jsp代码:[html]viewplaincopyprint?1.?xmlversion=1.0encoding=utf-8?2.3.%@pageimport=java.util.*%4.5.%@pageimport=java.sql.*%6.7.%@pagecontentType=text/html;charset=gb2312%8.9.10.11.%12.13.Stringpath=request.getContextPath();14.15.StringbasePath=request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;16.17.%18.19.20.21.%22.23.try24.25.{26.27.Class.forName(oracle.jdbc.driver.OracleDriver).newInstance();28.29.Stringurl=jdbc:oracle:thin:@10.88.5.117:1521:myOra1;30.31.Stringuser=system;32.33.Stringpassword=manager;34.35.Connectionconn=DriverManager.getConnection(url,user,password);36.37.Statementstmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);38.39.Stringsql=select*fromsystem.SAZHAOXUN;40.41.ResultSetrs=stmt.executeQuery(sql);42.43.44.45.StringstrID;46.47.StringstrNAME;48.49.StringstrAGE;50.51.StringstrSEX;52.53.54.55.%56.57.INFO58.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.TONGXIN08180.81.ID%=strID%/ID82.83.NAME%=strNAME%/NAME84.85.AGE%=strAGE%/AGE86.87.SEX%=strSEX%/SEX88.89./TONGXIN08190.91.92.93.%}%94.95.96.97.98.99./INFO100.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(Exceptione)132.133.{134.135.e.printStackTrace();136.137.}138.139.140.141.%下图为jsp后台取出数据的结果://当jsp后台从Oracle数据库取到数据后就应该返回给Android,这样就实现了Android客户端间接获得Oracle中的数据Android客户端代码:1.main.xml(布局文件):[html]viewplaincopyprint?1.?xmlversion=1.0encoding=utf-8?2.3.LinearLayoutxmlns:android=:orientation=vertical6.7.android:layout_width=fill_parent8.9.android:layout_height=fill_parent10.11.12.13.Button14.15.android:id=@+id/myButton16.17.android:layout_width=wrap_content18.19.android:layout_height=wrap_content20.21.android:text=获取Oracle数据22.23./24.25.TextView26.27.android:id=@+id/myText28.29.android:layout_width=fill_parent30.31.android:layout_height=fill_parent32.33./34.35./LinearLayout2.main.java(Activity):[html]viewplaincopyprint?1.packagecom.AndroidLinkToJsp;2.3.importjava.io.IOException;4.5.importjava.io.StringReader;6.7.importjavax.xml.parsers.SAXParserFactory;8.9.importorg.apache.http.HttpResponse;10.11.importorg.apache.http.client.ClientProtocolException;12.13.importorg.apache.http.client.methods.HttpGet;14.15.importorg.apache.http.impl.client.DefaultHttpClient;16.17.importorg.apache.http.util.EntityUtils;18.19.importorg.xml.sax.InputSource;20.21.importorg.xml.sax.XMLReader;22.23.importcom.sazhaoxun.AndroidLinkToJsp.R;24.25.importandroid.app.Activity;26.27.importandroid.os.Bundle;28.29.importandroid.view.View;30.31.importandroid.view.View.OnClickListener;32.33.importandroid.widget.Button;34.35.importandroid.widget.TextView;36.37.38.39.publicclassmainextendsActivity{40.41.privateTextViewmyText;42.43.privateButtonmyButton;44.45.//命名空间46.47.//privatestaticfinalStringurlStr=**Calledwhentheactivityisfirstcreated.*/50.51.@Override52.53.publicvoidonCreate(BundlesavedInstanceState){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(newshowButton());64.65.}66.67.68.69.classshowButtonimplementsOnClickListener{70.71.@Override72.73.publicvoidonClick(Viewv)74.75.{76.77.78.79.//TODOAuto-generatedmethodstub80.81.System.out.println(开始获得数据);82.83.StringuriAPI=*建立HTTPGet联机*/86.87.HttpGethttpRequest=newHttpGet(uriAPI);88.89.try90.91.{92.93./*发到HTTPrequest*/94.95.HttpResponsehttpResponse=newDefaultHttpClient().execute(httpRequest);96.97./*若状态码为200ok*/98.99.if(httpResponse.getStatusLine().getStatusCode()==200)100.101.{102.103./*取叨并应?串*/104.105.StringstrResult=EntityUtils.toString(httpResponse.getEntity());106.107./*?除?余?元*/108.109.//strResult=eregi_replace((\r\n|\r|\n|\n\r),,strResult);110.111.try{112.113.SAXParserFactoryfactory=SAXParserFactory.newInstance();114.115.XMLReaderreader=factory.newSAXParser().getXMLReader();116.117.reader.setContentHandler(newContentHandler());118.119.reader.parse(newInputSource(newStringReader(strResult)));120.121.}122.123.catch(Exceptione){124.125.e.printStackTrace();126.127.}128.129.System.out.println(成功获得数据);130.131.myText.setText(strResult.toString());132.133.134.135.}136.137.138.139.else140.141.{142.143.myText.setText(ErrorResponse:+httpResponse.getStatusLin

1 / 36
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功