使用android访问SQLServer数据库。
首先说一下背景,由于公司的项目需要我需要开发一款android app,数据库是部署在远程服务器上的sqlserver2012数据库。对于我这个android开发新人来讲,这无疑是遇到的第一个拦路虎。Android studio环境下如何向Java中那样直接连接远程服务器上的数据库呢?我开始了网上寻找答案之旅,值得庆幸的是我找到了一篇博客解决了我的问题,博客名称为: 使用android访问SQLServer数据库。
http://blog.csdn.net/easyboot/article/details/54692297 转载地址
下面还是把博客的内容在抄一遍:
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
[java] view plain copy
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");
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
{
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())
{
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:
[java] view plain copy
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:
[html] view plain copy