要点:
数据库URL:jdbc:jtds:sqlserver://localhost:1433;DatabaseName=bid 驱动类:net.sourceforge.jtds.jdbc.Driver??
? ----------------------------------- 对比: //microsoft
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; //jtds Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance(); String url = "jdbc:jtds:sqlserver://localhost:1433;DatabaseName=pubs"; //String url = "jdbc:jtds:sqlserver://localhost:1433/pubs"; String user = "sa"; String password = "dog"; Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sql = "select top 10 * from titles"; //titles为表名; ResultSet rs = stmt.executeQuery(sql);
? --------------------------------- 示例: JAVA使用JTDS连接SQL2000问题 一般有以下几个方面: 1.WINDOWS防火墙屏蔽了1433端口 2.检查SQL2000是否使用的是1433端口 3.检查SQL2000是否升级到SP3以上版本(基本都是这个原因) 以下是使用JTDS连接SQL2000的代码段 连接SQL2000下的TheTest库 -------------------------- public static Connection getConnection(){
String dbDriver = "net.sourceforge.jtds.jdbc.Driver"; String strConnection = "jdbc:jtds:sqlserver://localhost:1433/TheTest"; String user = "sa"; String password = "sa"; Connection conn = null; try{ //定义连接驱动 Class.forName(dbDriver); } catch(java.lang.ClassNotFoundException e){ System.err.println("DBconnection():"+e.getMessage()); } //--------连接SQL数据库------------------ try { conn = DriverManager.getConnection(strConnection,user,password); } catch(SQLException ex) { System.err.println("aq.executeQuery:"+ex.getMessage()); } return conn; } -----------------------以下为关闭连接-------------------------- public static void closeConnection(PreparedStatement ps,Connection conn,ResultSet rs){ try{ if (rs!=null){ rs.close(); } if (ps!=null){ ps.close(); } if (conn!=null){ conn.close(); } } catch(SQLException sqlerror){ sqlerror.printStackTrace(); } } public static void closeConnection(PreparedStatement ps,Connection conn){ try{ if (ps!=null){ ps.close(); } if (conn!=null){ conn.close(); } } catch(SQLException sqlerror){ sqlerror.printStackTrace(); } } public static void closeConnection(Connection conn){ try{ if (conn!=null){ conn.close(); } } catch(SQLException sqlerror){ sqlerror.printStackTrace(); } }