0

I just created a Class Jdbc when i tried to run an error is shown.

Exception in thread "main" java.lang.NoClassDefFoundError: Jdbc

Here's the code

import java.sql.*;
public class Jdbc {
    public static void main(String [] args)
    {
        try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ims","","");
        Statement st=con.createStatement();
        DatabaseMetaData meta=con.getMetaData();
        ResultSet r=meta.getTables(null,null,"%",null);
        String tableNames="";
        while(r.next()){
        tableNames=r.getString(3);
        System.out.println(tableNames);
        }
        }catch (Exception e){}}}
7
  • no need to call .newInstance while loading the class. And set the classpath properly to mysql Driver. Commented Mar 25, 2013 at 7:07
  • Did you compile the code with javac before trying to run it? This is a generic error unrelated to jdbc. Commented Mar 25, 2013 at 7:08
  • then i have to specify return value Commented Mar 25, 2013 at 7:09
  • See this stackoverflow.com/questions/11380681/… Commented Mar 25, 2013 at 7:09
  • ya joni its compiled perfectly but not running Commented Mar 25, 2013 at 7:09

4 Answers 4

0

The issue is not with mysql jar but your own class "Jdbc" is not on classpath, add it to classpath.

If you are running from console add .; to classpath.

Sign up to request clarification or add additional context in comments.

Comments

0

Its most likely that your program is not able to find the class
Place the mysql jdbc driver mysql-connector-java-5.x.x.jar in classpath and then check.

Comments

0

The 2 quick fixes would be either

1) Pass the classspath to the execution java -classpath mysql.jar

See

JAVA classpath pass file as command line argument

2) Put the mysql jar in your JRE's extdirectory. Details.

You probably want to put your class in a package since you're using the default package. Also an IDE such as Eclipse could help you for convienience. You don't tell us your execution environment or build path and your result may depend on if you're running the program from the command line, from a runnable jar or from an IDE.

I use the Eclipse Juno IDE so I don't need to worry about setting my classpath. For Ubuntu and MS-Windows, Eclipse IDE is a development environment you might want to use.

2 Comments

i just use text editor only for jdbc demo
@Gaurav Ok, to run your program, first make sure you actually have the jar from mysql or you can get it from the link listed here.
0

!. Make sure that the jar is in your class path
2. If not then download it from here

1 Comment

i got the solution i just add class file path in my class path nor problem with connector thanks every one