使用 Java 连接 SQL Server 2005 Express
我的项目有问题。这让我发疯——也许你可以帮助我。 我有一台运行 SQL Server 2005 Express 的 PC,它运行良好。 在 Eclipse 中,我测试了以下代码来查询数据库:
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 );
}
它工作正常并运行查询。我从 Microsoft 下载了驱动程序并将 sqljdbc4.jar 添加到类路径中。
真正必须访问数据库的项目是另一个版本的Eclipse(Eclipse SDK 3.2)。它运行一个需要查询数据库的 Web 服务。 我将相同的 jar 添加到类路径中并执行相同的代码,即:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
抛出这个异常:
java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver 在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1438) 在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1284) 在 java.lang.Class.forName0(本机方法) 在 java.lang.Class.forName(来源未知) 在 com.banquito.Sucursales.HolaMundo.pruebaBD(HolaMundo.java:42) 在 com.banquito.Sucursales.SucursalesSOAPImpl.getConfigPaciente(SucursalesSOAPImpl.java:44) 在 com.banquito.Sucursales.SucursalesSOAPSkeleton.getConfigPaciente(SucursalesSOAPSkeleton.java:80) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(来源未知) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(来源未知) 在 java.lang.reflect.Method.invoke(来源未知) 在 org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397) 在 org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186) 在 org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323) 在 org.apache.axis.strategies.InitationStrategy.visit(InitationStrategy.java:32) 在 org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118) 在 org.apache.axis.SimpleChain.invoke(SimpleChain.java:83) 在 org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:453) 在 org.apache.axis.server.AxisServer.invoke(AxisServer.java:281) 在 org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 在 org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174) 在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879) 在 org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) 在 org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) 在 org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) 在 org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689) 在 java.lang.Thread.run(来源不明)
看来 Eclipse 无法解析导出。有人可以帮我吗? 谢谢
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:186)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是 Eclipse,请单击“配置构建路径”,然后将此 jar 添加到应用程序中,然后重试
if you are using eclipse click configure build path, and add this jar into tha application and try again
jar 文件似乎丢失,清理项目。如果它仍然存在,请尝试创建 com.microsoft.sqlserver.jdbc.SQLServerDriver 的实例或该 jar 中存在的任何其他类,您将得到答案。
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.
看起来您正在使用一些 servlet 容器来运行您的应用程序。因此,最好不要直接从应用程序创建与数据库的连接,而是在容器中创建数据源并从代码中使用它。
然后,您需要将带有 JDBC 驱动程序的 jar 放入容器的
lib
文件夹中。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.