简体   繁体   English

如何将我的应用程序连接到 SQL Server 数据库?

[英]How to connect my app to a SQL Server database?

How to connect Android app to a SQL Server database?如何将 Android 应用程序连接到 SQL Server 数据库?

I want to make an Android login that connects to sql server but I am confused to make a connection from Android to sql server我想建立一个连接到 sql server 的 Android 登录,但我很困惑要建立从 Android 到 sql server 的连接

First, download the driver from this link and place it at the following address https://sourceforge.net/projects/jtds/ Next you need to introduce the location of the above file in Gardel: Complite file ('libs / jtds-1.3.1.jar) Then create a class with the desired name and in it create values ​​(username and password and database name and IP name) of the string type首先从这个链接下载驱动,放在如下地址https://sourceforge.net/projects/jtds/接下来需要引入上面文件在Gardel中的位置:Complite file('libs/jtds-1.3 .1.jar) 然后创建一个具有所需名称的类,并在其中创建字符串类型的值(用户名和密码以及数据库名称和 IP 名称)

public class sqlConnectionToSQL {
    // Database Username
    String userName;
    // Database Password
    String password;
    // Connection builder class
    String classes;
    // Site or Local IP
    String ip;
}

Then create the connection function in this way and put the following code in it然后以这种方式创建连接函数,并在其中放入以下代码

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder ();
StrictMode.setThreadPolicy (policy);
    Connection conn = null;
    String connURL = null;
try

    {
        Class.forName (classes)
        connURL = "jdbc: jtds: sqlserver: //" + ip + ": 1433" + "; databasename:" + db + "; Username:" + userName + "; Password:" + password + ";";
    conn = DriverManager.getConnection (connURL);
    } Catch (ClassNotFoundException e) {
        Log.e (e.getMessage (), "Error:");
    } Catch (sqlException e) {
        Log.e (e.getMessage (), "Error:");
    }
    Connection Catch (Exception e) {
        Log.e (e.getMessage (), "Error:");
    }
return conn;
}

This is the way to connect the database to Android这是将数据库连接到Android的方式

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

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