Java servlet builds Java Web development environment with Oracle _java

Source: Internet
Author: User
Tags stmt java web tomcat server

Recently, the company leader told the next product, may involve Oracle database, formerly used most is MSSQL, some time ago learned MySQL is also a little used. Oracle has not been contacted. Should have done for me before. NET development, so data access interface this piece, involves the ashx WCF webserver these, learned the next servlet feel these technologies compare like ashx write interface.

First, install the Oracle database on the computer, install the Oracle database according to the common sense, the direct download installation on the line, but midway is a problem, the reason is that my system is Win7-Oracle client clients do not support Win7 64, Even 64 of the system can only download 32-bit client installation, where I reuse ghost restore the next system. (It was found to be completely uninstalled later)

Second, my computer is always equipped with ecplise, but did not install the Java Web Development environment, the following I say the installation method bar

Ecplise menu bar----Help-->install New Software

Then in work with: text box on the left: Http://download.eclipse.org/releases/kepler

The Javaweb installation option appears when you wait a moment: Then select the Last web ... Tick the next step is the screenshot below

Here our Javaweb development environment is installed, which contains the servlet ...

Third, then download the APACHE-TOMCAT server: I am under 7, and then extract to any path

Iv. Creating a servlet environment

1: Open Ecplise

2: Create the Apache-tomcat server (the servlet will use, I'll say the creation method below)

The menu bar file----->new---->other---->server (Point open folder)--server then direct next then select Apache to select the corresponding version of the server you downloaded, if you are 6.7. Several of these versions of GM have chosen 6 or 7 ... Next and then in browser select the folder path you just unzipped Apache-tomcat (here is a screenshot)

3: Create Severlet

Menu bar File----->new---->other---->web-->dynamic WEB Project Create and then directly next, jump after a random name on the finish of the following is a screenshot

Once created: We'll need to create the. java file for the servlet. I'm going directly to the diagram

Here, our servlet is created.

4: Run the servlet project we created

Java Resources--->SRC---> Package name---. java (servlet file) right mouse click Run as

Select the server in the list: If the server status is stopped, we need to reboot the machine

Then you can start the servlet after the electrical finish.

5: Import Oracle driver package Ojdbc14.jar do not know the path directly input query, screenshot below

Above is the deployment assembly screenshot Click Java Build Path entries after selecting the jar you just created and import

6: Connect the local Oracle database query the data output of a table

protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {&N
bsp;             connection conn = null;
        statement stmt = null;
        resultset rs = null;
        response.setcontenttype ("text/html");
        response.setcharacterencoding ("gb2312");
        printwriter out = Response.getwriter ();
        out.println ("<table border=1>");         out.println ("<tr><td>content:</td></tr
> ");         try {                     &nbsP;   class.forname ("Oracle.jdbc.driver.OracleDriver");/Load Driver             conn = Drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:orcl", " SCOTT "," Lh19870426 ");//Create connection           stmt = Conn.createstatement ()//Create statement             rs = Stmt.executequery ("SELECT * from Oracletestdatabase"); Get result set             while (Rs.next ()) {//traverse result set                  out.println ("<tr
> ");                 out.println (" <td> "+ rs.getstring (" name ") +" </td> ")//Fetch column value                  out.println ("</tr>");             }       
      out.println ("</table>"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} catch (ClassNotFoundException e) {     
       e.printstacktrace (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} catch (SQLException e) {      
      e.printstacktrace (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} finally {             try {                 if (rs!= null) {             
       rs.close ();                     rs = null;                 }                  if (stmt!= null) {                    
 stmt.close ();                   
  stmt= null;                 }    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF (conn!= null) {                    
 conn.close ();                   
  conn = null;                }    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} catch (SQLException e) {    
            e.printstacktrace ();             }        &NBSP;&NBSP}                }

Output screenshot:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.