Insert into database | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

12th Jul 2019, 10:30 AM
Hadi
Hadi - avatar
7 Answers
+ 2
make sure you have the mysql driver file then set your classpath when running the program.
12th Jul 2019, 10:53 AM
Taste
Taste - avatar
+ 1
i thougt mysqldriver is com.mysql.jdbc.Driver also make sure the library are in the classpath
12th Jul 2019, 10:37 AM
Taste
Taste - avatar
0
I got the same error with: com.mysql.jdbc.Driver
12th Jul 2019, 10:50 AM
Hadi
Hadi - avatar
0
Test connection on odbc data sources is sucsseful
12th Jul 2019, 11:14 AM
Hadi
Hadi - avatar
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
12th Jul 2019, 11:20 AM
Taste
Taste - avatar
0
after download and install driver : https://dev.mysql.com/downloads/connector/odbc/ i got above error again
13th Jul 2019, 11:33 AM
Hadi
Hadi - avatar
0
https://dev.mysql.com/downloads/connector/j/ add that to classpath then change the connection url to jdbc:mysql://localhost:port/databasename
13th Jul 2019, 11:39 AM
Taste
Taste - avatar