简体   繁体   English

与SQL Server的Java连接(错误)

[英]Java connection to SQL Server (Error)

I have been trying to connect to my SQL server many different way. 我一直在尝试以多种不同方式连接到我的SQL Server。 I am using jdbc4 driver for Microsoft SQL Server 2012 . 我正在为Microsoft SQL Server 2012使用jdbc4驱动程序 I have input it in the build path. 我已经在构建路径中输入了它。 My confusion comes when I try to run my code as an android application. 当我尝试将代码作为android应用程序运行时,我感到困惑。 I get the following error when the code is ran. 运行代码时出现以下错误。

 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

If you would like to submit a bug report, please visit: http://bugreport.sun.com/bugreport/crash.jsp 如果您想提交错误报告,请访问: 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());
    }

}

} }

Your class name driver does not seem correct, change to: 您的类名驱动程序似乎不正确,请更改为:

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

and make sure the driver jar is in the classpath. 并确保驱动程序jar位于类路径中。

Don't know for sure if the driver is incorrect as isah says, but your error seems pretty much like that one 不能确定驱动程序是否如isah所说的那样不正确,但是您的错误似乎很像那一个

So I think you are forgetting one step: Run As -> Android Application 所以我认为您忘记了一步:运行方式-> Android应用程序

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

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