0

I have a SQL Server 2012 database in the network of my company. I can access to this database to observe the tables etc... with SQL Server Management Studio giving the following :

  • Server type : Database Engine
  • Server name : xxx-yyy-zzz.eu.company.corp, number_of_port
  • Authentication : Windows Authentication
  • Username and password

I also know the name of the database and nothing more.

I would like to connect a new Java program I am writing with Eclipse to this SQL Server database. But I have never connected or even used a SQL Server before so I have no idea how to do it. I understood reading some other posts that I need a driver, but I don't understand where I need to install this driver, which driver and what I need to do to extract data from the database in my Java program.

Could you please tell me more about it ? Thank you :)

LaPalme
  • 339
  • 5
  • 16

2 Answers2

1

I suggest you start here with this Microsoft documentation, Microsoft JDBC Driver for SQL Server.

Looks like you can download the actual driver from Microsoft JDBC Driver 6.0 for SQL Server.

What you need to do may vary depending on the operating system you are using however the above driver looks to be pretty inclusive for Linux, Windows and also works with Azure, Microsoft's cloud offering.

The Programming Guide for JDBC SQL Driver has a number of links explaining what you will need to do and Building the Connection URL describes the actual connect string.

This stack overflow post has a sample program. Java program to connect to Sql Server and running the sample query From Eclipse.

You may also find How to connect to Microsoft SQL Server database using Eclipse to be helpful.

Just remember, you need Eclipse IDE for Java EE developers to access the database. It contains tools for database development e.g. database explorer. The Eclipse IDE for Java Developers doesn't contain those tool by default. FYI, I am using Eclipse Java EE IDE for Web Developers, Version: Kepler Service Release 2.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • Thank you for your help. I think there is enough information in your post to go forward and the examples below are also useful. I cannot try inmediatly the solution because with the proxy of my company I need days and days of authorizations to download a driver but I'm pretty sure it will work then. Thanks. – LaPalme Jul 06 '17 at 15:05
  • @LaPalme let me know how it goes by posting a comment to this answer. Good luck. – Richard Chambers Jul 06 '17 at 18:29
  • sure ! I'll let you know, thanks. – LaPalme Jul 07 '17 at 09:16
  • Just to let you know that your solution worked well ! Thank you and have a nice day. – LaPalme Jul 18 '17 at 09:13
  • @LaPalme I'm glad to hear that and I appreciate you letting me know how it turned out. Take care and enjoy life. – Richard Chambers Jul 18 '17 at 11:31
1

Some time back I had problems with Microsoft JDBC Driver and used open source JTDS. I do not remember exact problems, but JTDS worked just fine for me.

So, code to connect to database may look like:

        //Not required anymore - just for demonstration. Driver class must be in class path  
        Class.forName("net.sourceforge.jtds.jdbc.Driver");

        Connection dbCon = DriverManager
                .getConnection("jdbc:jtds:sqlserver://{db_host}:[db_port]/{Database Name};domain={user Windows domain};user={user id};password={user password}");
        PreparedStatement stmt = dbCon.prepareStatement("SELECT GETDATE()");
        ResultSet resSet = stmt.executeQuery();
        while (resSet.next()) {
            System.out.println(resSet.getString(1));
        }
        System.out.println("Done");
        dbCon.close();

Maven dependency for JTDS driver:

        <dependency>
           <groupId>net.sourceforge.jtds</groupId>
           <artifactId>jtds</artifactId>
           <version>1.3.1</version>         
        </dependency>

BTW: Maybe there is a newer version available...

Vadim
  • 4,027
  • 2
  • 10
  • 26