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