简体   繁体   中英

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!

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.

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.

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