0

I need to Connect the SQL Server 2008 from the Java.

Here is my code:

public class Sql {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");


         public static void main(String[] args){
                // Neue DB und los geht's :)
                DB db = new DB();
                db.dbConnect("jdbc:sqlserver://Data Source=500.20.13.1;InitialCatalog=LicenceManagement;UseID=XXXXX;Password=YYYY");
            }
    }

    class DB{

        public void dbConnect(  String db_connect_string, 
                                String db_userid, 
                                String db_password){
            try{
            Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
                Connection conn = DriverManager.getConnection(
                                db_connect_string, 
                            db_userid, 
                            db_password);
                System.out.println( "connected" );
            }
            catch( Exception e ){
                e.printStackTrace();
            }
        }
    };

But the connection isn't established, and I get the following error:

ERR :No suitable driver found for jdbc:sqlserver://Data Source=500.20.13.1;InitialCatalog=LicenceManagement;UseID=XXXXX;Password=YYYY"

Moni
  • 47
  • 2
  • 11
  • 1
    Do you have SQL Server jdbc driver on path? – nick79 Apr 25 '18 at 11:43
  • driver is missing, include that - No suitable driver found for jdbc:sqlserver – JavaLearner1 Apr 25 '18 at 11:44
  • 1
    Have you downloaded and added SQL Server JDBC driver to your classpath? And it should be UserId not UseId. Connection string looks incorrect too – Siraj K Apr 25 '18 at 11:46
  • You have the wrong URL format: https://learn.microsoft.com/en-us/sql/connect/jdbc/using-the-jdbc-driver?view=sql-server-2017#making-a-simple-connection-to-a-database –  Apr 25 '18 at 11:52

2 Answers2

0

first of all you have to find a driver like @Benedikt Geltenpoth said above (or below).

Secondly include your driver in your classpath, after you've download it.

Third From Java 1.6 upward, you dont need to register the driver class anymore see (in theorie) here. the driver is JDBC type 4

Lastly A simple pattern for your connection would be jdbc:sqlserver://server:port;DatabaseName=dbname plus your url parameters

    public class Sql {

             public static void main(String[] args){
                    // Neue DB und los geht's :)
                    DB db = new DB();
                    int yourPort = 1433;
                    String initialCatalog = "LicenceManagement";
                    String userId = "userOne";
                    String password= "passwordOne";
db.dbConnect("jdbc:sqlserver://"+500.20.13.1+":"+yourPort+";DatabaseName="+initialCatalog,userId,password);
                }
        }

        class DB{

            public void dbConnect(  String db_connect_string, 
                                    String db_userid, 
                                    String db_password){
                try{
                    Connection conn = DriverManager.getConnection(
                                    db_connect_string, 
                                db_userid, 
                                db_password);
                    System.out.println( "connected" );
                }
                catch( SQLException e ){
                    e.printStackTrace();
                }
            }
        };
arthur
  • 3,245
  • 4
  • 25
  • 34