You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中连接SQL Server失败求助:getConnection异常排查

问题排查请求

已下载jtds与JDBC驱动并放入libs目录,尝试使用com.microsoft.sqlserver.jdbc.SQLServerDriver和net.sourceforge.jtds.jdbc.Driver两种驱动,且已在build.gradle中添加依赖、在AndroidManifest.xml配置了网络权限,但连接仍失败。确定问题出在getConnection方法,以下是相关配置代码,请求协助排查:

build.gradle依赖配置

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    //implementation 'com.microsoft.sqlserver:mssql-jdbc:9.4.0.jre8'
    //implementation 'net.sourceforge.jtds:jtds:1.3.1'
    implementation files('libs/jtds-1.3.1.jar')
    implementation 'com.microsoft.sqlserver:mssql-jdbc:12.2.0.jre8'
}

AndroidManifest.xml权限配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.canteenmeal">
    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-feature android:name="android.hardware.nfc" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.CanteenMeal"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
            </intent-filter>
        </activity>
        <activity
            android:name=".AdminActivity"
            android:exported="true">
            <!-- Define any necessary intent filters or other attributes for AdminActivity -->
        </activity>
        <activity
            android:name=".CanteenEmployeeActivity"
            android:exported="true">
            <!-- Define any necessary intent filters or other attributes for CanteenEmployeeActivity -->
        </activity>
        <!-- Other manifest entries -->
    </application>
</manifest>

DatabaseHelper.java核心代码

public class DatabaseHelper {
    private static final String DATABASE_URL = "jdbc:sqlserver://10.1.32.111:1433/PROD";
    private static final String DATABASE_USERNAME = "Sexy";
    private static final String DATABASE_PASSWORD = "sexy";
    // 表常量定义略
    private Connection connection;
    private Context context;
    public DatabaseHelper(Context context) {
        this.context= context;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            connection = DriverManager.getConnection(DATABASE_URL, DATABASE_USERNAME, DATABASE_PASSWORD);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}

SQL Server详情

SQL Server详情

排查方向

  • URL格式错误:微软官方JDBC驱动的URL格式应为jdbc:sqlserver://[服务器IP]:[端口];databaseName=[数据库名],你当前的jdbc:sqlserver://10.1.32.111:1433/PROD不符合规范,需改为jdbc:sqlserver://10.1.32.111:1433;databaseName=PROD;若使用jtds驱动,URL格式才是jdbc:jtds:sqlserver://10.1.32.111:1433/PROD,两种驱动格式不能混用。
  • 驱动冲突:同时引入微软JDBC和jtds驱动会导致类加载冲突,建议只保留一种驱动测试,注释掉另一个依赖。
  • 主线程限制:Android禁止在主线程执行网络操作,你在DatabaseHelper构造方法中直接调用getConnection,会触发NetworkOnMainThreadException,必须将连接操作移到子线程(比如用Coroutine、AsyncTask或Thread)。
  • SQL Server配置问题:确认SQL Server已开启远程连接、启用TCP/IP协议、端口1433对外开放,且账号Sexy拥有远程访问PROD数据库的权限。
  • 日志信息缺失:仅打印堆栈无法精准定位问题,建议在catch块中输出e.getMessage(),获取具体错误原因(如认证失败、连接超时、驱动未找到等)。

内容的提问来源于stack exchange,提问作者Umendran Muniandy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 03:50:06