|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-15-2008, 11:46 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 27
|
|
|
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.
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-16-2008 at 12:04 AM.
|
|

07-16-2008, 05:37 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 31
|
|
|
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
|
|

07-16-2008, 06:00 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 27
|
|
Originally Posted by skaspersen
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:
Class.forName("com.mysql.jdbc.Driver").newInstance();
As doesn't that just try to load from CLASSPATH?
|
|

07-16-2008, 06:42 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 31
|
|
You still need that,
Heres a generic class which is more or less what I use
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.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
|
|

07-16-2008, 08:01 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 27
|
|
Thanks alot! Will take a look at that. 
|
|

09-08-2008, 03:13 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 4
|
|
|
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"
|
|

09-08-2008, 12:05 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 27
|
|
Originally Posted by Emil Kesler
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.
|
|

09-09-2008, 03:46 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 4
|
|
|
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
|
|

09-09-2008, 10:52 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 4
|
|
|
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-31-2008, 11:37 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 2
|
|
|
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-01-2008, 02:38 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 4
|
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|