连接到mssql服务器返回null

xxslljrj  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(339)

我想将android应用程序连接到sql server,但连接总是返回null。我没有发现任何有助于相同代码无处不在,但问题是相同的。有什么问题吗?

class DatabaseHandler {

 private static Connection connection;
 private static String db,ip,un,pass,connectionURL,port;
 private static Statement statement;

static Connection getConnection(Context context)
{
    //ActivityCompat.requestPermissions((Activity) context,new String[]{Manifest.permission.INTERNET}, PackageManager.PERMISSION_GRANTED);

    ip = "****"; //database ip
    db = "***"; //database name
    un = "***"; //username to connect to db
    pass = "***"; //password to connect to db
    connectionURL = null;
    port = "1433";

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

    if(connection == null)
    {
        try
        {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            connectionURL = "jdbc:jtds:sqlserver://" + ip + ";databaseName=" + db + ";user=" + un+ ";password=" + pass +";";
            connection =  DriverManager.getConnection(connectionURL);
            statement = connection.createStatement();

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
    Log.i("ifconnectionsucceeded",""+connection);

    return connection;

}
2ul0zpep

2ul0zpep1#

我想出来了。。问题是我使用的是jtdsjar1.3.1,我把它降级到了1.2.7,它运行得很好

相关问题