Tomcat链接MSSQL2005

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

【所需�n案or工具】
Microsoft SQL Server 2005
�自行��洌�亦可使用Express版本�y�看看!^^
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
下�d路�剑� http://archive.apache.org/dist/tomcat/tomcat-6/

【�O定方式】
一、Microsoft SQL Server 2005 JDBC Driver
下�d解�和赆幔��放置至你��人喜好的路��
微$的使用�f明�e是建�h放置於C:\Program Files底下
因此,小弟就把解�和赆岬哪夸�放置於 C:\Program Files\Microsoft_SQL_Server_2005_JDBC_Driver
要把驱动放在tomcat的目录下。
PS:
1.此目�是要�O定在�h境���CLASSPATH�e的,所以,��_�J後勿在�S便更��
2.下�d下�淼�嚎s包�e,在help目�有�f明文件,可自行�⒖佳e面的�O定

二、Tomcat 6.0.14
相�P�O定方式,��⒖夹〉艿牧硪��帖!�x�x!^^
http://www.javaworld.com.tw/jute/post/view?bid=9&id=227419&sty=2

三、�h境��翟O定
��得��Q:JAVA_HOME
��抵担�C:\Program Files\Java\jdk1.6.0_06

��得��Q:PATH
��抵担�*;%JAVA_HOME%\bin

��得��Q:CLASSPATH
��抵担�.;C:\Program Files\Microsoft_SQL_Server_2005_JDBC_Driver\sqljdbc_1.2\cht\sqljdbc.jar

四、�0搁_�l�r,相�P文件�O定
(1)server.xml�O定�热� (�置放於<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=�Y料�烀��Q;" />
            </Context>


(2)web.xml�O定�热�
            
<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�B�程式部份
  
//�Y料�爝B�Y�O定 - 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();
            //�Y料�爝B�Y�O定 - end
            


五、SQL Server 2005相�P�O定
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了

你可能感兴趣的:(tomcat,数据库,职场,休闲,mssql2005)