MyEclipse添加Oracle数据库并使用Servlet读取Oracle数据

准备工作:

1.已安装oracle数据库,本文使用oracle11;

2.已安装好Myeclipse,本文使用Myeclipse2014;

3.已经安装Tomcat,本文使用Tomcat 8.0。

具体步骤:

1.按下图步骤进入MyEclipse Database Explorer视图;

MyEclipse添加Oracle数据库并使用Servlet读取Oracle数据_第1张图片

2.空白处鼠标右击后点选new进入Create a new connection driver界面;

MyEclipse添加Oracle数据库并使用Servlet读取Oracle数据_第2张图片

3.按下图步骤填表,具体数据意义如下:

database_name要改成数据库的SID,orcl为Oracle的数据库的自带实例。

在User name中填写实例中的案例,这里我填写scott;

在password中填写scott的密码;

JARS添加Oracle的驱动ojdbc6.jar,这个驱动在你Oracle的安装目录下\product\11.2.0\dbhome_1\jdbc\lib中可以找到;

MyEclipse添加Oracle数据库并使用Servlet读取Oracle数据_第3张图片

4.点击Finish之后,就可以看到成功了Oracle的连接了

MyEclipse添加Oracle数据库并使用Servlet读取Oracle数据_第4张图片

5.将上面的ojdbc6.jar放到tomcat的lib目录下,这一步很重要,不然驱动会没加载到;

6.测试代码:新建一个web项目,在项目中新建一个Servlet代码如下:

package com.city.j2ee.ch02;
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class ShowEmployeeList extends HttpServlet {
    private Connection cn=null;
           
    public void init(ServletConfig config) throws ServletException {
    // TODO Auto-generated method stub
        super.init(config);
        String driver=config.getInitParameter("driver");
        String url=config.getInitParameter("url");
        String user=config.getInitParameter("user");
        String password=config.getInitParameter("password");
        System.out.println(driver);
        System.out.println(url);
        System.out.println(user);
        System.out.println(password);
        try{
            Class.forName(driver);
            cn=DriverManager.getConnection(url,user,password);
            
        }catch(Exception e){
            System.out.println("Init Error:"+e.getMessage());
        }
    }
    /**
    * @see Servlet#destroy()
    */
    public void destroy() {
    // TODO Auto-generated method stub
        super.destroy();
        try{
            if(cn!=null&&(!cn.isClosed())){
                cn.close();
                cn=null;
            }
        }catch(Exception e)   {
            System.out.println("Destroy Error:"+e.getMessage());
        }
    }
    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        response.setCharacterEncoding("GBK");
        PrintWriter out =response.getWriter();
        out.println("");
        out.println("A Servlet");
        out.println("");
        try{
            String sql="select DEPTNO,DNAME,LOC from DEPT";
            PreparedStatement ps=cn.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            out.print("

TEST列表

"); out.print(""); out.print(""); out.print(""); out.print(""); while(rs.next()){ out.print(""); out.println(""); out.println(""); out.println(""); out.println(""); } rs.close(); ps.close(); out.print("
numbernameremark
"+ rs.getString("DEPTNO")+""+ rs.getString("DNAME")+""+ rs.getString("LOC")+"
"); }catch(Exception e) { out.println("

处理请求发生错误:"+e.getMessage()+"

"); } out.println(""); out.println(""); out.flush(); out.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request,response); } }
7. Web Root下的WEB-INF下的web.xml中配置servlet,代码如下:



  MyOracle
  
    This is the description of my J2EE component
    This is the display name of my J2EE component
    ShowEmployeeList
    com.city.j2ee.ch02.ShowEmployeeList
    
    driver
    oracle.jdbc.driver.OracleDriver
    
    
    url
    jdbc:oracle:thin:@localhost:1521:orcl
    
    
    user
    scott
    
    
    password
    123456
    
  

  
    ShowEmployeeList
    /ShowEmployeeList
  
8.将工程添加到Tomcat服务器,运行服务器,在浏览器输入url,结果如下:

MyEclipse添加Oracle数据库并使用Servlet读取Oracle数据_第5张图片


参考链接:http://jingyan.baidu.com/article/ad310e80ab1c8c1849f49e22.html  注意:此文忽略了我上面的第五步,所以连不上,我是菜鸟,刚开始老是不知道问题出在哪



你可能感兴趣的:(JavaWeb)