JAVA報錯:java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcodbcDrive


這個問題的原因是找不到類,下面的代碼是正常的,在JDK1.6版本是可以運行的,在JDK1.6版本后甲骨文公司已經取消ODBC橋連的方式,解決方法是換另一種驅動方式,當前Server SQL用驅動是com.microsoft.sqlserver.jdbc.SQLServerDriver,連接串是jdbc:sqlserver://localhost:1433;databaseName=數據庫名

 

 1 /**
 2  * 作者:白客C
 3  * 時間:2020年03月15日
 4  * 演示使用jdbc-odbc橋連方式操作數據庫
 5  * 1.配置數據源
 6  * 2.在程序中連接數據源
 7  */
 8 package com.beekc.www;
 9 import java.sql.*;
10 
11 public class Odbc {
12     public static void main(String[] args) {
13 
14         //2
15         Connection ct = null;
16         //3
17         Statement sm = null;
18         try {
19             //1.加載驅動
20             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
21             //2.得到連接[指定連接到哪個數據源,用戶名,密碼]
22             //如果配置數據源的時候,選擇Windons NT驗證就不需要填用戶名和密碼
23             //Connection ct = DriverManager.getConnection("jdbc:odbc:beekc");
24             ct = DriverManager.getConnection("jdbc:odbc:beekc", "sa", "admin..");
25 
26             //3.創建Statement或者PreparedStatement
27             //Statement用處:主要用於發送sql語句到數據庫
28             sm = ct.createStatement();
29 
30             //4.執行(crud、創建數據庫、備份數據庫、刪除數據...)
31             //1.添加一條數據到
32             //executeUpdate可以執行cud操作,放回int型
33             //int i = sm.executeUpdate("insert into stu values('000013','吳用','三國')");
34             //if (i == 1)
35             //{
36                 //System.out.println("添加成功");
37             //}else{
38                 //System.out.println("添加失敗");
39             //}
40             //2.查詢
41             //ResultSet結果集,可以把ResultSet理解成一個表行的結果集
42             ResultSet rs = sm.executeQuery("select * from stu");
43 
44             //rs指向結果集的第一行的前一行
45             //循環取出
46             while (rs.next())
47             {
48                 String str1 = rs.getString(1);
49                 String str2 = rs.getString(2);
50                 String str3 = rs.getString(3);
51                 System.out.println("sno:" + str1 + "sname:" + str2 + "address:" + str2 );
52             }
53         }catch (Exception e){
54             e.printStackTrace();
55         }finally {
56             //關閉資源
57             //關閉順序,后創建先關閉
58             try{
59                 if(sm != null)
60                 {
61                     sm.close();
62                 }
63                 if (ct != null)
64                 {
65                     ct.close();
66                 }
67             }catch (Exception e){
68                 e.printStackTrace();
69             }
70         }
71 
72     }
73 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM