jdbc 连接 sqlserver 学习

使用sqljdbc.jar 连接sqlserver

下载网址:

http://www.drv5.cn/sfinfo/8228.html#softdown

package test_sql_server;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.microsoft.sqlserver.jdbc.*;


public class test_jdbc {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Test
    public void test() throws SQLException {
//        fail("Not yet implemented");
        
          Connection connect = null;
          Statement stmt = null;
          ResultSet rs = null;
          
          String hostName = "192.168.1.1";
          String userid = "sa";
          String password = "123456";
          
          String databaseName = "DataService_Flight";
          
          String m_Driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
          String m_Url = "jdbc:sqlserver://"+hostName+":1433;DatabaseName="+databaseName;
        
          try {
              Class.forName(m_Driver);
            }
            catch (ClassNotFoundException ex) {
              ex.printStackTrace();
            }
          
          connect = DriverManager.getConnection(m_Url, userid, password);
          
          stmt = connect.createStatement();
          
          String query_str = "select * from sys.tables";
          
          rs = stmt.executeQuery(query_str);
          
          
          int i = 0;
          while( rs.next() ){
              i++;
              if(i < 100 ){
                  System.out.println(rs.getString(1));
              }else{
                  break;
              }
          }
          
          connect.close();
        
        
    }

}

连接成功后,和一般的JDBC使用一致。

参考文章:http://www.singlex.net/2379.html

jdbc 连接 sqlserver 学习,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。