JDBC CLASSPATH Not Working

前端 未结 5 1557
遇见更好的自我
遇见更好的自我 2021-01-20 03:51

I\'m setting up a simple JDBC connection to my working MySQL database on my server. I\'m using the Connector-J provided by MySQL. According to their documentation, I\'m supp

相关标签:
5条回答
  • 2021-01-20 03:59

    Forget the CLASSPATH environment variable. It's a big joke. It's ignored by pretty much everything else than a low-level java com.example.Foo command. It's ignored when you add the -jar argument. It's ignored when you add the -cp or -classpath argument. It's ignored by IDE's and web/application servers. That environment variable was intented as a convenience for starters. But beyond that, it's useless.

    It boils down to that the JAR file has got to be placed in any of the existing/default paths of the runtime classpath of the Java application in question, or that at least the path to the JAR file is to be added to the runtime classpath of the Java application in question.

    Since you're talking about a server and considering the fact that the CLASSPATH environment variable doesn't work, I'll assume that it's actually a webserver/appserver such as Apache Tomcat. If that's indeed true, you've got to either drop the JAR file in Tomcat/lib folder (if you use the container managed DataSource approach for a fast connection pool), or in webapp's WEB-INF/lib folder (if you use the poor man's Class#forName() approach to load the driver).

    If it's not and it's actually a Java application which is to be executed as a JAR, then you've got to specify the classpath in MANIFEST.MF file of the JAR in question. But if it's also not that and it is a loose .class file, then you've got to specify the classpath in -cp or -classpath argument of java command. To save yourself from typing it again and again when executing it, just create a .sh file with the command.

    0 讨论(0)
  • 2021-01-20 04:10

    Try to do :

    Class.forName("com.mysql.jdbc.Driver"); 
    

    before getting your connection

    You might also want to get a more recent version of the mysql jdbc driver. 5.0.8 is pretty old.

    Take a look at JDBC Basics it will give you some tips.

    This tutorial might help you as well.

    0 讨论(0)
  • 2021-01-20 04:11

    For MAVENS project solution. You can directly modify the pom.xml file. Add the mysql-connector dependency to the pom.xml project file:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.40</version>
    </dependency>
    
    0 讨论(0)
  • 2021-01-20 04:21

    I know this is an old thread but it might help someone.

    I have succeeded connecting to SQLite3 on Windows 7 with the proper CLASSPATH

    I use the JDBC driver sqlite-jdbc-3.7.2.

    First I tried to copy the driver under the directory c:\Program Files\java\jre7\lib and then set the CLASSPATH variable, but that failed to recognize the line :

    Class.forName("org.sqlite.JDBC");
    

    I think that's due to the Windows bug to have spaces in a folder name like "Program Files".

    So I copied the JAR file under C:\jbmorla and set the CLASSPATH to:

    .;c:\jbmorla\sqlite-jdbc-3.7.2.jar
    

    Hope this helps

    0 讨论(0)
  • 2021-01-20 04:22

    You seem to be working on a linux box. Make sure that your terminal launches a login shell. For example the gnome-terminal application (probably what you are using if you are using a ubuntu or fedora box for development) has a checkbox under the profile preferences > Title and command menu called "run command as a login shell". Check that and make sure you also put this code in the ~/.bash_profile file at the end.

    export CLASSPATH=/path/mysql-connector-java-5.0.8-bin.jar:$CLASSPATH
    

    But i would argue against doing this and doing a simple launch script for your application. Something similar to this:

    #!/bin/bash
    
    Set CLASSPATH=CLASSPATH=/path/mysql-connector-java-5.0.8-bin.jar:$CLASSPATH
    
    java -cp "$CLASSPATH" <your.main.application.Class>
    

    make executable and use that to launch your application.

    0 讨论(0)
提交回复
热议问题