0

我正在用 Eclipse 在 java 中编写一个基本的 TCP 服务器,并且在从客户端接收到特定数据包后,我试图执行一个 MSSQL 存储过程。我可以记录所有内容,但是当我尝试插入行时,会出现此错误:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

试图用谷歌搜索解决方案,但没有任何效果,一直给我这个错误。这是我的类路径 xml(jar 文件在那里):

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry exported="true" kind="lib" path="/home/leandro/workspace/LHub/sqljdbc4.jar"/>
    <classpathentry exported="true" kind="lib" path="/home/leandro/workspace/LHub/sqljdbc.jar"/>
    <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

这是我与插入的 db 类的连接:

package servers;

import java.sql.*;

public class LServerDBConn {

    private Statement stmt;
    private String DriverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private String url = "jdbc:microsoft:sqlserver://192.168.x.x;databaseName=XXX";
    private String DBuser = "sa";
    private String DBpassword = "123123";

    public void connectionText(String query) throws ClassNotFoundException,SQLException {
        Class.forName(this.DriverName);

        Connection con = DriverManager.getConnection(this.url, this.DBuser, this.DBpassword);

        this.stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery("EXEC SPI_TK103_PACKET" + query);
        if (rs.next()) {
            System.out.println("DBConnection success");
        } else {
            System.out.println("DBConnection failure");
        }
        con.close();
    }

}

这是我的主要课程:

LServerDBConn con = new LServerDBConn();
con.connectionText(new String(buf, 0, bytes_read));

谢谢你的帮助。

[编辑] 文件似乎有问题。该项目没有读取文件,因此找不到类。我尝试使用 JTDS 并且发生了同样的情况。我想我缺少一些配置。

4

1 回答 1

1

看起来你错过了

sqljdbc4.jar 或 sqljdbc.jar

在你的类路径中。

编辑(如何将它包含在您的类路径中) 类路径不容易在一个快速答案中解释,您需要查看类路径维基百科,基本上您必须告诉您的 java 应用程序在哪里寻找 jars/resources 以加载到其类路径中.

Linux

java -classpath '/home/leandro/workspace/LHub/*:' YourApp

下载 tar.gz 并解压以获取 jar 尝试从Microsoft下载它

于 2013-06-18T15:59:15.847 回答