如何使用 APICloud 的端 API 连接到自己的服务器数据库获取数据

如何使用 APICloud 的端 API 连接到自己的服务器数据库获取数据,第1张

方法/步骤
1
首先
1如果想快速开发,没有自己的服务器,还是推荐使用云API的数据云。 2不使用云API的数据云,可能是因为有web版且已经有商用数据。 3不使用云API的数据云,需自己写数据接口,即客户端发送请求接收处理与生成json数据给客户端进行交互。 4感谢APICloud提供新的APP开发方式。 其实也不难,使用apiajax进行请求即可。PHPer,以下使用演示使用PHP,数据库使用Mysql,JAVA党或者NET党请自行coding
2
连接到数据库(文件放在服务器上),服务器端输出JSON
<php
/
配置连接数据库信息
/
$host='localhost';//主机
$user='root';//数据库账号
$password='';//数据库密码
$database='test';//数据库名
//打开数据库连接
$db=mysqli_connect($host,$user,$password,$database);
//判断连接是否成功
if($db){
$db->query("set names utf8");//设置UTF-8编码(JSON的唯一编码)
}else{
echo 'DATABASE_CONNECTION_DIE';//数据库连接失败
exit;
}
//sql查询语句
$sql="select id,name,sex,age from person";
$result=$db->query($sql);
while($row=$result->fetch_assoc()){
$person_info[]=$row;//将取得的所有数据赋值给person_info数组
}
echo json_encode($person_info);//输出JSON
>
复制代码
输出的JSON示例:
[{"id":"1","name":"\u54c8\u5c3c","sex":"\u7537","age":"22"},{"id":"2","name":"\u5c0f\u9648","sex":"\u5973","age":"21"},{"id":"3","name":"\u5c0f\u767d","sex":"\u672a\u77e5","age":"1"}]
复制代码
3
测试用的数据库SQL语句
--
-- Database: `test`
--

-- --------------------------------------------------------

--
-- 表的结构 `person`
--

CREATE TABLE IF NOT EXISTS `person` (
`id` int(11) NOT NULL,
`name` varchar(5) NOT NULL,
`sex` varchar(2) NOT NULL,
`age` int(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- 转存表中的数据 `person`
--

INSERT INTO `person` (`id`, `name`, `sex`, `age`) VALUES
(1, '哈尼', '男', 22),
(2, '小陈', '女', 21),
(3, '小白', '未知', 1);

复制代码

搞定服务器端输出JSON后,重点来了
4
端API使用apiajax读取接口数据
点我获取数据
复制代码
5
OK,如果要POST方式,请自行参考官方文档。
思路就是这样,其它的依此类推:
客户端发送请求,服务器端接收处理后返回数据。
用户登录和状态检查推荐使用token检验,不要直接post密码。

使用 SQLDMO(SQL Distributed Management Objects,SQL分布式管 理对象)。

添加引用->COM里面

//得到所有本地网络中可使用的SQL服务器列表。

SQLDMOApplication sqlApp = new SQLDMOApplicationClass();

SQLDMONameList sqlServers = sqlAppListAvailableSQLServers();

for(int i=0;i<sqlServersCount;i++)

{

object srv = sqlServersItem(i + 1);

if(srv != null)

{

thiscboServersItemsAdd(srv);

}

}

if(thiscboServersItemsCount > 0)

thiscboServersSelectedIndex = 0;

else

thiscboServersText = "<No available SQL Servers>";

//得到指定SQL服务器所有数据库的列表

SQLDMOApplication sqlApp = new SQLDMOApplicationClass();

SQLDMOSQLServer srv = new SQLDMOSQLServerClass();

srvConnect(thiscboServersSelectedItemToString(),thistxtUserText,thistxtPasswordText);

foreach(SQLDMODatabase db in srvDatabases)

{

if(dbName!=null)

thiscboDatabaseItemsAdd(dbName);

}

手机是不能直接去连接你服务器的mysql数据库

请在你的服务端写代码去连接mysql数据吧

Mysql连接方法

1 加载数据库驱动: ClassforName("orggjtmmmysqlDriver"); //加载数据库驱动

String url = "jdbc:mysql://localhost:3306/test";

String user = "root";

String passowrd = "123456";

2 获取数据库连接Connection con数= DriverManagergetConnection(url,user,password)

3 获取SQL执行器 PreparedStatement prepare = conprepareStatement("SQL语句")

4 执行SQL语句,得到结果集 ResultSet result = prepareexecuteQuery();

while(resultnext()){

  //读取结果

}

最后不要忘记导入jdbc驱动包

纯工手打字,请采纳哈

改不了吧,一般是跟电脑绑定的 ,可以有以下几种都表示同一个:
1、你登录数据库管理工具时候那个数据库引擎里面的就是
2、直接输入127001或者数据库IP地址也是
3、有些支持一个 点 或者 点加 \ ( 或者 \)
4、带实例名的如127001\实例名,这是安装时候不按默认实例名安装的

在net中提供了一些类来显示和控制Windows系统上的服务,并可以实现对远程计算机服务服务的访问,如SystemServiceProcess命名空间下面的ServiceController 类,SystemManagement下面的一些WMI *** 作的类。虽然用ServiceController可以很方便的实现对服务的控制,而且很直观、简洁和容易理解。但是我认为他的功能同通过WMI来 *** 作服务相比,那可能就有些单一了,并且对多个服务的 *** 作可能就比较麻烦,也无法列出系统中的所有服务的具体数据。这里要讲的就是如何使用SystemManagement组件来 *** 作远程和本地计算机上的服务。
WMI作为Windows 2000 *** 作系统的一部分提供了可伸缩的,可扩展的管理架构公共信息模型(CIM)是由分布式管理任务标准协会(DMTF)设计的一种可扩展的、面向对象的架构,用于管理系统、网络、应用程序、数据库和设备。Windows管理规范也称作CIM for Windows,提供了统一的访问管理信息的方式。如果需要获取详细的WMI信息请读者查阅MSDN。SystemManagement组件提供对大量管理信息和管理事件集合的访问,这些信息和事件是与根据 Windows 管理规范 (WMI) 结构对系统、设备和应用程序设置检测点有关的。
但是上面并不是我们最关心的,下面才是我们需要谈的话题。毫无疑问,我们要引用SystemManagementDll程序集,并要使用SystemManagement命名空间下的类,如ManagementClass,ManagementObject等。下面用一个名为Win32ServiceManager的类把服务的一些相关 *** 作包装了一下,代码如下:
using System;
using SystemManagement;
namespace ZZWmi
{
public class Win32ServiceManager
{ private string strPath;private ManagementClass managementClass;public Win32ServiceManager():this("",null,null){
}
public Win32ServiceManager(string host,string userName,string password)
{thisstrPath = "\\\\"+host+"\\root\\cimv2:Win32_Service";
thismanagementClass = new ManagementClass(strPath);if(userName!=null&&userNameLength>0)
{
ConnectionOptions connectionOptions = new ConnectionOptions();connectionOptionsUsername = userName;
connectionOptionsPassword = password;
ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;
thismanagementClassScope = managementScope;
}
}
// 验证是否能连接到远程计算机
public static bool RemoteConnectValidate(string host,string userName,string password){
ConnectionOptions connectionOptions = new ConnectionOptions();connectionOptionsUsername = userName;
connectionOptionsPassword = password;
ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;
try
{
managementScopeConnect();}
catch
{
}
return managementScopeIsConnected;}
// 获取指定服务属性的值
public object GetServiceValue(string serviceName,string propertyName){
ManagementObject mo = thismanagementClassCreateInstance();moPath = new ManagementPath(thisstrPath+"Name=\""+serviceName+"\"");return mo[propertyName];}
// 获取所连接的计算机的所有服务数据
public string [,] GetServiceList()
{
string [,] services = new string [thismanagementClassGetInstances()Count,4];int i = 0;
foreach(ManagementObject mo in thismanagementClassGetInstances())
{
services[i,0] = (string)mo["Name"];services[i,1] = (string)mo["DisplayName"];
services[i,2] = (string)mo["State"];
services[i,3] = (string)mo["StartMode"];
i++;
}
return services;}
// 获取所连接的计算机的指定服务数据
public string [,] GetServiceList(string serverName)
{
return GetServiceList(new string []{serverName});}
// 获取所连接的计算机的的指定服务数据
public string [,] GetServiceList(string [] serverNames)
{
string [,] services = new string [serverNamesLength,4];
ManagementObject mo = thismanagementClassCreateInstance();
for(int i = 0;i<serverNamesLength;i++)
{
moPath = new ManagementPath(thisstrPath+"Name=\""+serverNames[i]+"\"");
services[i,0] = (string)mo["Name"];
services[i,1] = (string)mo["DisplayName"];
services[i,2] = (string)mo["State"];
services[i,3] = (string)mo["StartMode"];
} return services;}
// 停止指定的服务
public string StartService(string serviceName)
{ string strRst = null;
ManagementObject mo = thismanagementClassCreateInstance();moPath = new ManagementPath(thisstrPath+"Name=\""+serviceName+"\"");
try
{if((string)mo["State"]=="Stopped")//!(bool)mo["AcceptStop"]moInvokeMethod("StartService",null);
}
catch(ManagementException e){ strRst =eMessage; }
return strRst;
}
// 暂停指定的服务
public string PauseService(string serviceName)
{
string strRst = null;
ManagementObject mo = thismanagementClassCreateInstance();moPath = new ManagementPath(thisstrPath+"Name=\""+serviceName+"\"");
try{
//判断是否可以暂停
if((bool)mo["acceptPause"]&&(string)mo["State"]=="Running")moInvokeMethod("PauseService",null);}
catch(ManagementException e){
strRst =eMessage; }
return strRst;}
// 恢复指定的服务
public string ResumeService(string serviceName)
{
string strRst = null;
ManagementObject mo = thismanagementClassCreateInstance();
moPath = new ManagementPath(thisstrPath+"Name=\""+serviceName+"\"");
try
{
//判断是否可以恢复
if((bool)mo["acceptPause"]&&(string)mo["State"]=="Paused")moInvokeMethod("ResumeService",null);}
catch(ManagementException e){strRst =eMessage;
}
return strRst;}
// 停止指定的服务
public string StopService(string serviceName)
{ string strRst = null;
ManagementObject mo = thismanagementClassCreateInstance();
moPath = new ManagementPath(thisstrPath+"Name=\""+serviceName+"\"");
try
{//判断是否可以停止
if((bool)mo["AcceptStop"])//(string)mo["State"]=="Running"
moInvokeMethod("StopService",null);
}
catch(ManagementException e)
{
strRst =eMessage; }
return strRst;
}
}
}
在Win32ServiceManager中通过RemoteConnectValidate静态方法来测试连接成功与否;另外提供了GetServiceValue方法和GetServiceList方法以及它的重载来获取服务信息;后面的四个方法就是对服务的状态控制了。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zz/12764626.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-27
下一篇2025-08-27

发表评论

登录后才能评论

评论列表(0条)

    保存