javaclassnotfoundexceptionsqljdbcmssql-jdbc

java.lang.ClassNotFoundException on TCP Server


I'm writting a basic TCP Server in java with Eclipse, and I'm trying to execute a MSSQL Stored Procedure after receiving an specific packet from the client. I'm able to log everything, but when I try to insert the row, this error appears:

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

Tried to google for solution but nothing works, keeps giving me that error. Here is my classpath xml (the jar files are there):

<?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>

This is my connection to db class for the insert:

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();
    }

}

This is in my main class:

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

Thanks for any help.

[EDIT] It seems to be a problem with the files. The project is not reading the files therefore it can't find the classes. I tried with JTDS and the same happens. I think i'm mmissing some configuration.


Solution

  • Looks like you are missing the

    sqljdbc4.jar or sqljdbc.jar

    in your classpath.

    Edit (how to include it in your classpath) The classpath is not easy to explain in one quick answer you will need to look at classpath wikipedia, basically you have to tell your java application where to look for jars/resources to load into its classpath.

    Linux

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

    Download the tar.gz and extract it to get the jar Try downloading it from Microsoft