java – 使用mysql的jdbc连接

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/;Stri...

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

      }

    }

所有
我试着这个..然后

c:/>java MysqlConnect.java

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

MySQL Connect示例.

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)

发生此类型错误
请帮助任何人….

解决方法:

如果你可以在类路径中包含rt.jar,那么异常就会消失.

本文标题为:java – 使用mysql的jdbc连接

基础教程推荐