Insert into database | SoloLearn: Learn to code for FREE!


Insert into database

Hi I use mysql database and my code is: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn=DriverManager.getConnection("jdbc:odbc:bookdsn"); Statement st=cn.createStatement(); String query="Insert into book (Bname,Bsubject,Bshabok,Bwriter)values('H','D','1234','AA')"; st.executeUpdate(query); st.close(); cn.close(); It compile without error but got this runtime error: Exception in thread "main" java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver at Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at BookDatabase.main( What is reason?

7/12/2019 10:30:19 AM


7 Answers

New Answer


make sure you have the mysql driver file then set your classpath when running the program.


i thougt mysqldriver is com.mysql.jdbc.Driver also make sure the library are in the classpath


I got the same error with: com.mysql.jdbc.Driver


Test connection on odbc data sources is sucsseful


the problem is jdbcodbc driver is no longer part of java (both jdk and jre) they even encourage to use the driver from the database vendor


after download and install driver : i got above error again

0 add that to classpath then change the connection url to jdbc:mysql://localhost:port/databasename