1.SQL驱动
下载可以支持android的SQL驱动,下载地址http://sourceforge.net/projects/jtds/files/ 注意只能下载1.2.7版本。android不支持用高版本的。
将jtds-1.2.7.jar放在android项目libs目录下。
2.开发环境
windows 7 X64
android 4.2
android studio 2.2
3.代码
数据库连接类DBUtil.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 DBUtil { private static Connection getSQLConnection(String ip, String user, String pwd, String db) { Connection con = null; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); //有时查询出的字段含中文会出现乱码,需要把charset=utf8 改为 charset=gbk 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 QuerySQL() { String result = ""; try { //xx.xx.xx.xx 替换为自己的服务器IP地址 //sa 为数据库登录用户名 //123 为数据库登录密码 //DataBaseName 替换为自己的数据库名称 Connection conn = getSQLConnection("xx.xx.xx.xx", "sa", "123", "DataBaseName"); String sql = "select top 10 * from Users"; Statement stmt = conn.createStatement();// ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { //UserName Password 为数据表中字段名 String s1 = rs.getString("UserName"); String s2 = rs.getString("Password"); 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) { QuerySQL(); } }
MainActivity.java
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()); } 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 = DBUtil.QuerySQL(); 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>
string.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>
最后如果出现连接数据错误信息 空指针错误,则需要在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" />
参考原文http://blog.csdn.net/easyboot/article/details/54692297
微信公众号 weisico-com
转载请注明:微思考学习网-关注技术,分享知识 >> android访问SQLServer数据库