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 try this .. then

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

MySQL connection 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) 

there is an error like help someone ....

+4
source share
2 answers

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

+1
source

You did not compile the file properly. You used c:/>java MysqlConnect.java to compile. Instead, you should use c:/>javac MysqlConnect.java . Make sure that the class file actually exists before running the program.

0
source

Source: https://habr.com/ru/post/1347232/


All Articles