简体   繁体   中英

Connect with SQL Server 2005 Express with Java

I have problem with my project. It's making me crazy - maybe you can help me. I have a PC running SQL server 2005 Express and it works nicely. In Eclipse I tested the following code to query the database:

String user= "sa";
String pass= "root";
Statement smt;
ResultSet rs = null;
System.out.println("2");

try {
  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 2005 version
}catch (Exception e){}

try {
  System.out.println("3");
  Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=master",user,pass);
  smt = con.createStatement();
  smt.executeQuery("SELECT * from test");
  rs = smt.getResultSet();

  while (rs.next()) {
    printentry(rs);
  }

  con.close()
  System.out.println("5");
} catch ( SQLException excepcionSql) {
  JOptionPane.showMessageDialog( null, excepcionSql.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
}

And it works fine and runs the query. I downloaded the driver from Microsoft and added the sqljdbc4.jar to the class path.

The project that must really access the database is another version of Eclipse (Eclipse SDK 3.2). It runs a web service which need to make queries to the database. I added the same jar to the class path and executing the same code, the line:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

throws this exception:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1438) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1284) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at com.banquito.Sucursales.HolaMundo.pruebaBD(HolaMundo.java:42) at com.banquito.Sucursales.SucursalesSOAPImpl.getConfigPaciente(SucursalesSOAPImpl.java:44) at com.banquito.Sucursales.SucursalesSOAPSkeleton.getConfigPaciente(SucursalesSOAPSkeleton.java:80) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397) at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:1 86) at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323) at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32) at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118) at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83) at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:453) at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281) at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org .apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689) at java.lang.Thread.run(Unknown Source)

Is seems Eclipse can't resolve the export. Can somebody help me with that? Thanks

如果您使用的是eclipse,请单击“配置构建路径”,然后将此jar添加到tha应用程序中,然后重试

It looks like you are using some servlet container to run your app. So it better not to create connections to database directly from your app but create datasource in container and use it from your code.

Then you'll need to put jar with JDBC driver into lib folder of your container.

jar file seems to be missing, clean the project. If it still there try to create instance of com.microsoft.sqlserver.jdbc.SQLServerDriver or any other class present in that jar, you'll get the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM