【问题标题】:SQL database server connecting to Android StudioSQL 数据库服务器连接到 Android Studio
【发布时间】:2021-04-22 02:44:27
【问题描述】:

我正在 Android Studio 中为学校创建一个应用程序,用户可以在其中选中带有症状的复选框,系统将使用数据库来查找最有可能的诊断。 我已将我的 Access 数据库上传到 Microsoft SQL Server 管理工作室,但当我尝试运行代码时,我收到来自catch (SQLException throwables) {throwables.printStackTrace();command 的SQLERROR。 应用程序不再崩溃,但我仍然收到SQLERROR。

完全例外:

2021-02-02 16:28:15.679 6182-6182/? E/psemedicustest: Unknown bits set in runtime_flags: 0x8000
2021-02-02 16:28:16.080 6182-6182/com.example.ipsemedicustest1 W/psemedicustest: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2021-02-02 16:28:16.082 6182-6182/com.example.ipsemedicustest1 W/psemedicustest: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2021-02-02 16:28:16.204 6182-6217/com.example.ipsemedicustest1 I/AdrenoGLES-0: QUALCOMM build                   : 39a1dfd, Ic628754133
   Build Date                       : 06/29/20
   OpenGL ES Shader Compiler Version: EV031.29.00.13
   Local Branch                     : 
   Remote Branch                    : 
   Remote Branch                    : 
   Reconstruct Branch               : 
2021-02-02 16:28:16.204 6182-6217/com.example.ipsemedicustest1 I/AdrenoGLES-0: Build Config                     : S P 8.0.16 AArch64
2021-02-02 16:28:16.204 6182-6217/com.example.ipsemedicustest1 I/AdrenoGLES-0: Driver Path                      : /vendor/lib64/egl/libGLESv2_adreno.so
2021-02-02 16:28:16.209 6182-6217/com.example.ipsemedicustest1 I/AdrenoGLES-0: PFP: 0x016ee188, ME: 0x00000000
2021-02-02 16:28:18.173 6182-6182/com.example.ipsemedicustest1 W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@e5772e6
2021-02-02 16:28:19.345 6182-6182/com.example.ipsemedicustest1 D/succes: knop ingedrukt
2021-02-02 16:28:19.354 6182-6182/com.example.ipsemedicustest1 D/succes: naar Thuisarts
2021-02-02 16:28:19.366 6182-6182/com.example.ipsemedicustest1 W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@22d4b15
2021-02-02 16:28:19.479 6182-6182/com.example.ipsemedicustest1 D/succes: internettoestemming toegestaan
2021-02-02 16:28:19.479 6182-6182/com.example.ipsemedicustest1 D/succes: Threadpolicy
2021-02-02 16:28:19.487 6182-6182/com.example.ipsemedicustest1 D/succes: zoeken voor verbinding
2021-02-02 16:28:26.779 6182-6182/com.example.ipsemedicustest1 D/mislukt: SQLERROR
2021-02-02 16:28:26.780 6182-6182/com.example.ipsemedicustest1 W/System.err: java.sql.SQLException: Network error IOException: failed to connect to /192.168.178.24 (port 1433) from /:: (port 40922): connect failed: ETIMEDOUT (Connection timed out)
2021-02-02 16:28:26.780 6182-6182/com.example.ipsemedicustest1 W/System.err:     at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
2021-02-02 16:28:26.780 6182-6182/com.example.ipsemedicustest1 W/System.err:     at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
2021-02-02 16:28:26.780 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:580)
2021-02-02 16:28:26.780 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:218)
2021-02-02 16:28:26.781 6182-6182/com.example.ipsemedicustest1 W/System.err:     at com.example.ipsemedicustest1.ThuisartsActivity.onCreate(ThuisartsActivity.java:55)
2021-02-02 16:28:26.781 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.Activity.performCreate(Activity.java:7822)
2021-02-02 16:28:26.781 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.Activity.performCreate(Activity.java:7811)
2021-02-02 16:28:26.781 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1328)
2021-02-02 16:28:26.781 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430)
2021-02-02 16:28:26.781 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3598)
2021-02-02 16:28:26.781 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
2021-02-02 16:28:26.781 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
2021-02-02 16:28:26.781 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
2021-02-02 16:28:26.782 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2164)
2021-02-02 16:28:26.782 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:107)
2021-02-02 16:28:26.782 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.os.Looper.loop(Looper.java:241)
2021-02-02 16:28:26.782 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7582)
2021-02-02 16:28:26.782 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-02-02 16:28:26.782 6182-6182/com.example.ipsemedicustest1 W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
2021-02-02 16:28:26.782 6182-6182/com.example.ipsemedicustest1 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
2021-02-02 16:28:26.782 6182-6182/com.example.ipsemedicustest1 W/System.err: Caused by: java.net.ConnectException: failed to connect to /192.168.178.24 (port 1433) from /:: (port 40922): connect failed: ETIMEDOUT (Connection timed out)
2021-02-02 16:28:26.782 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:143)
2021-02-02 16:28:26.782 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
2021-02-02 16:28:26.783 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
2021-02-02 16:28:26.783 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
2021-02-02 16:28:26.783 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
2021-02-02 16:28:26.783 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
2021-02-02 16:28:26.783 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.Socket.connect(Socket.java:621)
2021-02-02 16:28:26.783 6182-6182/com.example.ipsemedicustest1 W/System.err:     at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288)
2021-02-02 16:28:26.783 6182-6182/com.example.ipsemedicustest1 W/System.err:     at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251)
2021-02-02 16:28:26.783 6182-6182/com.example.ipsemedicustest1 W/System.err:     at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:331)
2021-02-02 16:28:26.783 6182-6182/com.example.ipsemedicustest1 W/System.err:    ... 19 more
2021-02-02 16:28:26.783 6182-6182/com.example.ipsemedicustest1 W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
2021-02-02 16:28:26.784 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.Linux.connect(Native Method)
2021-02-02 16:28:26.784 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.ForwardingOs.connect(ForwardingOs.java:95)
2021-02-02 16:28:26.784 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:136)
2021-02-02 16:28:26.784 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.ForwardingOs.connect(ForwardingOs.java:95)
2021-02-02 16:28:26.784 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:157)
2021-02-02 16:28:26.784 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:135)
2021-02-02 16:28:26.784 6182-6182/com.example.ipsemedicustest1 W/System.err:    ... 28 more
2021-02-02 16:28:26.810 6182-6182/com.example.ipsemedicustest1 I/Choreographer: Skipped 446 frames!  The application may be doing too much work on its main thread.
2021-02-02 16:28:26.868 6182-6217/com.example.ipsemedicustest1 I/OpenGLRenderer: Davey! duration=7495ms; Flags=1, IntendedVsync=158535574851321, Vsync=158543008184357, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=158543013906986, AnimationStart=158543013972611, PerformTraversalsStart=158543014622923, DrawStart=158543053212146, SyncQueued=158543059002667, SyncStart=158543059390584, IssueDrawCommandsStart=158543059519490, SwapBuffers=158543069816887, FrameCompleted=158543071013398, DequeueBufferDuration=116000, QueueBufferDuration=575000, 
2021-02-02 16:28:26.875 6182-6217/com.example.ipsemedicustest1 I/OpenGLRenderer: Davey! duration=7503ms; Flags=1, IntendedVsync=158535574851321, Vsync=158543008184357, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=158543013906986, AnimationStart=158543013972611, PerformTraversalsStart=158543014622923, DrawStart=158543072334179, SyncQueued=158543073167877, SyncStart=158543073501054, IssueDrawCommandsStart=158543073591679, SwapBuffers=158543074732408, FrameCompleted=158543078368450, DequeueBufferDuration=145000, QueueBufferDuration=1850000, 
2021-02-02 16:28:26.905 6182-6217/com.example.ipsemedicustest1 D/OpenGLRenderer: endAllActiveAnimators on 0x7aed745d00 (RippleDrawable) with handle 0x7b441ed820
2021-02-02 16:32:23.391 6182-6182/com.example.ipsemedicustest1 D/succes: knop ingedrukt
2021-02-02 16:32:23.391 6182-6182/com.example.ipsemedicustest1 D/mislukt: knop ingedrukt
2021-02-02 16:32:23.406 6182-6182/com.example.ipsemedicustest1 D/mislukt: naar pagina
2021-02-02 16:32:23.423 6182-6182/com.example.ipsemedicustest1 W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@388132
2021-02-02 16:33:14.165 6182-6182/com.example.ipsemedicustest1 D/succes: knop ingedrukt
2021-02-02 16:33:14.179 6182-6182/com.example.ipsemedicustest1 D/succes: naar Thuisarts
2021-02-02 16:33:14.188 6182-6182/com.example.ipsemedicustest1 W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@22c4719
2021-02-02 16:33:14.248 6182-6182/com.example.ipsemedicustest1 D/succes: internettoestemming toegestaan
2021-02-02 16:33:14.248 6182-6182/com.example.ipsemedicustest1 D/succes: Threadpolicy
2021-02-02 16:33:14.248 6182-6182/com.example.ipsemedicustest1 D/succes: zoeken voor verbinding
2021-02-02 16:33:21.385 6182-6182/com.example.ipsemedicustest1 D/mislukt: SQLERROR
2021-02-02 16:33:21.385 6182-6182/com.example.ipsemedicustest1 W/System.err: java.sql.SQLException: Network error IOException: failed to connect to /192.168.178.24 (port 1433) from /:: (port 40974): connect failed: ETIMEDOUT (Connection timed out)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:580)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:218)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at com.example.ipsemedicustest1.ThuisartsActivity.onCreate(ThuisartsActivity.java:55)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.Activity.performCreate(Activity.java:7822)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.Activity.performCreate(Activity.java:7811)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1328)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3598)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
2021-02-02 16:33:21.386 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2164)
2021-02-02 16:33:21.387 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:107)
2021-02-02 16:33:21.387 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.os.Looper.loop(Looper.java:241)
2021-02-02 16:33:21.387 6182-6182/com.example.ipsemedicustest1 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7582)
2021-02-02 16:33:21.387 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-02-02 16:33:21.387 6182-6182/com.example.ipsemedicustest1 W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
2021-02-02 16:33:21.387 6182-6182/com.example.ipsemedicustest1 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
2021-02-02 16:33:21.387 6182-6182/com.example.ipsemedicustest1 W/System.err: Caused by: java.net.ConnectException: failed to connect to /192.168.178.24 (port 1433) from /:: (port 40974): connect failed: ETIMEDOUT (Connection timed out)
2021-02-02 16:33:21.387 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:143)
2021-02-02 16:33:21.387 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
2021-02-02 16:33:21.387 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
2021-02-02 16:33:21.387 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err:     at java.net.Socket.connect(Socket.java:621)
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err:     at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288)
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err:     at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251)
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err:     at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:331)
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err:    ... 19 more
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.Linux.connect(Native Method)
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.ForwardingOs.connect(ForwardingOs.java:95)
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:136)
2021-02-02 16:33:21.388 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.ForwardingOs.connect(ForwardingOs.java:95)
2021-02-02 16:33:21.389 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:157)
2021-02-02 16:33:21.389 6182-6182/com.example.ipsemedicustest1 W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:135)
2021-02-02 16:33:21.389 6182-6182/com.example.ipsemedicustest1 W/System.err:    ... 28 more
2021-02-02 16:33:21.412 6182-6182/com.example.ipsemedicustest1 I/Choreographer: Skipped 433 frames!  The application may be doing too much work on its main thread.
2021-02-02 16:33:21.454 6182-6217/com.example.ipsemedicustest1 I/OpenGLRenderer: Davey! duration=7264ms; Flags=1, IntendedVsync=158830392856556, Vsync=158837609522934, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=158837615888834, AnimationStart=158837615956125, PerformTraversalsStart=158837616267323, DrawStart=158837647559931, SyncQueued=158837653362744, SyncStart=158837653690869, IssueDrawCommandsStart=158837653800296, SwapBuffers=158837656434359, FrameCompleted=158837657286702, DequeueBufferDuration=96000, QueueBufferDuration=408000, 

在Log.d("succes", "zoeken voor verbinding"); 之后一切顺利 (见下文)。好像无法建立连接,对吧? 我复制了这段视频中的代码:https://www.youtube.com/watch?v=MnmEXqfV5BU。

ps。 /* */ 之间的复选框部分尚未测试。我打算在用户选中的框上过滤我的数据库。首先,我需要解决所描述的问题(也是为了测试 this 部分),但如果您提前有任何建议,我们非常欢迎。 我隐藏了我的 IP 地址,但我使用了正确的地址。

public class ThuisartsActivity extends AppCompatActivity {

    private TextView textView;

    private static String ip = "";
    private static String port = "1433";
    private static String Classes = "net.sourceforge.jtds.jdbc.Driver";
    private static String database = "database";
    private static String username = "testentesten";
    private static String password = "testentesten";
    private static String url = "jdbc:jtds:sqlserver://"+ip+":"+port+"/"+database;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thuisarts);
        getSupportActionBar().setTitle("Thuisarts zelftest");

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET}, PackageManager.PERMISSION_GRANTED);
        Log.d("succes", "internettoestemming toegestaan");

        textView = (TextView)findViewById(R.id.textView);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Log.d("succes", "Threadpolicy");

            try {
                Class.forName(Classes);
                Log.d("succes", "zoeken voor verbinding");
                Connection connection = DriverManager.getConnection(url, username, password);
                Log.d("succes", "verbinding");
                textView.setText("Succes");
                Log.d("succes", "ingelogd server");
            } catch (ClassNotFoundException e) {
                Log.d("mislukt", "ERROR");
                e.printStackTrace();
                textView.setText("ERROR");
            } catch (SQLException throwables) {
                Log.d("mislukt", "SQLERROR");
                throwables.printStackTrace();
                textView.setText("SQLERROR");
            }


    }
   public void sqlButton(View view) {
        Log.d("succes", "knop ingedrukt");
        Connection connection = null;
        if (connection != null) {
            Log.d("succes", "connection !=null");
            try {
                Statement statement = connection.createStatement();
/* final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox);
if (checkBox.isChecked()) {
String name = ((CheckBox) view).getText().toString();
*/
                    ResultSet resultSet = statement.executeQuery("SELECT TOP(1) aandoening FROM dbo.dbim WHERE" + name + " = 1");
                    while (resultSet.next()) {
                        textView.setText(resultSet.getString(1));
                    }
                }

            } catch (SQLException throwables) {
                throwables.printStackTrace();

            }

        } else {
            Log.d("mislukt", "knop ingedrukt");
            Intent g = new Intent(this, MainActivity.class);
            Log.d("mislukt", "naar pagina");
            textView.setText("Geen verbinding");
        }
    }
}

【问题讨论】:

  • 请编辑您的问题并将包括堆栈跟踪(来自 logcat)在内的整个异常粘贴到问题中。
  • 基本上这是很糟糕的代码。它正在onCreate() 中请求权限,但如果未授予权限,应用程序将崩溃。它在主 (UI) 线程上执行网络 I/O,这是危险的、不受欢迎的并且通常是一个坏主意(因为它还可能导致应用程序因 ANR 应用程序无响应而崩溃)。这些问题可能会或可能不会导致您立即遇到问题,但我只是想告诉您,这是构建您的应用程序的糟糕基础。
  • 感谢您的警告,听起来很熟悉。关于如何修复它的任何建议?我应该创建一个单独的空隙还是什么?
  • 请将 logcat 中的异常和堆栈跟踪复制到您的问题中。
  • 第 66 行是什么?它在哪一行崩溃?

标签: java android database android-studio


【解决方案1】:

编辑:您忘记在 findviewbyid 之前放置 (TextView),这就是在第 66 行给出空指针异常的原因。

将textView = findViewById(R.id.textView); 更改为textView = (TextView) findViewById(R.id.textView);

完成此操作后,再次发布堆栈跟踪仍然有错误。

【讨论】:

  • 我故意隐藏了我的IP(在这里),但在测试代码中我使用了正确的(cmd.exe-->ipconfig)。此外,我检查了 TCP/IP 都已启用。我使用的是 SQL Delevoper 而不是 Express,在这种情况下我需要更改代码吗?当我回家时,我会添加整个例外。提前致谢。
  • 此外,如果我将您的查询字符串与我的进行比较,我注意到表名不同。我不添加“dbo”。部分,但只使用我的数据库的名称。在您的示例中,您的字符串将是: "SELECT TOP(1) aandoening FROM dbim WHERE" + name + " = 1" 可能会出现问题的另一件事是您连接查询字符串。出于调试目的,通过表中的名称删除“+name+”,因此它只是 1 个完整的字符串。
  • 您是否检查过服务器正在侦听端口 1433?为此,请转到 sql 服务器管理器,转到 SQL Server 网络配置,然后双击 tcp/ip ,转到 IP 地址并向下滚动到底部。确保动态端口为空并且 TCP 端口设置为 1433
猜你喜欢
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 2017-08-21
相关资源
最近更新 更多