1
import java.sql.*;

public class SQlConnection{
    public static void main(String args[]){
        try{
            //loading the driver
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:MyDB");
            Statement st=con.createStatement();
            String a = "";
            String q = "select level from myTable";
            ResultSet rs = st.executeQuery(q);
            while (rs.next()){
                // retrieving information from the 11th field of the table
                a = rs.getString(11);
                System.out.println(a);
            }
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            System.out.println(e.toString());
            System.out.println(e);
        }
    }
}

I am executing the above code and I am getting the following error can any one help me please?

sun.jdbc.odbc.JdbcOdbcDriver
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver

I am using: 1. Ms Sql Server 2012 Express Edition 2. Eclipse IDE for Java Developer Luna Service Release 2 (4.4.2) 3.Windows 10 Home Edition

Thanks in Advance

Ned
  • 23
  • 4
  • You don't assign the first line to anything, so why is the line here ? ^^ – azro Jul 30 '17 at 15:59
  • Sorry didn't get it? – Ned Jul 30 '17 at 16:02
  • `Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");` is useless – azro Jul 30 '17 at 16:04
  • Well, it is the driver, if i remove that line, the code wouldn't work – Ned Jul 30 '17 at 16:06
  • But the code doesn't work anyways? – Taylor Jul 30 '17 at 16:19
  • Use `e.printStackTrace();` instead of printing `e.getMessage()` and others. You lose important information otherwise. – Kayaman Jul 30 '17 at 16:30
  • ..and as said in the duplicate, there's no more JDBC-ODBC bridge, so get a driver (the regular SQL Server driver I'd guess? that supports SQL Express Edition and use that instead. – Kayaman Jul 30 '17 at 16:33
  • @azro the `Class.forName()` is used to load a class. You're right about it being useless, but wrong about why it's useless. – Kayaman Jul 30 '17 at 16:34
  • 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 SQlConnection.main(SQlConnection.java:7) – Ned Jul 30 '17 at 16:55
  • Problem Solved: First I needed to start SQL Server Browser in services.msc... Second URL was required ... conUrl = "jdbc:sqlserver://Ewa\\MSSQLSERVER2016; databaseName=carlsberg; integratedSecurity = True;"; – Ned Aug 07 '17 at 14:05

0 Answers0