0

this is my code in Java and i am using Linux mint i have installed mysql in mu Linux OS and then i try to run that program, program run accurate but database did not change i do not know why ..

import java.sql.*;
import java.io.*;
public class InsertTest
{
public static void main(String[] args)
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the id");
int id=Integer.parseInt(br.readLine());
System.out.println("enter the name");
String name=br.readLine();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:testdb");
PreparedStatement stmt= con.prepareStatement("insert into student values(?,?)");
stmt.setInt(1,id);
stmt.setString(2,name);
stmt.executeUpdate();
System.out.println("record is inserted");
con.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}

output of the program is --|

enter the id
110
enter the name
joshi
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
  • 1
    Is this class on your classpath? –  May 06 '18 at 09:03
  • `Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");` tries to load the class `sun.jdbc.odbc.JdbcOdbcDriver` if it's in your classpath. If not, a `java.lang.ClassNotFoundException` will be thrown. Add the appropriate driver JAR to your classpath – Alexander May 06 '18 at 09:05
  • 1
    If you want MSSQL as tagged, dupe https://stackoverflow.com/questions/14229072/removal-of-jdbc-odbc-bridge-in-java-8 https://stackoverflow.com/questions/22984438/java-lang-classnotfoundexception-sun-jdbc-odbc-jdbcodbcdriver https://stackoverflow.com/questions/21955256/manipulating-an-access-database-from-java-without-odbc https://stackoverflow.com/questions/29356855/sun-jdbc-odbc-jdbcodbcdriver-not-working-with-jdk-1-8 . If you actually want MySQL, discard everything related to Odbc; use the MySQL 'connector-J' (driver) and a MySQL connectionstring. – dave_thompson_085 May 06 '18 at 10:51

1 Answers1

-1

You need to download the ojdbc7.jar (or similar depending on your jdk version) and add it to your project. Look up info on how to do that depending on your IDE

Khepu
  • 183
  • 2
  • 10
  • 1
    `ojdbc.jar` is the driver for _Oracle database_, not Odbc (and not MySQL either). The former Jdbc-Odbc-bridge was included in rt.jar through j7, and does not exist at all in j8+. – dave_thompson_085 May 06 '18 at 10:51