aspose file tools
The moose likes JDBC and Relational Databases and the fly likes JDBC with MySQL 5.2 in Eclipse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "JDBC with MySQL 5.2 in Eclipse" Watch "JDBC with MySQL 5.2 in Eclipse" New topic
Author

JDBC with MySQL 5.2 in Eclipse

Ranjeet Deshmukh
Ranch Hand

Joined: Jan 18, 2013
Posts: 35

Hi Everyone,
I'm newbie to JDBC, please can anyone tell me the Exact steps for executing following code in Eclipse, because I'm getting "ClssNotFound Exception".

import java.sql.*;

public class JDBCConn{
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}


~R@njeet.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1574
Ranjeet Deshmukh wrote:...because I'm getting "ClssNotFound Exception".


Where? Can you give the stack trace?

Also try to use codetags.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19849
    
  20

Did you add the MySQL driver JAR file to your class path?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ranjeet Deshmukh
Ranch Hand

Joined: Jan 18, 2013
Posts: 35

Thanks for the reply Rob and Akhilesh. Rob from next time I will follow the instructions.

I have added class path

CLASSPATH = "C:\Program Files\MySQL\Connector J 5.1.20.0\mysql-connector-java-5.1.20-bin.jar"

& I have confirmed in cmd echo %CLASSPATH% also.

Now the error I'm getting is :-

C:\Program Files\Java\jdk1.7.0_05\bin>java JDBCConn
Error: Could not find or load main class JDBCConn



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19849
    
  20

When you manually set the class path, the current directory (.) will no longer be a part of it. You need to add it as well:
CLASSPATH = .;"C:\Program Files\MySQL\Connector J 5.1.20.0\mysql-connector-java-5.1.20-bin.jar"
Ranjeet Deshmukh
Ranch Hand

Joined: Jan 18, 2013
Posts: 35

Thanks Rob, My problem is solved. Now I have got connectivity. Thanks a lot.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19849
    
  20

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC with MySQL 5.2 in Eclipse