Tag Archives: java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcodbcDrive

[Solved] JAVA:java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcodbcDrive

The reason for this problem is that the class can not be found. The following code is normal. It can run in JDK1.6. Oracle has canceled the ODBC bridge after JDK1.6. The solution is to change another driver mode. The current SQL Server driver is com.microsoft.sqlserver.jdbc.sqlserverdriver, and the connection string is jdbc:sqlserver :// localhost:1433; Databasename = database name

/**
 * Author:C
 * Date: 03/15/2020
 * Demonstrate the use of jdbc-odbc bridge to operate the database
 * 1. Configure the data source
 * 2. Connect to the data source in the program
 */
package com.beekc.www;
import java.sql.*;

public class Odbc {
    public static void main(String[] args) {

        //2
        Connection ct = null;
        //3
        Statement sm = null;
        try {
            //1. Load the driver
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            //2. Get the connection [specify which data source to connect to, username, password]
            //If you choose Windons NT authentication when configuring the data source, you don't need to fill in the username and password
            //Connection ct = DriverManager.getConnection("jdbc:odbc:beekc");
            ct = DriverManager.getConnection("jdbc:odbc:beekc", "sa", "admin...") ;

            //3. Create a Statement or PreparedStatement
            //Statement use: mainly used to send sql statements to the database
            sm = ct.createStatement();

            //4. Execute (crud, create database, backup database, delete data...)
            //1. Add a data to
            //executeUpdate can perform cud operation, put back int type
            //int i = sm.executeUpdate("insert into stu values('000013','Wu with','Three Kingdoms')");
            //if (i == 1)
            //{
                //System.out.println("Added successfully");
            ///}else{
                //System.out.println("Failed to add");
            //}
            //2. Query
            //ResultSet result set, you can understand the ResultSet as a result set of table rows
            ResultSet rs = sm.executeQuery("select * from stu");

            //rs points to the previous row of the first row of the result set
            //loop out
            while (rs.next())
            {
                String str1 = rs.getString(1);
                String str2 = rs.getString(2);
                String str3 = rs.getString(3);
                System.out.println("sno:" + str1 + "sname:" + str2 + "address:" + str2 );
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //Close resources
            //Close order, create first, close first
            try{
                if(sm != null)
                {
                    sm.close();
                }
                if (ct != null)
                {
                    ct.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}