najeer
najeer

Reputation: 37

jdbc connection using mysql

 import java.sql.*;

    public class MysqlConnect{

      public static void main(String[] args) {

        System.out.println("MySQL Connect Example.");

        Connection conn = null;

        String url = "jdbc:mysql://localhost:3306/";

        String dbName = "sint";

        String driver = "com.mysql.jdbc.Driver";

        String userName = "root"; 

        String password = "najeer";

        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();
        }

      }

    }

hai all i am try to this.. then

c:/>java MysqlConnect.java

C:\>java -cp .;\local\lib\mysql-connector-java-5.1.15-bin.jar MysqlConnect

MySQL Connect Example.

Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/SQLClientInf
oException

        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at com.mysql.jdbc.ConnectionImpl.<clinit>(ConnectionImpl.java:270)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:305)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at MysqlConnect.main(MysqlConnect.java:15)

this type error occur please help any one....

Upvotes: 4

Views: 4241

Answers (2)

Yasin
Yasin

Reputation: 2039

You have not compiled the file properly. You have used c:/>java MysqlConnect.java for compilation. Instead you should use c:/>javac MysqlConnect.java. Make sure if the class file is actually there before running the program.

Upvotes: 0

user591593
user591593

Reputation:

If you can include rt.jar in the classpath then the exception will go away.

Upvotes: 1

Related Questions