简体   繁体   English

可以使用此代码连接到SQL Server 2008吗?

[英]Can this code be used to connect to a SQL server 2008?

I'm trying to connect to a Microsoft SQL 2008 server, this seems to work for others, but I'm having no luck. 我正在尝试连接到Microsoft SQL 2008服务器,这似乎对其他人有用,但是我没有运气。 I know it isn't the best practice to directly connect to the server, but this app is only for demonstration and won't be distributed beyond my device. 我知道直接连接到服务器不是最佳实践,但是此应用程序仅用于演示,不会在我的设备之外分发。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import net.sourceforge.jtds.jdbc.*;

public void query2()
{
    Log.i("Android"," MySQL Connect Example.");
    Connection conn = null;
    try {
        String driver = "net.sourceforge.jtds.jdbc.Driver";
        Class.forName(driver).newInstance();
        //test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
        String connString = "jdbc:jtds:sqlserver://server_ip_address               
        :1433/DBNAME;encrypt=fasle;user=xxxxxxxxx;password=xxxxxxxx;instance=SQLEXPRESS;";
        String username = "xxxxxx";
        String password = "xxxxxxxxxx";
        conn = DriverManager.getConnection(connString,username,password);
        Log.w("Connection","open");
        Statement stmt = conn.createStatement();
        ResultSet reset = stmt.executeQuery("select * from TableName");

        //Print the data to the console
        while(reset.next()){
            Log.w("Data:",reset.getString(3));
            // Log.w("Data",reset.getString(2));
        }
        conn.close();
    } 
    catch (Exception e)
    {
        Log.w("Error connection","" + e.getMessage());
    }
}

If not, what needs to be changed? 如果不是,则需要更改什么? It crashes on the connection statement each time. 每次它都会在连接语句上崩溃。 Thank You. 谢谢。

Edit: Here is the logcat for the event. 编辑:这是事件的日志。

12-12 06:11:41.273: E/AndroidRuntime(5745): FATAL EXCEPTION: main 12-12 06:11:41.273: E/AndroidRuntime(5745): java.lang.IllegalStateException: Could not execute method of the activity 12-12 06:11:41.273: E/AndroidRuntime(5745): at android.view.View$1.onClick(View.java:3597) 12-12 06:11:41.273: E/AndroidRuntime(5745): at android.view.View.performClick(View.java:4202) 12-12 06:11:41.273: E/AndroidRuntime(5745): at android.view.View$PerformClick.run(View.java:17340) 12-12 06:11:41.273: E/AndroidRuntime(5745): at android.os.Handler.handleCallback(Handler.java:725) 12-12 06:11:41.273: E/AndroidRuntime(5745): at android.os.Handler.dispatchMessage(Handler.java:92) 12-12 06:11:41.273: E/AndroidRuntime(5745): at android.os.Looper.loop(Looper.java:137) 12-12 06:11:41.273: E/AndroidRuntime(5745): at android.app.ActivityThread.main(ActivityThread.java:5039) 12-12 06:11:41.273: E/AndroidRuntime(5745): at java.lang.reflect.Method.invokeNative(Native Method) 12-12 06:11:41.273: E/AndroidRuntime(5745) 12-12 06:11:41.273:E / AndroidRuntime(5745):致命异常:主12-12 06:11:41.273:E / AndroidRuntime(5745):java.lang.IllegalStateException:无法执行活动12的方法-12 06:11:41.273:E / AndroidRuntime(5745):在android.view.View $ 1.onClick(View.java:3597)12-12 06:11:41.273:E / AndroidRuntime(5745):在android。 view.View.performClick(View.java:4202)12-12 06:11:41.273:E / AndroidRuntime(5745):在android.view.View $ PerformClick.run(View.java:17340)12-12 06: 11:41.273:E / AndroidRuntime(5745):位于android.os.Handler.handleCallback(Handler.java:725)12-12 06:11:41.273:E / AndroidRuntime(5745):位于android.os.Handler.dispatchMessage (Handler.java:92)12-12 06:11:41.273:E / AndroidRuntime(5745):位于android.os.Looper.loop(Looper.java:137)12-12 06:11:41.273:E / AndroidRuntime (5745):位于android.app.ActivityThread.main(ActivityThread.java:5039)12-12 06:11:41.273:E / AndroidRuntime(5745):位于java.lang.reflect.Method.invokeNative(本机方法)12 -12 06:11:41.273:E / AndroidRuntime(5745) : at java.lang.reflect.Method.invoke(Method.java:511) 12-12 06:11:41.273: E/AndroidRuntime(5745): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 12-12 06:11:41.273: E/AndroidRuntime(5745): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 12-12 06:11:41.273: E/AndroidRuntime(5745): at dalvik.system.NativeStart.main(Native Method) 12-12 06:11:41.273: E/AndroidRuntime(5745): Caused by: java.lang.reflect.InvocationTargetException 12-12 06:11:41.273: E/AndroidRuntime(5745): at java.lang.reflect.Method.invokeNative(Native Method) 12-12 06:11:41.273: E/AndroidRuntime(5745): at java.lang.reflect.Method.invoke(Method.java:511) 12-12 06:11:41.273: E/AndroidRuntime(5745): at android.view.View$1.onClick(View.java:3592) 12-12 06:11:41.273: E/AndroidRuntime(5745): ... 11 more 12-12 06:11:41.273: E/AndroidRuntime(5745): Caused by: java.sql.SQLException: No suitable driver 12-12 06:11:41.273: E/AndroidRuntime(5745): at java.sql.DriverManager.getConnec :at java.lang.reflect.Method.invoke(Method.java:511)12-12 06:11:41.273:E / AndroidRuntime(5745):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit .java:793)12-12 06:11:41.273:E / AndroidRuntime(5745):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)12-12 06:11:41.273:E / AndroidRuntime(5745):位于dalvik.system.NativeStart.main(本机方法)12-12 06:11:41.273:E / AndroidRuntime(5745):由以下原因引起:java.lang.reflect.InvocationTargetException 12-12 06:11 :41.273:E / AndroidRuntime(5745):at java.lang.reflect.Method.invokeNative(本机方法)12-12 06:11:41.273:E / AndroidRuntime(5745):at java.lang.reflect.Method.invoke (Method.java:511)12-12 06:11:41.273:E / AndroidRuntime(5745):at android.view.View $ 1.onClick(View.java:3592)12-12 06:11:41.273:E / AndroidRuntime(5745):... 11更多12-12 06:11:41.273:E / AndroidRuntime(5745):原因:java.sql.SQLException:没有合适的驱动程序12-12 06:11:41.273:E / AndroidRuntime (5745):位于java.sql.DriverManager.getConnec tion(DriverManager.java:182) 12-12 06:11:41.273: E/AndroidRuntime(5745): at java.sql.DriverManager.getConnection(DriverManager.java:140) 12-12 06:11:41.273: E/AndroidRuntime(5745): at com.nateapp.finalproject.MainActivity.sendData(MainActivity.java:266) 12-12 06:11:41.273: E/AndroidRuntime(5745): ... 14 more tion(DriverManager.java:182)12-12 06:11:41.273:E / AndroidRuntime(5745):at java.sql.DriverManager.getConnection(DriverManager.java:140)12-12 06:11:41.273:E / AndroidRuntime(5745):位于com.nateapp.finalproject.MainActivity.sendData(MainActivity.java:266)12-12 06:11:41.273:E / AndroidRuntime(5745):...还有14个

All it needs to be able to do is an insert statement. 它需要做的就是插入一条语句。

What does "crashes" look like? “崩溃”是什么样的? Post the stack trace. 发布堆栈跟踪。

This code is wrong on every level. 此代码在每个级别上都是错误的。 You shouldn't be creating connections that way in a method that interacts with a database. 您不应该在与数据库交互的方法中以这种方式创建连接。 It's better to create it elsewhere (eg get it from a pool) and pass it into the method. 最好在其他地方创建它(例如,从池中获取它)并将其传递给方法。

You don't close ResultSet or Statement . 您不关闭ResultSetStatement Closing the Connection won't clean those up. 关闭Connection不会清除这些连接。 You should close them in a finally block. 您应该在finally块中关闭它们。

This is the reason for your problem: 这是您出现问题的原因:

12-12 06:11:41.273: E/AndroidRuntime(5745): Caused by: java.sql.SQLException: No suitable driver

"No suitable driver" usually means that the connection URL is borked. “没有合适的驱动程序”通常意味着连接URL异常。 Check that. 检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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