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

0

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 java.net.URLClassLoader.findClass(Unknown 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(BookDatabase.java:6) What is reason?

7/12/2019 10:30:19 AM

Hadi

7 Answers

New Answer

+2

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

+1

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

0

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

0

Test connection on odbc data sources is sucsseful

0

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 https://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/bridge.html

0

after download and install driver : https://dev.mysql.com/downloads/connector/odbc/ i got above error again

0

https://dev.mysql.com/downloads/connector/j/ add that to classpath then change the connection url to jdbc:mysql://localhost:port/databasename