【问题标题】:Java connection to SQL Server (Error)Java 连接到 SQL Server(错误)
【发布时间】:2014-02-08 22:28:35
【问题描述】:

我一直在尝试以多种不同的方式连接到我的 SQL 服务器。我正在为 Microsoft SQL Server 2012 使用 jdbc4 驱动程序。我已经在构建路径中输入了它。当我尝试将我的代码作为 android 应用程序运行时,我的困惑就出现了。运行代码时出现以下错误。

 Invalid layout of java.lang.String at value

 A fatal error has been detected by the Java Runtime Environment:

 Internal Error (javaClasses.cpp:136), pid=5992, tid=5208
 fatal error: Invalid layout of preloaded class

 JRE version:  (7.0_51-b13) (build )
 Java VM: Java HotSpot(TM) 64-Bit Server VM (24.51-b03 mixed mode windows-amd64 compressed oops)
 Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

 An error report file with more information is saved as:
 C:\Users\LiL_Blevs11\Google Drive\CPET 490\Database\workspace\MedRecords\hs_err_pid5992.log

如果您想提交错误报告,请访问: http://bugreport.sun.com/bugreport/crash.jsp

package com.seniordesign.medrecords;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import android.app.Activity;

public class MainActivity extends Activity {

public static void main(String[] args) {


    Connection connection = null;

    try {

        // Load the NetDirect JDBC driver

        String driverName = "com.microsoft.sqlserver.jdbc";

        Class.forName(driverName);

        // Create a connection to the database

        String computer = "LIL_BLEVS-PC";

        String serverName = "SQLEXPRESS";

        String serverPort = "1433";

        String database = computer + "\"" + serverName + ":" + serverPort;

        String url = "jdbc:sqlserver://" + database;

        String username = "username";

        String password = "password";

        connection = DriverManager.getConnection(url, username, password);

        System.out.println("Successfully Connected to the database!");

    } catch (ClassNotFoundException e) {

        System.out.println("Could not find the database driver "
                + e.getMessage());
    } catch (SQLException e) {

        System.out.println("Could not connect to the database "
                + e.getMessage());
    }

}

}

【问题讨论】:

    标签: java android sql sql-server-2012


    【解决方案1】:

    您的类名驱动程序似乎不正确,更改为:

    driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
    

    并确保驱动程序 jar 在类路径中。

    【讨论】:

      【解决方案2】:

      不确定驱动程序是否如 isah 所说的那样不正确,但您的错误看起来很像 that one

      所以我认为您忘记了一步:Run As -> Android Application

      【讨论】:

        猜你喜欢
        • 2013-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-31
        • 2011-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多