简体   繁体   English

将 android 与 MS SQL SERVER 2008 连接起来

[英]Connecting android with MS SQL SERVER 2008

Is there a way that we can connect an Android application to a central database server (eg MSSQLServer 2008)?有没有办法将 Android 应用程序连接到中央数据库服务器(例如 MSSQLServer 2008)?

I have a MySQL database that is accessed by both website and Android.我有一个可由网站和 Android 访问的 MySQL 数据库。 Connecting to the database from the website is fine, but how can it be done from the Android app?从网站连接到数据库没问题,但如何从 Android 应用程序完成呢?

here are some similar questions asked (an answered):以下是一些类似的问题(已回答):

Even though those are for MySQL, it should work for MSSQL by changing the engine or the driver's use to connect.即使这些是针对 MySQL 的,它也应该通过更改引擎或驱动程序用于连接来适用于 MSSQL。 Usually, the approach is to expose some limited level of modification through a web service.通常,该方法是通过 Web 服务公开一些有限级别的修改。 Still, nothing is stopping you from directly accessing the database, albeit depending on the case, could pose a security risk.尽管如此,没有什么能阻止您直接访问数据库,尽管视情况而定,可能会带来安全风险。

Main Reasons the web service approach is taking: Web 服务方法采用的主要原因:

  • Performance表现
  • Security安全
  • Best Practice最佳实践
  • Separation of concerns关注点分离

An exception is if you want to enable direct access because you're building a sort of database client through mobile.一个例外是如果您想启用直接访问,因为您正在通过移动构建一种数据库客户端。

All you have to do is use the appropriate driver, i'd recommend using JTDS , and version 1.2.5 seems to have worked well with android.Detailed instruction on how to use with eclipse can be found here A working code is available in github您所要做的就是使用适当的驱动程序,我建议使用JTDS ,并且1.2.5版似乎与 android 配合良好。有关如何使用 eclipse 的详细说明可以在此处找到github 中提供了工作代码

/**
 * This is a demo code to demonstrate db connection and operations and not 
 * meant for a live run. 
 * 
 */

public class DBTestActivity extends Activity {

    private Connection conn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dbtest);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.dbtest, menu);
        return true;

    }
    @Override
    protected void onResume() {

        super.onResume();
        (new DBConnection()).execute(null, null, null);

    }

    @Override 
    protected void onPause() {

        super.onPause();
        try {

            conn.close();

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }
    class DBConnection extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... arg0) {

            try {

                Log.e("MSSQL", "Attempting to connect");

                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                conn = DriverManager.getConnection(
                        "jdbc:jtds:sqlserver://yourserver.com/DBName",
                        "username", "password");

                Log.e("MSSQL", "Connected");

            } catch (Exception e) {

                e.printStackTrace();
                Log.e("MSSQL", e.toString());

            }

            return null;
        }

    }

    class UserInfo {

        String userID;
        String userName;
        String PhoneNo;
        String age;

        public UserInfo(String userID, String userName, String PhoneNo,
                String age) {

            this.userID = userID;
            this.userName = userName;
            this.PhoneNo = PhoneNo;
            this.age = age;

        }

        public String getUserID() {
            return userID;
        }

        public void setUserID(String userID) {
            this.userID = userID;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getPhoneNo() {
            return PhoneNo;
        }

        public void setPhoneNo(String phoneNo) {
            PhoneNo = phoneNo;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

    }

    class DBOperation {

        public List<UserInfo> getAllUsers() throws SQLException {

            Statement statement = getStatement(conn);

            List<UserInfo> userlist = new ArrayList<UserInfo>();

            ResultSet rs = statement.executeQuery("SELECT * FROM UserInfoTable");
            rs.next();
            int count = 0;

            while (rs.next()) {

                userlist.add(new UserInfo(rs.getString(1), rs.getString(2),
                        rs.getString(3), rs.getString(4)));
                count++;

            }

            rs.close();
            statement.close();
            return userlist;

        }

        public void addUser(UserInfo info) {

            Log.e("MSSQL", "in adduser");

            Statement statement = getStatement(conn);

            try {

                ResultSet rs = statement.executeQuery("INSERT INTO UserInfoTable "
                        + " VALUES ('1001', 'Bob', '333333', '33')");
                rs.close();
                statement.close();

            } catch (SQLException e) {

                e.printStackTrace();

            } 

        }

        private Statement getStatement(Connection connection) {

            try {

                return connection.createStatement();

            } catch (Exception e) {

                throw new RuntimeException(e);

            }
        }

    }

}

An other approach, musch simpler than Web Service, is to use a Virtual JDBC Driver that uses a three-tier architecture: your JDBC code is sent through HTTP to a remote Servlet that filters the JDBC code (configuration & security) before passing it to the SQL Server JDBC Driver.另一种比 Web 服务更简单的方法是使用使用三层架构的虚拟 JDBC 驱动程序:您的 JDBC 代码通过 HTTP 发送到远程 Servlet,该 Servlet 过滤 JDBC 代码(配置和安全),然后再将其传递给SQL Server JDBC 驱动程序。 The result is sent you back through HTTP.结果通过 HTTP 发回给您。 There are some free software that use this technique.有一些免费软件使用了这种技术。 Just Google "Android JDBC Driver over HTTP".只是谷歌“Android JDBC Driver over HTTP”。

将您的 android 应用程序直接连接到外部数据库服务器是一个坏主意,而是创建一个 Web 应用程序并通过它访问数据库。

There are a number of strategies you can employ to accomplish what you want to do.您可以采用多种策略来完成您想做的事情。 Given that the SOAP support for Android is non-existent, you're going to most likely want to push the data out in either XML or JSON format through WCF, ASP.NET, Ruby On Rails, PHP or any number of web frameworks.鉴于对 Android 的 SOAP 支持不存在,您很可能希望通过 WCF、ASP.NET、Ruby On Rails、PHP 或任意数量的 Web 框架以 XML 或 JSON 格式推出数据。

Without knowing what your web application is currently running, it's hard to say how to best make that data connection.在不知道您的 Web 应用程序当前正在运行什么的情况下,很难说如何最好地建立该数据连接。 You can use WCF Data Services if you want to get up and running as fast as possible, and MSDN has a decent article on getting started with it:如果您想尽快启动和运行,可以使用 WCF 数据服务,MSDN 上有一篇不错的入门文章:

http://msdn.microsoft.com/en-us/library/cc668792.aspx http://msdn.microsoft.com/en-us/library/cc668792.aspx

I suggest that you examine your existing solution and figure out how to best extend that to push data out to your Android app.我建议您检查现有的解决方案并找出如何最好地扩展它以将数据推送到您的 Android 应用程序。

i've try to connect android via PHPto ms sql server, you can read here ,am using httprequest and json.我尝试通过 PHP 连接 android 到 ms sql 服务器,你可以在这里阅读,我使用 httprequest 和 json。 If you want to connect to Ms SQL Server 2005 or higher, you must download Microsoft Driver for PHP for SQL Server.如果要连接到 Ms SQL Server 2005 或更高版本,则必须下载 Microsoft Driver for PHP for SQL Server。

I've used php as web service to connect Ms SQL Server database, anyway you can used jdbc to connect from android direct to MS SQL Server database我已经使用 php 作为 web 服务来连接 Ms SQL Server 数据库,无论如何你可以使用jdbc从 android 直接连接到 MS SQL Server 数据库

If you need to do that probably you donìt have already a 3 tier architecture.如果您需要这样做,您可能还没有 3 层架构。 If this is the case consider writing a mobile web application.如果是这种情况,请考虑编写移动 Web 应用程序。 I did that to add mobile interface to a client server 2 tier legacy system.我这样做是为了将移动界面添加到客户端服务器 2 层遗留系统。

I had a legacy Delphi client/server app, and i created by using Raudus a web app optimized for mobile devices.我有一个旧的 Delphi 客户端/服务器应用程序,我使用Raudus创建了一个针对移动设备优化的网络应用程序。 Of course this makes sense if you have Delphi skills, but for other languages/tecnhologies there are the coutnerparts.当然,如果您具有 Delphi 技能,这很有意义,但是对于其他语言/技术,则有相关的合作伙伴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM