一、测试过的开发环境
Eclispse + SqlServer2008R2
二、下载支持Android的SQL驱动
只能下载1.2.7版本的,将 jtds-1.2.7.jar 包放在android项目 libs 目录下。 下载地址:http://sourceforge.net/projects/jtds/files/
三、实现步骤
1、创建数据库连接类 DBUtil.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { private static String IP = "192.168.137.135"; private static String DBName = "Test"; private static String USER = "sa"; private static String PWD = "F123456789f"; /** 创建数据库对象 */ private static Connection getSQLConnection() { Connection con = null; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); //加上 useunicode=true;characterEncoding=UTF-8 防止中文乱码 con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + IP + ":1433/" + DBName + ";useunicode=true;characterEncoding=UTF-8", USER, PWD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return con; } /** 查询数据库 */ public static String Query() { String result = ""; try { Connection conn = getSQLConnection(); String sql = "select * from SysUser"; Statement stmt = conn.createStatement();// ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { String s1 = rs.getString("UserCode"); String s2 = rs.getString("RealName"); result += s1 + " - " + s2 + "\n"; } rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); result += "查询数据异常!" + e.getMessage(); } return result; } }
2、创建一个布局文件,activity_main.xml
一个Buttton控件,用来点击查询。
一个TextView控件,用来显示查询到的信息。
代码省略。。。
3、具体代码调用 MainActivity.java
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button button; TextView textview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); textview = (TextView) findViewById(R.id.textview); // 按钮点击事件 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { textview.setText(""); test(); } }); } // 查询 private void test() { Runnable run = new Runnable() { @Override public void run() { String ret = DBUtil.Query(); 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"); textview.setText(str); break; default: break; } }; }; }
4、在AndroidManifest.xml 加上权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5、问题
- 如何捕获连接数据库失败?
- 如何连接其它的数据库?
- 能使用的Android版本有哪些?