连接到外部sql server工具类

news/2024/7/8 10:17:01

首先第一步:我们需要下载一个jtds驱动让Android连接数据库

jtds下载地址http://sourceforge.net/projects/jtds/files/

第二步:数据库连接和测试类DataBaseUtil.java

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 s) {
String result = "字段1  -  字段2\n";
try {
Connection conn = getSQLConnection(IP地址, 用户名,
密码, "数据库库名");
if (conn == null) {
LogUtil.e("服务器正在忙,请稍后");
// toast.show(context, "服务器正在忙,请稍后连接");
} else {
LogUtil.e("连接成功");
String sql = s;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String s1 = rs.getString("CardId");
// String s2 = rs.getString("Id");
// result += s1 + "  -  " + s2 + "\n";
result = s1;
// System.out.println(s1 + "  -  " + s2);
System.out.println(s1);
}


rs.close();
stmt.close();
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
result += "查询数据异常!" + e.getMessage();
}
return result;
}


// 查询数据
public static String testSQL(String s, String column) {
String result = "字段1  -  字段2\n";
try {
Connection conn = getSQLConnection(IP地址, 用户名,
密码, "数据库库名");
if (conn == null) {
LogUtil.e("服务器正在忙,请稍后");
// toast.show(context, "服务器正在忙,请稍后连接");
} else {
LogUtil.e("连接成功");
String sql = s;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String s1 = rs.getString(column);
// String s2 = rs.getString("Id");
// result += s1 + "  -  " + s2 + "\n";
result = s1;
// System.out.println(s1 + "  -  " + s2);
System.out.println(s1);
}
rs.close();
stmt.close();
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
result = "查询数据异常!" + e.getMessage();
}
return result;
}


// 查询数据
public static ArrayList<String> testSQL1(String s, String column) {
String result = "字段1  -  字段2\n";
ArrayList<String> lists = new ArrayList<String>();
try {
Connection conn = getSQLConnection(IP地址, 用户名,
密码, "数据库库名");
if (conn == null) {
LogUtil.e("服务器正在忙,请稍后");
// toast.show(context, "服务器正在忙,请稍后连接");
} else {
LogUtil.e("连接成功");
String sql = s;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String s1 = rs.getString(column);
// String s2 = rs.getString("Id");
// result += s1 + "  -  " + s2 + "\n";
result = s1;
lists.add(result);
// System.out.println(s1 + "  -  " + s2);
System.out.println(s1);
}
rs.close();
stmt.close();
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
result = "查询数据异常!" + e.getMessage();
}
return lists;
}


public static void main(String[] args) {
testSQL(args.toString());
}

}

获取ip地址方法:window-cmd-ipconfig


MainActivity中主要代码

private void test() {
Runnable run = new Runnable() {


@Override
public void run() {


// 2.对卡号进行查询订单编号
String payorderno = "select ChargeOrderNo from ChargeRecord WHERE CardNo = 10000001";


ArrayList<String> order_No = DataBaseUtil.testSQL1(payorderno,
"ChargeOrderNo");
LogUtil.e(order_No+"");
// 3.根据卡号查询支付状态
String remark = "select MemberName from Member PhoneNum CardNo = 15868449932";


String name = DataBaseUtil.testSQL(remark,
"MemberName");


Message msg = new Message();
/**
* 代表全部
*/
msg.what = 1001;
Bundle data = new Bundle();
// data.putString("order_no", order_number);
data.putStringArrayList("order_no", order_No);
data.putString("order_remark", name);
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:
ArrayList<String> order_No2 = msg.getData().getStringArrayList(
"order_no");
String name = msg.getData().getString("order_remark");
Log.e("MainActivity", name);
default:
break;
}
};
};


项目下载地址:https://github.com/RangersEZ/connect-to-sqlserver-external-database.git


http://www.niftyadmin.cn/n/3649425.html

相关文章

什么是用户体验地图?该如何绘制?

什么是用户体验地图&#xff1f; 就像打仗需要地形图&#xff0c;体验提升的战斗也需要一个蓝图来规划和指引。 用户体验地图(Experience Maps)也被称为使用者旅程图(User Journey Map)。 用直白的话来解释下&#xff1a;用户体验地图就是通过一张图&#xff0c;用一种讲故事的…

如何在Ubuntu 18.04上使用复制迁移Redis数据

介绍 (Introduction) Redis is an in-memory, key-value data store known for its flexibility, performance, wide language support, and built-in features like replication. Replication is the practice of regularly copying data from one database to another in ord…

Android打开微信等其他应用

打开110String phoneNumber "120";Intent intentPhone new Intent(Intent.ACTION_CALL, Uri.parse("tel:" phoneNumber));startActivity(intentPhone);打开微信{Intent intent new Intent();ComponentName cmp new ComponentName("com.tencent.m…

解决eclipse中logcat不显示log的问题

调试程序需要打印一些消息出来&#xff0c;logcat不好用的话就很麻烦了。这个问题折腾了好久&#xff0c;为啥就是不出来呢&#xff1f; 上网找了很多解决办法&#xff1a; 重启eclipse 重启adb 重启logcat ......等等好多 都没能解决我的问题。英文水平有限一般小问题就问百度…

最好用的Windows 10终端——FluentTerminal

Windows 10自带的终端太丑 Windows 10的终端界面设计的如此丑&#xff0c;无论是cmd&#xff0c;powershell 还是wsl。默认的颜色和字体都非常难看。虽然进行一些颜色的设置可以改善视觉效果&#xff0c;但是十分麻烦&#xff0c;而且最奇怪的是设置不能同步到不同的快捷方式。…

[SIP]SIP协议场景生成器

http://www.iptel.org/~sipsc/IPTel提供了一款用Perl写的SIP协议场景生成器&#xff0c;能够以html方式生成SIP呼叫流或SIP场景数据包。下载解压缩后&#xff0c;可以浏览sip_scenario.v1.2.7/generated_files下的那些html文件&#xff0c;也可以运行sip_scenario.exe创建。ipt…

相机和CircleImageView的使用

首先第一步&#xff1a;导入包:compile de.hdodenhof:circleimageview:2.1.0各位可以去这个网址看这个控件的介绍https://github.com/hdodenhof/CircleImageView 布局&#xff1a; <de.hdodenhof.circleimageview.CircleImageViewandroid:id"id/profile_image"a…