微软自带的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();
- }
- }