MyEclipse的javabean
2010年11月30日
MyEclipse配置数据源连接池2010-03-07 20:14转自http://blog.csdn.net/olinor/archive/2009/11/26/4873772.aspx
软件版本myeclispe7.0,自带tomcat6.0.13。
jdbc:mysql-connector-java-3.1.7-bin.jar。
第一步:建立工程。
在Myeclipse中file->new->web project。
因为在测试数据源(jsp)时用到了标签库,所以可以在这里选上jsdl支持,当然也可以在工程建好后右键工程文件夹->myeclipse->add JSTL
Libraries…实现同样的功能。
第二步:导入jdbc的jar包。
要直接将mysql-connector-java-3.1.7-bin.jar复制到“工程文件夹/WebRoot/WEB-INF/lib”文件夹下。这时,myeclipse会自动生成一个
Referenced Libraries,不用管。
第三步:建立context.xml文件。
在“工程文件夹/WebRoot/META-INF”下,新建context.xml文件。文件内容如下:
解释:
name="jdbc/mysql" //连接名,jndi中使用。具在JSP中用调用,servlet用 DataSource ds
= (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");调用。这里是tomcat的格式,不同的服务器可能有所不同。
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root" //mysql的用户名
password="" //mysql的用户密码,我这里是空
driverClassName="com.mysql.jdbc.Driver" //驱动类名,一般确定
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true" //javatest是mysql中要使用的数据库名
第四步:修改web.xml文件。
在web.xml文件中添加以下内容:
DB Connection
jdbc/mysql
javax.sql.DataSource
Container
这部分内容一般是确定的。
OK,现在可以测试数据源是否好了。下面是JSP测试文件:
">
My JSP 'MyJsp.jsp' starting page
-->
select id, username, password from user
Results
Foo ${row.username}
Bar ${row.password}
当然,如果你用servlet测试数据源也是可以的,下面是一个servlet测试例子:
package fx;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class DsTest extends HttpServlet
{
/**
* Constructor of the object.
*/
public DsTest()
{
super();
}
/**
* Destruction of the servlet.
*/
public void destroy()
{
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("");
out.println("");
out.println(" A Servlet");
out.println(" ");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
try
{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
Connection conn = ds.getConnection();
ResultSet rs=conn.createStatement().executeQuery("select * from user");
rs.next();
out.print(rs.getString(2));
} catch (NamingException e) {
e.printStackTrace(out);
System.out.println(e.getMessage());
} catch (SQLException e) {
e.printStackTrace(out);
}
out.println("connection pool connected !!haha");
out.println(" ");
out.println("");
out.flush();
out.close();
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException
{
// Put your code here
}
}
我的mysql数据库名为javatest,表名为user,有三列"id","username","password"。servlet运行结果将打印出user表中的第一个用户名。
注意:如果你新建servlet,myeclipse会自动帮你在web.xml中生成相应的mapping,而这部分内容可能“插”进
DB Connection
jdbc/mysql
javax.sql.DataSource
Container
中,造成错误。注意自己手动调整。
别外,注意myeclipse中的servlet映射为“服务器ip:端口/工程文件名/servlet/servlet名”。
发表评论
-
堆栈的认知!
2012-01-20 10:15 521堆栈的认知! 2010年11月2 ... -
堆和栈的区别
2012-01-20 10:15 546堆和栈的区别 2010年10 ... -
VC串口通讯win32SDK版
2012-01-20 10:14 764VC串口通讯win32SDK版 2011年05月14日 ... -
Eclipse插件之Java代码分析工具: FindBugs
2012-01-20 10:14 679Eclipse插件之Java代码分析工具: FindBugs ... -
编译 boost
2012-01-20 10:14 546编译 boost 2011年05月17日 ... -
常用词汇解释
2012-01-19 15:15 768常用词汇解释 2011年02月10日 1*Android ... -
Linux下FrameBuffer直接写屏
2012-01-19 15:15 847Linux下FrameBuffer直接写屏 2011年09月 ... -
Grub4dos命令用法
2012-01-19 15:15 682Grub4dos命令用法 2010年06月01日 blo ... -
linux下nginx+php的安装与配置
2012-01-19 15:15 585linux下nginx+php的安装与 ... -
C段错误总结
2012-01-19 15:15 672C段错误总结 2012年01月07日 最近一段时间在li ... -
读写windows注册表
2012-01-17 05:02 1312读写windows注册表 2011年05月22日 Win ... -
VC编译器配置及编译选项
2012-01-17 05:02 447VC编译器配置及编译选 ... -
VC中的0XCCCCCCCC
2012-01-17 05:02 635VC中的0XCCCCCCCC 2011年10月23日 在 ... -
Windows系统的Regsvr32
2012-01-17 05:02 779Windows系统的Regsvr32 2011 ... -
VC 中用 IPicture 在窗口中显示图片文件
2012-01-17 05:02 983VC 中用 IPicture 在窗口中显示图片文件 2011 ... -
MyEclipse使用Junit
2012-01-16 03:53 750MyEclipse使用Junit 2011年05月08日 ... -
MyEclipse6.5上基于JAX-WS开发Webservice
2012-01-16 03:53 542MyEclipse6.5上基于JAX-WS开发Webservi ... -
myeclipse深度剖析
2012-01-16 03:53 905myeclipse深度剖析 2010年04月16日 工欲 ... -
MyEclipse7.0
2012-01-16 03:53 688MyEclipse7.0 2010年05月06日 ...
相关推荐
基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写...
网上问卷调查系统 使用myeclipse编写 javaBean+JSP+Servlet.zip 1、该资源内项目代码经过严格调试,下载即用确保可以运行! 2、该资源适合计算机相关专业(如计科、人工智能、大数据、数学、电子信息等)正在做课程...
适合初学者使用Myeclipse 做web开发
通过myeclipse将wsdl文件生成javabean
javabean的简单例子。可直接用。myeclipse的
MyEclipse开发调试JSP,Servlet,JavaBean,JSF,Structs
SOA练习通过myeclipse2014将wsdl文件生成javabean
该资源主要参考我的博客 Java+MyEclipse+Tomcat (五)DAO和Java Bean实现数据库和界面分开操作完成,http://blog.csdn.net/eastmount/article/details/45833663 主要讲述通过DAO和Java Bean实现对数据库的操作,并...
一般原则,所有的Javabean,servlet放在src中(可以建包),所有的jsp,html文件放在WebRoot下或子目录。例子:在tomcat目录E:\Tomcat6.0\conf,配置server.xml <Contextpath="/MyWeb"docBase="E:\\Myeclipse-...
jsp+javabean实现的分页web程序,另外附上我对这个程序做了个总结 和程序所需要用到的数据库,希望对大家有帮助。
解决MyEclipse不编译的方法,My Eclipse开发软件的不编译问题。
我们也想过很多办法解决这个问题,包括用MyEclipse连接数据库生成JavaBean,但多多少少还是会有一些不尽人意的地方,包括表和表字段的comment总是无法生成,而且还会生成很多无用的注释代码,让代码看起来一点都不...