myeclipse6.5下配置JNDI方法

数据库为sqlserver2005

 

第一步:

确定你的eclipse工作空间,我的为C:/workspace

在其下有一个testproject文件夹为工程件夹,在其下有一个root文件夹为存放JSP文件的文件夹。

 

第二步:将sqlserver2005的jdbc放到java的jre目录下

我的目录为:

c:/program files/java/jrexxxx/lib/ext/

 

第三步,在工作空间目录应该有一个叫做.metadata的文件夹,进到里面去,直到看到com.genuitec.eclipse.easie.tomcat.myeclipse/tomcat文件夹,进入conf目录下,打开context.xml,在其中加入:

    <Resource name="jdbc/myDataBase"
    type="javax.sql.DataSource" maxActive="50" maxIdle="10"
    maxWait="10000" username="sa" password="ilove148080152008"
    driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
    url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=cashier" />

 

第四步:

进入到工程文夹,即在我的c:/workspace/testproject/root/web-inf/

打开web.xml文件,<web-app>节之间加入:

<resource-ref>
        <description>cashier database connnection pool</description>
        <res-ref-name>jdbc/myDataBase</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
</resource-ref>

第五步:

在myeclipse中新建一个jsp文件测试,代码如下:

 

<% 
try{
      Context contxt=new InitialContext();
       Context envctx=(Context)contxt.lookup("java:comp/env");

DataSource ds=(DataSource)envctx.lookup("jdbc/myDataBase");
Connection con=ds.getConnection();
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select * from titems");
while(rs.next())
{
  out.println(rs.getObject("costitem")+"<br>");

}
rs.close();
cmd.close();
con.close();

}
catch(Exception ex)
{
  out.println(ex.getMessage());

}

 %>

 

 

第六步:restart tomcat in myeclipse,and browser the web ,good lucky!!! 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值