0

When I try to run the simple Java program below from the CentOS 7 terminal, I get a java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error at the line of code that says Class.forName("com.mysql.jdbc.Driver");. How can I resolve this and similar errors in the code below so that the simple program below will run without errors?

Here is what I have so far:

I navigate to /path/to/ and then type:

javac somepackage/TestJDBC.java
java somepackage.TestJDBC  

This results in the error described above. The full code of this simple program is:

package somepackage;
//STEP 1. Import required packages
import java.sql.*;

public class TestJDBC {
 // JDBC driver name and database URL
 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
 static final String DB_URL = "jdbc:mysql://localhost:3306/somedb?autoReconnect=true";

 //  Database credentials
 static final String USER = "usrname";
 static final String PASS = "pword";

 public static void main(String[] args) {
 Connection conn = null;
 Statement stmt = null;
 try{
    //STEP 2: Register JDBC driver
    Class.forName("com.mysql.jdbc.Driver");

    //STEP 3: Open a connection
    System.out.println("Connecting to database...");
    conn = DriverManager.getConnection(DB_URL,USER,PASS);

    //STEP 4: Execute a query
    System.out.println("Creating statement...");
    stmt = conn.createStatement();
    String sql;
    sql = "SELECT id, name FROM peeps";
    ResultSet rs = stmt.executeQuery(sql);

    //STEP 5: Extract data from result set
    while(rs.next()){
       //Retrieve by column name
       int id  = rs.getInt("id");
       String name = rs.getString("name");

       //Display values
       System.out.print("ID: " + id);
       System.out.println(", name: " + name);
    }
    //STEP 6: Clean-up environment
    rs.close();
    stmt.close();
    conn.close();
 }catch(SQLException se){
    //Handle errors for JDBC
    se.printStackTrace();
 }catch(Exception e){
    //Handle errors for Class.forName
    e.printStackTrace();
 }finally{
    //finally block used to close resources
    try{
       if(stmt!=null)
          stmt.close();
    }catch(SQLException se2){
    }// nothing we can do
    try{
       if(conn!=null)
          conn.close();
    }catch(SQLException se){
       se.printStackTrace();
    }//end finally try
 }//end try
 System.out.println("Goodbye!");
}//end main
}//end FirstExample  
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • Is the mysql driver jar on your classpath? – duckstep Feb 13 '15 at 02:03
  • @duckstep I have never done this before. I have lived in eclipse and compiled things to war files. So I do not know the answer to your question. I would like a solution that keeps things as simple as possible. – CodeMed Feb 13 '15 at 02:07

2 Answers2

2

You need the library - cannot simplify that away. Previously I'm guessing you used a web app server like JBoss that provided the library for you. Now you're outside the container.

First download the Connector/J jar (contains the JDBC driver class com.mysql.jdbc.Driver) from http://dev.mysql.com/downloads/connector/j/5.0.html.

Then with the jar file in the current working directory, invoke your program something like this:

java -cp mysql-connector-java-5.n.nn.bin.jar:. somepackage.TestJDBC

Also see this SO post: Including jars in classpath on commandline (javac or apt)

HTH

Community
  • 1
  • 1
chrisinmtown
  • 3,571
  • 3
  • 34
  • 43
1

Locate (or upload) the mysql driver jar to the server, then run your code with

java -classpath /path/to/mysql-connector-java-5.0.8-bin.jar:. somepackage.TestJDBC

Adjust the version to whatever you're using.

duckstep
  • 1,148
  • 8
  • 17
  • The same way you upload your .java source files to the server. Or, if you write the code using a terminal text editor, you can use the terminal to download the distribution package: `wget http://central.maven.org/maven2/mysql/mysql-connector-java/5.0.8/mysql-connector-java-5.0.8.jar`. Note the slight difference in file names: The file from the distribution package has `-bin` after the version number, the file from maven.org doesn't. – duckstep Feb 13 '15 at 02:34
  • I copy/pasted it from the Download (JAR) link from the [MySQL Java Connector » 5.0.8](http://mvnrepository.com/artifact/mysql/mysql-connector-java/5.0.8) page on mvnrepository.com. try if copying directly from there helps, the download works on this end. – duckstep Feb 13 '15 at 02:44
  • Never mind. I found it already installed with `find / -mount -name '*mysql-connector-java*' -print` – CodeMed Feb 13 '15 at 02:48