zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    22:django 配置详解
    21:序列化django对象
    20:django中的安全问题
    19:django 分页
    HTML 标签(一)
    流程图学习绘制
    HTTP原理
    终端的颜色代码
    Python 进程 线程总结
    Python Select模型
  • 原文地址:https://www.cnblogs.com/chenfool/p/3634251.html
Copyright © 2011-2022 走看看