http://blog.csdn.net/yaoneng119/article/details/8859326
案例说明
前一段时间要做一个用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,
注意修改连接字符串
- package com.example.androidsql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class DataBaseUtil
- {
- private static Connection getSQLConnection(String ip, String user, String pwd, String db)
- {
- Connection con = null;
- try
- {
- Class.forName("net.sourceforge.jtds.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + ip + ":1433/" + db + ";charset=utf8", user, pwd);
- } catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- return con;
- }
- public static String testSQL()
- {
- String result = "字段1 - 字段2\n";
- try
- {
- Connection conn = getSQLConnection("xx.xx.xx.xx", "sa", "123", "DataBaseName");
- String sql = "select top 10 * from UserTable";
- Statement stmt = conn.createStatement();// 閸掓稑缂揝tatement
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next())
- {// <code>ResultSet</code>閺堬拷鍨甸幐鍥ф倻缁楊兛绔寸悰锟�
- String s1 = rs.getString("Name1");
- String s2 = rs.getString("Name2");
- result += s1 + " - " + s2 + "\n";
- System.out.println(s1 + " - " + s2);
- }
- rs.close();
- stmt.close();
- conn.close();
- } catch (SQLException e)
- {
- e.printStackTrace();
- result += "查询数据异常!" + e.getMessage();
- }
- return result;
- }
- public static void main(String[] args)
- {
- testSQL();
- }
- }
- package com.example.androidsql;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.widget.TextView;
- public class MainActivity extends Activity
- {
- private View btnTest;
- private View btnClean;
- private TextView tvTestResult;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btnTest=findViewById(R.id.btnTestSql);
- btnClean=findViewById(R.id.btnClean);
- tvTestResult = (TextView)findViewById(R.id.tvTestResult);
- btnTest.setOnClickListener(getClickEvent());
- btnClean.setOnClickListener(getClickEvent());
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- private View.OnClickListener getClickEvent(){
- return new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- tvTestResult.setText("...");
- if(v==btnTest){
- test();
- }
- }
- };
- }
- private void test()
- {
- Runnable run = new Runnable()
- {
- @Override
- public void run()
- {
- String ret = DataBaseUtil.testSQL();
- Message msg = new Message();
- msg.what=1001;
- Bundle data = new Bundle();
- data.putString("result", ret);
- msg.setData(data);
- mHandler.sendMessage(msg);
- }
- };
- new Thread(run).start();
- }
- Handler mHandler = new Handler(){
- public void handleMessage(android.os.Message msg) {
- switch (msg.what)
- {
- case 1001:
- String str = msg.getData().getString("result");
- tvTestResult.setText(str);
- break;
- default:
- break;
- }
- };
- };
- }
activity_main.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <Button
- android:id="@+id/btnTestSql"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/testSQL"/>
- <Button
- android:id="@+id/btnClean"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:text="@string/clean"/>
- <TextView
- android:id="@+id/tvTestResult"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/btnTestSql"
- android:text="@string/hello_world" />
- </RelativeLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">AndroidSQL</string>
- <string name="action_settings">Settings</string>
- <string name="hello_world">Hello world!</string>
- <string name="testSQL">测试SQL</string>
- <string name="clean">清除数据</string>
- </resources>
Demo下载地址: