-1

I found many answers to similar questions but I still can't solve my problem.

My connection code is:

import java.io.*;
import java.text.*;
import java.util.*;
import java.sql.*;

public class TransformData
{
public static void main(String args[])
{
    String url = "jdbc:sqlserver://SQLFULL:1433;databaseName=BA_ELTRUN;";
    Connection dbcon;
    Statement stmt;
    ResultSet rs;
    Scanner input = new Scanner(System.in);

    try
    {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    }
    catch(java.lang.ClassNotFoundException e)
    {
        System.out.print("ClassNotFoundException: ");
        System.out.println(e.getMessage());
    }

    try
    {
        dbcon = DriverManager.getConnection(url,"username","password");
        stmt = dbcon.createStatement();
    }
     catch(SQLException e)
     {
         System.out.print("SQLException: ");
         System.out.println(e.getMessage());
     }
}
}

When I run this I get the following exception:

ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver SQLException: No suitable driver found for jdbc:sqlserver://Konstantina\SQLFULL; databaseName=BA_ELTRUN

sqljdbc.jar and sqljdbca.jar are on this directory:C:\Program Files\Microsoft SQL Server JDBC Driver 2.0\Sqljdbc_2.0\Enu Do I have to move them in another directory?

JAL
  • 41,701
  • 23
  • 172
  • 300
  • if you import `com.microsoft.sqlserver.jdbc.SQLServerDriver` does your app compile without errors? – Mateus Viccari Jan 08 '16 at 16:27
  • @MateusViccari Do you mean in an import statement like this `import com.microsoft.sqlserver.jdbc.SQLServerDriver;` ? Sorry if this is a stupid question, I am new to all this stuff – konna.papag Jan 08 '16 at 16:33
  • Yeah, or just replace `Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");` by `Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver.class.getName());`. If by doing this your IDE shows an error in that line, that's because the library was not correctly added to the project. – Mateus Viccari Jan 08 '16 at 16:35
  • @MateusViccari There is an error message: `error: package com.microsoft.sqlserver.jdbc does not exist import com.microsoft.sqlserver.jdbc.SQLServerDriver;` – konna.papag Jan 08 '16 at 16:37
  • That's it, the lib was not added. What build system are you using? (Maven, ANT) or what IDE are you using? – Mateus Viccari Jan 08 '16 at 16:38
  • @MateusViccari I use Textpad – konna.papag Jan 08 '16 at 16:43
  • Possible duplicate of [java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver](http://stackoverflow.com/questions/5616898/java-sql-sqlexception-no-suitable-driver-found-for-jdbcmicrosoftsqlserver) – Andy Guibert Jan 08 '16 at 20:11

2 Answers2

0

You need to add SQLServerDriver library to the project dependencies

Mateus Viccari
  • 7,389
  • 14
  • 65
  • 101
  • I never used TextPad, but you should have a way to package your application into a .jar file, which would create the manifest.mf file pointing to the libraries. Usually this is easier if you use some IDE, like Netbeans, Eclipse or Intellij rather than a simple text editor – Mateus Viccari Jan 08 '16 at 17:02
0

The problem is that when you register the driver via the Class.forName call, it is expected to already be on the classpath. The ClassNotFoundException you are receiving means that the sqljdbc.jar and sqljdbca.jar libraries are not on your classpath so you will need to ensure they are included. See the following for additional details and exact steps to ensure the libraries are on your classpath: https://msdn.microsoft.com/en-us/library/ms378526(v=sql.110).aspx

pricetag
  • 111
  • 3
  • I add them into these direcories: C:\Program Files\Java\jre7\lib\ext and C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext and finally it works – konna.papag Jan 08 '16 at 18:14