Android通过jsp连接Oracle数据库 -- 实例
//首先写一个jsp后台服务连接到Oracle数据库的实例myOra1(此处只是测试,所以用system身份连接到该实例)
//然后再通过jsp把数据返回给Android手机客户端
//运行jsp代码之前必须导入Oracle数据库的jdbc驱动包(jar包),名字为:classes12.jar.这个包在Oracle的安装目录下可已
//找到,在浏览器中打开jsp网页前必须保证Tomcat已经正确启动。
//jsp取到的数据以xml格式展现在web页面中
//Oracle数据库中的表如下:
//jsp代码:
- <?xml version="1.0" encoding="utf-8"?>
- <%@ page import="java.util.*"%>
- <%@ page import="java.sql.*"%>
- <%@page contentType="text/html;charset=gb2312"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <%
- try
- {
- Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
- String url="jdbc:oracle:thin:@10.88.5.117:1521:myOra1";
- String user="system";
- String password="manager";
- Connection conn= DriverManager.getConnection(url,user,password);
- Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- String sql="select * from system.SAZHAOXUN";
- ResultSet rs=stmt.executeQuery(sql);
- String strID;
- String strNAME;
- String strAGE;
- String strSEX;
- %>
- <INFO>
- <%
- while(rs.next())
- {
- strID=rs.getString("ID");
- strNAME=rs.getString("NAME");
- strAGE=rs.getString("AGE");
- strSEX=rs.getString("SEX");
- %>
- <TONGXIN081>
- <ID><%=strID%></ID>
- <NAME><%=strNAME%></NAME>
- <AGE><%=strAGE%></AGE>
- <SEX><%=strSEX%></SEX>
- </TONGXIN081>
- <% } %>
- </INFO>
- <%
- if(rs!=null)
- {
- rs.close();
- }
- if(stmt!=null)
- {
- stmt.close();
- }
- if(conn!=null)
- {
- conn.close();
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- %>
下图为jsp后台取出数据的结果:
//当jsp后台从Oracle数据库取到数据后就应该返回给Android,这样就实现了Android客户端间接获得Oracle中的数据
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20