Dmitry
Dmitry

Reputation: 3038

And Again... MySQL Embedded in Java App

There are a lot of questions: "How to start working with MySQL as an embedded database?", "How to use Connector/MXJ", etc. But there is no any useful information (neither tutorials!). I mean there is no detailed instructions how to do such things. Of course, there is a MySQL website, where is an article about using MysqldResource. Actually, I don't understand what it is.

Let's finish this lack of any restraint! Please, if you are experienced in this topic, give as full instruction as you can! (what to download, how to add jars(say, to eclipse), some code will be great...)

For example, the following code doesn't work - ClassNotFoundException- though I have added mysql-connector-mxj-gpl-5-0-11.jar and mysql-connector-mxj-gpl-5-0-11-bd-files.jar to the project classpath.

 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;


  public class DatabaseWorks {

public static void main(String[] args) {
    try {
        Class.forName("com.mysql.jdbc.Driver");

        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost", "root", "");
            Statement st = con.createStatement();
            String query = "SELECT VERSION();";
            ResultSet rs = st.executeQuery(query);
            rs.next();
            System.out.println("success!!!! " + rs.getString(1));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Upvotes: 2

Views: 2853

Answers (2)

Stephen C
Stephen C

Reputation: 718708

For example, the following code doesn't work - ClassNotFoundException- though I have added mysql-connector-mxj-gpl-5-0-11.jar and mysql-connector-mxj-gpl-5-0-11-bd-files.jar to the project classpath.

This is a basic Java problem. Here's what I'd do to solve it:

  1. Examine the stacktrace to find out which class is missing.
  2. Use jar tvf ... to list the contents of JAR file(s) you think the missing class should be in. Is it there? Is the name / package correct?
  3. If it is there, you've got the application's runtime classpath wrong.
  4. If it is not there, you are missing a JAR file. Go back to the documentation and read it again.

(If you showed us the stacktrace, and told us how you are building and launching your code, perhaps we could be a bit more specific ...)

Upvotes: 1

Related Questions