数据库为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!!!