Java – JDBC connection using MySQL

import java.sql.*;
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();
        }

      }

    }

So I tried 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 of error occurred. Please help anyone

Solution

If you can include rt.jar in your classpath, the exception will disappear

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>