Android连接远程数据库

转自:http://blog.csdn.net/sharpxu1985/article/details/6170362

Android虽然自带java.sql包,但是各数据库的JDBC Driver是否可用争议很多,不论国内网站还是国外网站,有人说能用,有人说不行,有人说虚拟机上能跑,上真手机就不行,有人说自己在手机上测试过也能跑。


但不管怎么说,直接连接远程数据库被公认不是一个很好的做法,至少在安全性上非常差的,所以现在最简单也是最流行的做法是访问远程服务器前段的PHP,PHP函数完成数据库操作,把结果经过JSON编码后传回,Android端再parse出结果。


SQL数据库创建测试表people 代码如下:

[xhtml]  view plain copy
  1. CREATE TABLE `people` (  
  2.     `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,  
  3.     `name` VARCHAR( 100 ) NOT NULL ,  
  4.     `sex` BOOL NOT NULL DEFAULT '1',  
  5.     `birthyear` INT NOT NULL  
  6. )  
 

 

PHP前端文件getAllPeopleBornAfter.php代码如下,查询出生年月在year之后的人:

[php]  view plain copy
  1. <?php  
  2.     mysql_connect("host","username","password");  
  3.     mysql_select_db("PeopleData");  
  4.     $q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");  
  5.     while($e=mysql_fetch_assoc($q))  
  6.         $output[]=$e;  
  7.         print(json_encode($output));  
  8.         mysql_close();  
  9. ?>  
 

 

Android端的方法略微复杂一点,代码如下:

[java]  view plain copy
  1. String result = "";  
  2. //首先使用NameValuePair封装将要查询的年数和关键字绑定  
  3. ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
  4. nameValuePairs.add(new BasicNameValuePair("year","1980"));  
  5.    
  6. //使用HttpPost封装整个SQL语句  
  7. //使用HttpClient发送HttpPost对象  
  8. try{  
  9.         HttpClient httpclient = new DefaultHttpClient();  
  10.         HttpPost httppost = new HttpPost("http://example.com/getAllPeopleBornAfter.php");  
  11.         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
  12.         HttpResponse response = httpclient.execute(httppost);   
  13.         HttpEntity entity = response.getEntity();  
  14.         InputStream is = entity.getContent();  
  15. }catch(Exception e){  
  16.         Log.e("log_tag""Error in http connection "+e.toString());  
  17. }  
  18. //将HttpEntity转化为String  
  19. try{  
  20.         BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);  
  21.         StringBuilder sb = new StringBuilder();  
  22.         String line = null;  
  23.         while ((line = reader.readLine()) != null) {  
  24.                 sb.append(line + "/n");  
  25.         }  
  26.         is.close();  
  27.    
  28.         result=sb.toString();  
  29. }catch(Exception e){  
  30.         Log.e("log_tag""Error converting result "+e.toString());  
  31. }  
  32.    
  33. //将String通过JSONArray解析成最终结果  
  34. try{  
  35.         JSONArray jArray = new JSONArray(result);  
  36.         for(int i=0;i<jArray.length();i++){  
  37.                 JSONObject json_data = jArray.getJSONObject(i);  
  38.                 Log.i("log_tag","id: "+json_data.getInt("id")+  
  39.                         ", name: "+json_data.getString("name")+  
  40.                         ", sex: "+json_data.getInt("sex")+  
  41.                         ", birthyear: "+json_data.getInt("birthyear")  
  42.                 );  
  43.         }  
  44. }  
  45. }catch(JSONException e){  
  46.         Log.e("log_tag""Error parsing data "+e.toString());  
  47. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值