当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - Java jdbc classpath on linux server

问题描述:

I have a problem with the jdbc classpath in Java. I am using Maven, but jdbc is not handable within maven, so I created a "lib" folder with the mysql-connector.jar und added this jar to the build path of the project.

If I run it local in eclipse it is all working fine, but when I put the project on my server and try to run it via command line it prints out this error:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/eventlist

at java.sql.DriverManager.getConnection(DriverManager.java:689)

at java.sql.DriverManager.getConnection(DriverManager.java:247)

at WriteData.writeEventDataToDatabase(WriteData.java:24)

at Main.main(Main.java:6)

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:497)

at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)

at java.lang.Thread.run(Thread.java:745)

I am using Java 1.8.0_60 and Maven 3.0.5 on CentOS 6. I hope you can tell me how the add the jdbc file to the classpath.

thanks!

网友答案:

You need to do two things:

  1. Execute mvn:install install-file to get the JAR into a repository.
  2. Modify your project pom.xml to ask the repository for that JAR.

Here's an example of the mvn:install command:

call mvn install:install-file -Dfile=sqljdbc.jar -DgroupId=microsoft -DartifactId=sqljdbc -Dversion=4 -Dpackaging=jar

Here's the pom.xml entry that calls for it in my project:

<dependency>
    <groupId>microsoft</groupId>
    <artifactId>sqljdbc</artifactId>
    <version>4</version>
</dependency>

You'll do the same thing for every JAR your project needs that is not available in a public repository.

If I execute mvn:install on my local machine, I see the JAR installed in my local .m2 repository. I don't have access to a private, shared repository. If I want my local machine and an integrated build facility to both have access to that JAR I'll have to get it into a private, shared repository that both can see.

网友答案:

Driver should be in server too. For example, if you are using tomcat on your server then driver must be in lib folder of tomcat.

分享给朋友:
您可能感兴趣的文章:
随机阅读: