Java connects sqlserver2008 database using Windows authentication

Source: Internet
Author: User
Tags stmt

Code snippet to connect to the database:

 Packagecom.db;ImportJava.sql.*; Public classDbutil { Public Static voidMain (string[] args) {Connection con=NULL; Statement stmt=NULL; ResultSet RS=NULL; String Driver= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; String URL= "jdbc:sqlserver://10.103.2.18:1433;databasename=hcm;integratedsecurity=true;";//Windows Integrated Mode connection        Try{class.forname (driver); Con=drivermanager.getconnection (URL); System.out.println ("Connecting successful!!!!!"); stmt=con.createstatement (); RS= Stmt.executequery ("Select Top ten * from Empdata");  while(Rs.next ()) {System.out.println (rs.getstring ("EmployeeID") + "\ T" +rs.getstring ("Name")); }                                } Catch(SQLException e) {e.printstacktrace (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } finally{            Try{rs.close ();                Stmt.close ();            Con.close (); } Catch(SQLException e) {e.printstacktrace (); }                    }            }}
View Code

The exceptions that occur are:

Failed to load the Sqljdbc_auth.dll cause:no Sqljdbc_auth in Java.library.path

Solution:

Add Sqljdbc_auth.dll to C:\Windows\System32

Exception in thread "main" Java.lang.UnsatisfiedLinkError: Com.microsoft.sqlserver.jdbc.AuthenticationJNI.GetDNSName (ljava/lang/string[ljava/lang/string; Ljava/util/logging/logger;) I

Solution:

1. Adding Sqljdbc4.jar to the project
2. Copy the Sqljdbc_auth.dll file to the following directory

    • C:/windows/system32
    • Under the bin directory of the JDK used by eclipse
    • Under the bin directory of the JRE used by eclipse

<sqljdbc_auth.dll from JDBC Driver x.0 for SQL Server, download URL http://www.microsoft.com/en-us/download/details.aspx?id= 11774 >

Java connects sqlserver2008 database using Windows authentication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.