使用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