如何将Android Studio与SQL Server数据库连接 [英] How to connect Android Studio with SQL Server database

查看:307
本文介绍了如何将Android Studio与SQL Server数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,通过尝试将基于JVM的IDE Android Studio与MS SQL Server连接起来,如果我的尝试是徒劳的,请立即告诉我.我确实知道Oracle或MySQL会更好.但是,我想尝试一下.

First, just tell me right off the bat if my attempts are futile by trying to connect Android Studio, a JVM-based IDE, with MS SQL Server. I do understand Oracle or MySQL would be better. However, I would like to try it.

通过将外部依赖关系添加到sqljdbc42.jar文件并使用Microsoft提供的一些便捷代码,我已经成功使用Eclipse访问了SQL Server数据库(忽略公共静态void main方法...这是从Eclipse获得的): /p>

I have successfully accessed a SQL Server database using Eclipse by adding an external dependency to the sqljdbc42.jar file and with some handy code provided by Microsoft (ignore the public static void main method...This was from Eclipse):

//=====================================================================
//
//  File:    connectURL.java      
//  Summary: This Microsoft JDBC Driver for SQL Server sample application
//       demonstrates how to connect to a SQL Server database by using
//       a connection URL. It also demonstrates how to retrieve data 
//       from a SQL Server database by using an SQL statement.
//
//---------------------------------------------------------------------
//
//  This file is part of the Microsoft JDBC Driver for SQL Server Code Samples.
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
//  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
//===================================================================== 

import java.sql.*;

public class connectURL {

public static void main(String[] args) {

    // Create a variable for the connection string.
    String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
        "databaseName=AdventureWorks2014;integratedSecurity=true;";

    // Declare the JDBC objects.
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;

        try {
            // Establish the connection.
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                con = DriverManager.getConnection(connectionUrl);

                // Create and execute an SQL statement that returns some data.
                String SQL = "SELECT TOP 10 * FROM Person.ContactType";
                stmt = con.createStatement();
                rs = stmt.executeQuery(SQL);

                // Iterate through the data in the result set and display it.
                while (rs.next()) {
                    System.out.println(rs.getString(1) + " " + rs.getString(2));
                }
        }

    // Handle any errors that may have occurred.
    catch (Exception e) {
        e.printStackTrace();
    }

    finally {
        if (rs != null) try { rs.close(); } catch(Exception e) {}
            if (stmt != null) try { stmt.close(); } catch(Exception e) {}
            if (con != null) try { con.close(); } catch(Exception e) {}
    }
}
}

本质上,我对Android Studio采用了相同的方法.我使用了Microsoft提供的相同的JDBC 4.2驱动程序.在build.gradle文件中,我尝试相应地调整了依赖性,以适应新的.jar文件.以下是我的build.gradle文件:

Essentially, I took the same approach for Android Studio. I used the same JDBC 4.2 driver provided by Microsoft. In the build.gradle file I have tried adjusted the dependencies accordingly to accommodate for the new .jar file. Below is my build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'

    defaultConfig {
        applicationId "androidapp.dillon.betterhalf_v2"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    //compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile files('libs/sqljdbc42.jar')
    }

您可以看到我已注释掉"compile fileTree(...)",这是根据我发现的其他帖子的指示.我还在每条指令的defaultConfig块中添加了"multiDexEnabled true"行.

You can see that I have commented out "compile fileTree(...)" This was by instruction from other posts I found. I also added the "multiDexEnabled true" line in my defaultConfig block per instruction.

问题出在sqljdbc42.jar并将其添加为依赖项.包括它在内,我的项目可以构建,清理和重建,但不能运行或调试.它会产生我发现的所有错误.关于该错误的大多数帖子都引用了我正在使用的JDK版本.我正在使用jdk1.8.0_71.我确实尝试针对JDK 1.7.错误消息如下:

The issue lies in the sqljdbc42.jar and adding it as a dependency. With it included, my project Builds, it Cleans, and it Rebuilds, but it does NOT Run or Debug. It generates an error(s) that I have found all over. Most posts on the error refer to the version of JDK I am using. I am using jdk1.8.0_71. I did try to target JDK 1.7. The error messages are as follows:

错误1:

Error:com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)

错误2:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:Process'command'C:\ Program Files \ Java \ jdk1 .8.0_71 \ bin \ java.exe''的退出值非零完成1

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_71\bin\java.exe'' finished with non-zero exit value 1

我为奇怪的格式表示歉意. 谢谢您的帮助.如果您需要更多信息,请发表评论.

I apologize for the weird formatting. Thanks, for you help. Please comment if you need any more information.

推荐答案

找到了解决方法.没有太多的答案.使用SourceForge提供的另一种JDBC驱动程序,而不是Microsoft提供的另一种.在此处中进行检查.

Found a workaround. Not much of an answer. Use a different JDBC driver provided by SourceForge instead of the one provided by Microsoft. Check it out here.

这篇关于如何将Android Studio与SQL Server数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆