`

连接sql server2005测试

阅读更多
import java.sql.*;


 public class testSqlserver {

   public static void main(String[] args) {

      // Create a variable for the connection string.
      String connectionUrl = "jdbc:sqlserver://localhost:50689;" +
         "databaseName=blogdb;user=sa;password=admin";

      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;

      try {
         // Establish the connection.
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         con = DriverManager.getConnection(connectionUrl);

         // Create and execute an SQL statement that returns some data.
         String SQL = "SELECT TOP 5 * FROM Class";
         stmt = con.createStatement();
         rs = stmt.executeQuery(SQL);

         // Iterate through the data in the result set and display it.
         while (rs.next()) {
            System.out.println(rs.getString(3));
         }
      }

      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
         if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (stmt != null) try { stmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
      }
   }
}

   今天测试了一下java连接sql server 2005,记录一下步骤:
   1:到微软官网下载了jdbc的驱动,添加到应用程序的jar包中
   2:就是上面的那段代码的测试,这是微软给的例子,原本端口号写的是:1433 可我的端口是50689(是在sql server 2005的网络配置管理器点tcp/ip的ip地址的最后一项:IPAll的那个端口值),汗 这个东西改了很久...
   在解决问题的过程中 发现还是谷歌专业点....同样的关键字 谷歌搜到的第一位就能解决问题 而百度的都是一些无关痛痒的东西.....汗...
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics