简体   繁体   中英

How to connect my app to a SQL Server database?

How to connect Android app to a SQL Server database?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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