2017-07-26 64 views
2

我創建應用程序,我需要我的服務連接到SQL Server。當我添加依賴項後運行測試代碼時,它會成功運行。這是一個成功運行的測試代碼:如何通過java代碼連接sql server?

import java.sql.Connection; 
import java.sql.DatabaseMetaData; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.sql.*; 

public class SQLConnection { 

    public static void main(String[] args) throws ClassNotFoundException, SQLException { 
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
       Connection conn = DriverManager.getConnection("jdbc:sqlserver://10.235.5.38\\name;user=sa;password=password;database=dbname"); 
       Statement sta = conn.createStatement(); 
       if (conn != null) { 
         DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData(); 
         System.out.println("Driver name: " + dm.getDriverName()); 
         System.out.println("Driver version: " + dm.getDriverVersion()); 
         System.out.println("Product name: " + dm.getDatabaseProductName()); 
         System.out.println("Product version: " + dm.getDatabaseProductVersion()); 
        } 
      } 
    } 

但是當我添加依賴JDBC和在創建服務運行相同的代碼,它給我的錯誤,如:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver 
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308) 
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:264) 
    at com.zcon.vmh.dao.implementations.AdvisoryBoxDaoImpl.connectionTest(AdvisoryBoxDaoImpl.java:131) 
    at com.zcon.vmh.services.implementation.AdvisoryBoxServiceImpl.connectionTest(AdvisoryBoxServiceImpl.java:19) 
    at com.zcon.vmh.controller.AdvisoryBoxController.connectionTest(AdvisoryBoxController.java:40) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 

我加入這個依賴在pom.xml中:

<dependency> 
      <groupId>com.microsoft.sqlserver</groupId> 
      <artifactId>sqljdbc4</artifactId> 
      <version>4.2</version> 
      <scope>test</scope> 
     </dependency> 

我應該做些什麼改變,以便在創建服務時能夠成功連接?

+0

https://stackoverflow.com/questions/19537396/missing-artifact-com-microsoft-sqlserversqljdbc4jar4-0/30111956#30111956 – soorapadman

+1

範圍是 '測試',依賴關係將不可見在正常運行時與應用程序一起打包或打包,而不是作爲測試。將其更改爲「運行時」。 – vempo

回答

1

你依賴的範圍是test,這意味着它不會在類路徑當應用程序正常運行(而不是測試)。更改範圍runtime,其中的.jar內殼體類會在運行時都可用,如果你在IDE中運行應用程序或使用Maven插件來包裝它。

+0

謝謝,它工作。 –

1

不幸的是微軟不作任何通過Maven倉庫中可用的這件神器。我建議你使用下面的依賴,以解決您的問題

微軟已經公佈了一些他們的司機到Maven中心:

<dependency> 
      <groupId>com.microsoft.sqlserver</groupId> 
      <artifactId>mssql-jdbc</artifactId> 
      <version>6.1.0.jre8</version> 
     </dependency> 
+0

這種依賴性顯然是從https://www.microsoft.com/en-us/download/details.aspx?id=11774下載,並且加入到私人Maven倉庫或使用MVN安裝本地安裝:安裝文件-Dfile = sqljdbc42.jar -DgroupId = com.microsoft.sqlserver -DartifactId = sqljdbc4 -Dversion = 4.2 -Dpackaging =罐子 – vempo

+0

@soorapadman,感謝您的建議。 –

1

如果你仍然面臨着行家的問題,您可以單獨下載SQLSERVER的jar文件,並在你的項目中添加它,並嘗試again.below運行是下載jar文件的路徑。

http://java2s.com/Code/Jar/s/Downloadsqlserverjdbcjar.htm

+0

謝謝你的時間Shrikant,用'運行時'替換'測試'實際上解決了這個問題。 –