这篇文章给大家介绍如何解决微软自带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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。