当前位置:   article > 正文

【Android解决方案】连接SQL Server_android连接sql

android连接sql


案例说明

前一段时间要做一个用Android平板连接SQL的项目,在这种情况下,我就想到了两种方案:
1)Android Client  + Webservice + SQL的方式来连接
2)Android Client + SQL

本人一直都在用第一种方案做的,都很坚持第一种方案,但由于客户的环境加上其他因素,不能用第一种方案来做,所以就用了第二种;

技术概要

由于SQL官方的驱动不能直接用于Android上运行,所以就在网上搜了一些资料,看到了JTDS驱动,这个是开源的,网址是: http://sourceforge.net/projects/jtds/files/

现在最新的版本是1.3.0,但这个版本不能在Android上用,因为1.3.0是支持Java7的,Android不支持Java7,所以我就选择了1.2.7的版本; 

开发环境

Windows7 x64 专业版 
Android4.0.2
Eclipse4.2+ADT 21+SDK 21

代码实现

以下是数据库连接和测试类DataBaseUtil.java, 注意修改连接字符串
  1. package com.example.androidsql;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. public class DataBaseUtil
  8. {
  9. private static Connection getSQLConnection(String ip, String user, String pwd, String db)
  10. {
  11. Connection con = null;
  12. try
  13. {
  14. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  15. con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + ip + ":1433/" + db + ";charset=utf8", user, pwd);
  16. } catch (ClassNotFoundException e)
  17. {
  18. e.printStackTrace();
  19. } catch (SQLException e)
  20. {
  21. e.printStackTrace();
  22. }
  23. return con;
  24. }
  25. public static String testSQL()
  26. {
  27. String result = "字段1 - 字段2\n";
  28. try
  29. {
  30. Connection conn = getSQLConnection("xx.xx.xx.xx", "sa", "123", "DataBaseName");
  31. String sql = "select top 10 * from UserTable";
  32. Statement stmt = conn.createStatement();// 閸掓稑缂揝tatement
  33. ResultSet rs = stmt.executeQuery(sql);
  34. while (rs.next())
  35. {// <code>ResultSet</code>閺堬拷鍨甸幐鍥ф倻缁楊兛绔寸悰锟�
  36. String s1 = rs.getString("Name1");
  37. String s2 = rs.getString("Name2");
  38. result += s1 + " - " + s2 + "\n";
  39. System.out.println(s1 + " - " + s2);
  40. }
  41. rs.close();
  42. stmt.close();
  43. conn.close();
  44. } catch (SQLException e)
  45. {
  46. e.printStackTrace();
  47. result += "查询数据异常!" + e.getMessage();
  48. }
  49. return result;
  50. }
  51. public static void main(String[] args)
  52. {
  53. testSQL();
  54. }
  55. }




MainActivity.java
  1. package com.example.androidsql;
  2. import android.os.Bundle;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.app.Activity;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.widget.TextView;
  9. public class MainActivity extends Activity
  10. {
  11. private View btnTest;
  12. private View btnClean;
  13. private TextView tvTestResult;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState)
  16. {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. btnTest=findViewById(R.id.btnTestSql);
  20. btnClean=findViewById(R.id.btnClean);
  21. tvTestResult = (TextView)findViewById(R.id.tvTestResult);
  22. btnTest.setOnClickListener(getClickEvent());
  23. btnClean.setOnClickListener(getClickEvent());
  24. }
  25. @Override
  26. public boolean onCreateOptionsMenu(Menu menu)
  27. {
  28. // Inflate the menu; this adds items to the action bar if it is present.
  29. getMenuInflater().inflate(R.menu.main, menu);
  30. return true;
  31. }
  32. private View.OnClickListener getClickEvent(){
  33. return new View.OnClickListener()
  34. {
  35. @Override
  36. public void onClick(View v)
  37. {
  38. tvTestResult.setText("...");
  39. if(v==btnTest){
  40. test();
  41. }
  42. }
  43. };
  44. }
  45. private void test()
  46. {
  47. Runnable run = new Runnable()
  48. {
  49. @Override
  50. public void run()
  51. {
  52. String ret = DataBaseUtil.testSQL();
  53. Message msg = new Message();
  54. msg.what=1001;
  55. Bundle data = new Bundle();
  56. data.putString("result", ret);
  57. msg.setData(data);
  58. mHandler.sendMessage(msg);
  59. }
  60. };
  61. new Thread(run).start();
  62. }
  63. Handler mHandler = new Handler(){
  64. public void handleMessage(android.os.Message msg) {
  65. switch (msg.what)
  66. {
  67. case 1001:
  68. String str = msg.getData().getString("result");
  69. tvTestResult.setText(str);
  70. break;
  71. default:
  72. break;
  73. }
  74. };
  75. };
  76. }

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <Button
  11. android:id="@+id/btnTestSql"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/testSQL"/>
  15. <Button
  16. android:id="@+id/btnClean"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_alignParentRight="true"
  20. android:text="@string/clean"/>
  21. <TextView
  22. android:id="@+id/tvTestResult"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_below="@id/btnTestSql"
  26. android:text="@string/hello_world" />
  27. </RelativeLayout>



strings.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">AndroidSQL</string>
  4. <string name="action_settings">Settings</string>
  5. <string name="hello_world">Hello world!</string>
  6. <string name="testSQL">测试SQL</string>
  7. <string name="clean">清除数据</string>
  8. </resources>

Demo下载地址: 






声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/314222
推荐阅读
相关标签
  

闽ICP备14008679号