(Java) Error connecting to SQL Server Local

Asked

Viewed 56 times

0

I am trying to get my project to connect with SQL Server Local, but it is showing the following error:

run:
Algo deu errado! 
java.lang.ClassNotFoundException: C:\Program Files\sqljdbc_6.0\ptb\jre8\sqljdbc42.jar

The archive sqljdbc42 is in the destination: C:\Program Files\sqljdbc_6.0\ptb\jre8\sqljdbc42.jar

SQL IP: 192.168.0.12 - Standard port: 1433

Follow my code for testing:

package testesql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager; 


//import com.microsoft.sqlserver.jdbc.SQLServerDriver;

/**
 *
 * @author Ronison Matos
 */
public class TesteSQL {

static final String JDBC_DRIVER = "C:\\Program Files\\sqljdbc_6.0\\ptb\\jre8\\sqljdbc42.jar";
static final String DB_URL = "jdbc:sqlserver://192.168.0.12:1433;DatabaseName=Estoque";

// Database credentials
static final String USER = "sa";
static final String PASS = "123456";

public static void main(String[] args) {
    // TODO code application logic here



    Connection conn = null;
    Statement stmt = null;
    try {
        // Register JDBC driver
        Class.forName(JDBC_DRIVER);

        // Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        stmt = conn.createStatement();
        String sql = "SELECT cod_prod FROM Produto";

        ResultSet result = stmt.executeQuery(sql);

        // Show result
        while (result.next()) {
            // Retrieve by column name
            int cod_prod = result.getInt("cod_prod");
            //int age = result.getInt("salary");
            //String first = result.getString("first");
            //String last = result.getString("last");

            // Display values
            System.out.print("ID: " + cod_prod);
            //System.out.print("\t Pay: " + age);
            //System.out.print("\tFirst: " + first);
            //System.out.println("\tLast: " + last);

        }
        // Clean-up
        result.close();
        stmt.close();
        conn.close();

    } catch (Exception e) {
        System.out.println("Algo deu errado! \n" + e);
    }

}

}

  • 1

    tried to run the program with java -classpath pointing to the folder where the sqljdbc42.jar?

  • 1

    In the Class.forName you pass the name of the driver class, not the path of the jar - something like Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") (confirm in the documentation the correct name). The jar must be in the classpath, either via the command line, as noted above, or via your IDE/dependency manager configuration, but you do not need to reference the file directly in the code

  • I did as mentioned by @hkotsubo. I went through Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") the same error persists. I also performed an update/downloaded the sqljdbc_6.2.2.1_enu.exe, also without advance.

  • @Ricardopunctual, can you kindly explain to me in more detail how I should do this procedure? For me, everything about this is news rs. From now on I thank you all for your help.

  • >Solved! < The file was missing sqljdbc42.jar within the project, done this solved this error. But another error has arisen that I will probably have to ask another question... Error: java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://192.168.0.12:1433;databaseName=Estoque

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.