您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何解決微軟自帶JDBC的resultset缺陷,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
微軟自帶的3個驅動包分別為:msbase.jar,mssqlserver.jar,msutil.jar
微軟JDBC resultset驅動的缺點:
(1)如果采用jdbc-odbc驅動,那么就必須按照查詢順序來一次讀取(不論有沒有image或text類型)
(2)如果采用微軟提供的ms sql server jdbc driver,如果查詢語句中,不存在image或text類型字段,那么可以按照無序獲取
(3)如果采用微軟提供的ms sql server jdbc driver,如果查詢語句中,存在image或text類型字段,那么就必須按照順序讀取,否則就會報告Driver]ResultSet can not re-read row data for column之類的錯誤
測試方法和代碼如下:
數據庫表:TestTable
表字段及類型:
guid char(38)
title varchar(100)
content Text
username varchar(20)
JDBC resultset代碼
public static void main(String[] args) { // TODO Auto-generated method stub String url = "jdbc:microsoft:sqlserver://192.168.5.20:1435;DatabaseName=emos2.2"; String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, "sa", "1234"); if (conn.isClosed() == true) { System.out.println("SomeThing goes wrong "); } PreparedStatement st = conn.prepareStatement("select * from TestTable"); ResultSet rs = st.executeQuery(); while(rs.next()){ System.out.println(rs.getString("guid")); System.out.println(rs.getString("username")); System.out.println(rs.getBinaryStream("content")); } } catch (ClassNotFoundException e) { System.out.println("SomeThing wrong "); System.out.println(e); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub String url = "jdbc:microsoft:sqlserver://192.168.5.20:1435;DatabaseName=emos2.2"; String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, "sa", "1234"); if (conn.isClosed() == true) { System.out.println("SomeThing goes wrong "); } PreparedStatement st = conn.prepareStatement("select * from TestTable"); ResultSet rs = st.executeQuery(); while(rs.next()){ System.out.println(rs.getString("guid")); System.out.println(rs.getString("username")); System.out.println(rs.getBinaryStream("content")); } } catch (ClassNotFoundException e) { System.out.println("SomeThing wrong "); System.out.println(e); } catch (SQLException e) { e.printStackTrace(); } } 而第三方的驅動jtds1.2很好的解決了這些bug Java代碼 public static void main(String[] args) { String url = "jdbc:jtds:sqlserver://192.168.5.20:1435/emos2.2"; String driver = "net.sourceforge.jtds.jdbc.Driver"; try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, "sa", "1234"); if (conn.isClosed() == true) { System.out.println("SomeThing goes wrong "); } PreparedStatement st = conn.prepareStatement("select * from TestTable"); ResultSet rs = st.executeQuery(); while(rs.next()){ System.out.println(rs.getString("guid")); System.out.println(rs.getString("username")); System.out.println(rs.getBinaryStream("content")); } } catch (ClassNotFoundException e) { System.out.println("SomeThing wrong "); System.out.println(e); } catch (SQLException e) { e.printStackTrace(); } }
關于如何解決微軟自帶JDBC的resultset缺陷就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。