zoukankan      html  css  js  c++  java
  • Tomcat 6 连接 MS SQL 2005

    http://www.javaworld.com.tw/jute/post/view?bid=9&id=232409&sty=3

    【環境】
    OS:Windows Server 2003 (x86)
    IDE:Eclipse 3.3 Europa
    Web Content:Tomcat 6.0.14
    DataBase:Microsoft SQL Server 2005

    【所需檔案or工具】
    Microsoft SQL Server 2005
    請自行準備,亦可使用Express版本測試看看!^^
    http://www.microsoft.com/downloads/details.aspx?familyid=220549B5-0B07-4448-8848-DCC397514B41&displaylang=zh-tw

    Microsoft SQL Server 2005 JDBC Driver
    http://www.microsoft.com/downloads/details.aspx?FamilyId=C47053EB-3B64-4794-950D-81E1EC91C1BA&displaylang=en

    Tomcat 6.0.14
    下載路徑:http://archive.apache.org/dist/tomcat/tomcat-6/

    【設定方式】
    一、Microsoft SQL Server 2005 JDBC Driver
    下載解壓完後,請放置至你個人喜好的路徑
    微$的使用說明裡是建議放置於C:\Program Files底下
    因此,小弟就把解壓完後的目錄放置於 C:\Program Files\Microsoft_SQL_Server_2005_JDBC_Driver

    PS:
    1.此目錄是要設定在環境變數CLASSPATH裡的,所以,請確認後勿在隨便更動
    2.下載下來的壓縮包裡,在help目錄有說明文件,可自行參考裡面的設定


    二、Tomcat 6.0.14
    相關設定方式,請參考小弟的另一個帖!謝謝!^^
    http://www.javaworld.com.tw/jute/post/view?bid=9&id=227419&sty=2

    三、環境變數設定
    變數名稱:JAVA_HOME
    變數值:C:\Program Files\Java\jdk1.6.0_06

    變數名稱:PATH
    變數值:*;%JAVA_HOME%\bin

    變數名稱:CLASSPATH
    變數值:.;C:\Program Files\Microsoft_SQL_Server_2005_JDBC_Driver\sqljdbc_1.2\cht\sqljdbc.jar

    四、專案開發時,相關文件設定
    (1)server.xml設定內容 (請置放於<host></host>標籤內)

     
    <Context docBase="CallCenter" path="/CallCenter" reloadable="true" source="org.eclipse.jst.jee.server:CallCenter">
                <Resource
                name="jdbc/MsSQLDB"
                driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
                maxActive="100"
                maxIdle="30"
                maxWait="10000"
                username="sa"
                password="xxxxxx"
                type="javax.sql.DataSource"
                url="jdbc:sqlserver://localhost:1433;database=資料庫名稱;" />
                </Context>


    (2)web.xml設定內容
    
                
    <resource-ref>
                <description>JNDI JDBC DataSource of uopint</description>
                <res-ref-name>jdbc/MsSQLDB</res-ref-name>
                <res-type>javax.sql.DataSource</res-type>
                <res-auth>Container</res-auth>
                <res-sharing-scope>Shareable</res-sharing-scope>
                </resource-ref>
                


    (3)servlet連線程式部份
      
    //資料庫連結設定 - start
                Connection con = null;
                PreparedStatement stmt = null;
                Context initContext = new InitialContext();
                Context envContext = (Context)initContext.lookup("java:/comp/env");
                DataSource ds = (DataSource)envContext.lookup("jdbc/MsSQLDB");
                con = ds.getConnection();
                //資料庫連結設定 - end
                


    五、SQL Server 2005相關設定
    http://denistek.blogspot.com/2008/07/configure-tomcat-6-datasource-using-sql.html
    This is a step-by-step instructions on how to configure Tomcat 6 DataSource using Sql Server 2005. The installation of Tomcat and SqlServer is not covered.
    1. Verify that you can login to the SQL Server using 'SQL Server Authentication'. You may wish to change the 'Server Authentication mode' to 'SQL Server and Windows Authentication mode'. You may also wish to check that the particular user's status of Login is 'Enabled'.
    2. Verify that 'Local and remote connections' is enabled.
    Go to Microsoft SQL Server 2005>Configuration Tools>SQL Server Surface Area Configuration(SQL Server 外围应用配置器)>Remote Connections: Enable TCP/IP
    3. Restart the database

    四. Write a testing JSP page like this:
    <%@   page   contentType="text/html;charset=UTF-8" %>  
    <%@   page   import="java.sql.*" %>
    <%@   page   import="javax.sql.*" %>
    <%@   page   import="javax.naming.*" %>
    <HTML>
    <HEAD>
    <TITLE>JSP example</TITLE>
    </HEAD>
    <BODY>
      <h1>Hello,test JNDI !  </h1>
      <%
        Context ctx = new InitialContext(); 
        Context envctx =  (Context) ctx.lookup("java:comp/env");
        DataSource ds =  (DataSource) envctx.lookup("jdbc/MsSQLDB"); 
        Connection  conn=ds.getConnection();  
        Statement  st=conn.createStatement();
        String    sql="select * from status"; 
        ResultSet    rs=st.executeQuery(sql);
        while(rs.next())   {
      %>  
      ID:<%=rs.getInt(1) %> 
           Value:<%=rs.getString(2) %>
           <br>
      <%
       }
      %>  
      Here is just JNDI datasource SQL Server 2005 + tomcat example
      <%
       rs.close();
       st.close(); 
       conn.close();  
      %>
    </BODY>
    </HTML>
    按照上面的设置,就可以用 Tomcat 6 连接池 连接到 MS SQL 2005了

  • 相关阅读:
    python 获取qq群成员列表数据
    selenium定位伪类元素::before
    flask 重写HTTPException类并自定义异常信息
    flask 工厂模式与celery结合
    重写flask_sqlalchemy的BaseQuery下面的filter_by方法
    Pytest系列
    Pytest系列- 多重校验插件之pytest-assume的详细使用
    mysql 主主+ Keepalived 高可用
    redis 主从配置,主从切换
    FastDFS 学习笔记
  • 原文地址:https://www.cnblogs.com/flyfish/p/1349449.html
Copyright © 2011-2022 走看看