How to connect Android Studio with SQL Server database?

后端 未结 2 746
太阳男子
太阳男子 2021-01-07 10:18

We are currently having the same problem as here: How to connect Android Studio with SQL Server database, but it hasn\'t been answered.

We also succeeded the connect

2条回答
  •  执笔经年
    2021-01-07 10:47

    Before you make connection, you must add 2 lines of code:

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    

    Complete code as: (may be)

    package com.ipvsoft.sendsms.utility;
    
    import android.os.StrictMode;
    import android.util.Log;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    
    public class SQLConnection {
        private static final String LOG = "DEBUG";
        private static String ip = "192.168.3.85";
        private static String port = "1433";
        private static String classs = "net.sourceforge.jtds.jdbc.Driver";
        private static String db = "THTData";
        private static String un = "sa";
        private static String password = "admin";
        public static Connection connect() {
            Connection conn = null;
            String ConnURL = null;
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            try {
                Class.forName(classs);
                ConnURL = "jdbc:jtds:sqlserver://" + ip +":"+port+";"
                        + "databaseName=" + db + ";user=" + un + ";password="
                        + password + ";";
                conn = DriverManager.getConnection(ConnURL);
            } catch (SQLException e) {
                Log.d(LOG, e.getMessage());
            } catch (ClassNotFoundException e) {
                Log.d(LOG, e.getMessage());
            }
            return conn;
        }
    }
    

提交回复
热议问题