Results 1 to 12 of 12
  1. #1
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Post Connector J with Eclipse and MySQL 5.0.x

    I'm trying to get MySQL and Eclipse with connector j to work. I've installed MySQL 5.0.x and it's all working well, but I don't know where to put the connector-J.xxx.jar file so I can start using it all. I've followed a few tutorials and set the jar file in the CLASSPATH etc. (set CLASSPATH=<PATH TO CJ>) and copied a copy of CJ into c:\<PATH to Java install>\lib\ext so it can be seen by the JVM. But I'm not sure if I need to do something special for/with eclipse.

    I'm using win32 XP.

    Java Code:
    public class Simi {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		try {
    			Class.forName("com.mysql.jdbc.Driver").newInstance();
    			System.out.println("Good to go!");
    		} catch (Exception E) {
    			System.out.println("JDBC Driver Error!");
    			
    		}
    
    	}
    
    }
    This is a test program to see if it's all working.....and, it's not:

    "JDBC Driver Error!".

    I'm not sure what to do next. Thanks in advance.
    Last edited by int80; 07-15-2008 at 11:04 PM.

  2. #2
    skaspersen is offline Member
    Join Date
    Jul 2008
    Posts
    31
    Rep Power
    0

    Default

    It needs to be in the ext directory of the jvm that the project is set to use.
    If you have JDK installed it needs to go to [JDKDIR]/jre/lib/ext.

    The other option is to add the mysql jar to your project libraries.

    HTH
    Stephen

  3. #3
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by skaspersen View Post
    It needs to be in the ext directory of the jvm that the project is set to use.
    If you have JDK installed it needs to go to [JDKDIR]/jre/lib/ext.

    The other option is to add the mysql jar to your project libraries.

    HTH
    Stephen
    Thanks alot, it works now, I added it as a library instead. Does that mean I don't need this line:

    Java Code:
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    As doesn't that just try to load from CLASSPATH?

  4. #4
    skaspersen is offline Member
    Join Date
    Jul 2008
    Posts
    31
    Rep Power
    0

    Default

    You still need that,

    Heres a generic class which is more or less what I use
    Java Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class MysqlConnectionProvider {
    	private static MysqlConnectionProvider INST;
    
    	private MysqlConnectionProvider() throws ClassNotFoundException {
    		Class.forName("com.mysql.jdbc.Driver");
    	}
    
    	public static Connection getNewConnection(String host, String database, String user, String password)
    			throws SQLException, ClassNotFoundException {
    		if (INST == null) {
    			INST = new MysqlConnectionProvider();
    		}
    		return INST.createConnection(host, database, user, password);
    	}
    
    	public Connection createConnection(String host, String database, String user, String password) throws SQLException {
    		String url = new String("jdbc:mysql://" + host + "/" + database);
    		return DriverManager.getConnection(url, user, password);
    	}
    
    }
    Note that i am returning a java.sql.Connection and not a com.mysql.jdbc.Connection, this helps if you ever need to change the Database provider.

    Also remember to close preparedstatements and resultsets after use to prevent memory leaks.

    eg
    Java Code:
    java.sql.PreparedStatement ps = null;
    java.sql.ResultSet rs = null;
    try{
    	ps = connection.prepare......
    	rs = ps.executeQuery();
    	//work with rs here
    } catch (SQLException s){
    	//handle error here
    } finally{
    	if (rs != null) {
    		try {
    			rs.close();
    		} catch (SQLException e) {
    		}
    	}
    	if (ps != null) {
    		try {
    			ps.close();
    		} catch (SQLException e) {
    		}
    	}
    }
    Stephen

  5. #5
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default

    Thanks alot! Will take a look at that. :D

  6. #6
    Emil Kesler is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default Eclipse 3.4 Ganymede & MySQL

    My PC runs Windows Vista. I installed Eclipse 3.4 Ganymede and would like to work with MySQL through the Eclipse. I already have the MySQL 5.1 installed. For working with it in the Eclipse, I have downloaded the MySQL Connector/J 5.1.6 and, by the installation docs, have to set the CLASSPATH environment variable. However, I don't see how to do that.
    On the other hand, I opened the "Database Development" perspective in the Eclipse, and tryed to create Connection through the "Data Source Explorer", following the wizard leading through the process. Unfortunately, I failed to get the correct result. Only I had "Ping failed" or "Couldn't connect to NewMySQL"

  7. #7
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Emil Kesler View Post
    My PC runs Windows Vista. I installed Eclipse 3.4 Ganymede and would like to work with MySQL through the Eclipse. I already have the MySQL 5.1 installed. For working with it in the Eclipse, I have downloaded the MySQL Connector/J 5.1.6 and, by the installation docs, have to set the CLASSPATH environment variable. However, I don't see how to do that.
    On the other hand, I opened the "Database Development" perspective in the Eclipse, and tryed to create Connection through the "Data Source Explorer", following the wizard leading through the process. Unfortunately, I failed to get the correct result. Only I had "Ping failed" or "Couldn't connect to NewMySQL"
    I didn't do any of that in the end. I just right clicked on the folder containing the package and classes, went to properties, goto Java Build Path, Add external Jars, and then added the j connector jar file. Before you do that, put j connector jar in the workspace file.

    I hope that helps.

  8. #8
    Emil Kesler is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default Eclipse 3.4 Ganymede & MySQL

    Thanks alot, int80.
    I found the solution (with your help!)
    The key issue was to put the downloaded directory "mysql-connector-java-5.1.6" which contains the "mysql-connector-java-5.1.6-bin.jar" into the directory "eclipse", and to rename the driver in eclipse. After that, the "Database Development" perspective got working as needed

  9. #9
    Emil Kesler is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default Loading text into MySQL table

    Hi,
    I have a text file to use it as INFILE for SQL command LOAD into a table. The table has fields of the type DATE. Some strings representing dates in the text are actuall NULLs and contain only "/ /". How can I nicely load this text into the table?
    P.S. The text originates from the DBF-file, as exported through FoxPro. I found only the "text delimited by ..." export option in FoxPro is in accordance with the MySQL options to import tables from the foreign formats.
    Thanks in advance

  10. #10
    kairamr is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

    Hi Kesler,
    Could you please show how to rename the driver in eclipse. Also, do I need to copy the entire directory in to the Eclipse directory ?.
    Thanks
    Raj Kairam

  11. #11
    Emil Kesler is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    Hi, kairamr
    Unfortunately, I haven't found the DB project where I made the described in my post. So, I'll answer you relying on my memory.
    The naming of the driver in Eclipse doesn't matter. My problem was that at the start of the project, when I tryed connection to mySQL, I used the Eclipse defaults about the driver, and Eclipse was not able to find the driver, because it was not in the right place. My attempt to use different drivers, places, etc. with the initial (default) driver name were unsuccessful until I changed the name having relocated the connector/driver in the native Eclipse directory (I mean "plugins"), Eclipse "swallowed" that up and start working. Later I found out Eclipse now has two driver. The initial one doesn't work, and the new one do works, despite of the name I have chosen for it. I didn't think much about the name. Sincerely hoping this will help you.

  12. #12
    ressurrected is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default eclipse and xampp/mysql connector/j

    Hi all,

    It took ages but I finally found the cause of my problem with eclipse on a win xp environment. After you D/L the mysql connector/j for java. Open your project in eclipse and go into project/properties. Select "Java build Path" and click on Libraries. You'll see a button called "Add External Jar's". Click on it to open a browsing dialog and navigate to the directory that you've extracted mysql connector/j into and select "mysql-connector-java-5.1.12-bin.jar" file.(That's the one I have anyway) and click "ok". It should be added to your project.

    The next step is the code:

    Mine is:
    import java.sql.DriverManager;
    import java.sql.Connection;
    import java.sql.SQLException;

    public class jdbc
    {
    public static void main(String[] args)
    {
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("Good to go");
    }
    catch (Exception ex)
    {
    System.err.println("Got an exception! ");
    System.err.println(ex.getMessage());
    }
    }
    }

    The above code is from another forum by whoever(thank's whoever you are), not class path, no environment variables, clean and simple.

    Regards

Similar Threads

  1. MySQL connector selection
    By Ms.Ranjan in forum JDBC
    Replies: 3
    Last Post: 06-23-2008, 06:27 PM
  2. Replies: 0
    Last Post: 12-19-2007, 12:47 PM
  3. Mysql Help
    By Sumendra Maharjan in forum JDBC
    Replies: 1
    Last Post: 08-08-2007, 02:19 AM
  4. JCA connector
    By bbq in forum JDBC
    Replies: 1
    Last Post: 07-05-2007, 05:31 AM
  5. JSP and MySQL
    By Ed in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-04-2007, 06:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •