Recently, the company leader told the next product that it might involve an oracle database. The one I used the most was mssql. I learned mysql a few times ago and I have used it a little. I haven't contacted oracle. I should have done .net development before, so I have learned servlet and felt that these technologies are more like interfaces written by ashx wcf webserver.
1. First, install the oracle database on the computer. Install the ORACLE database. According to common sense, it is enough to download and install it directly, but I encountered problems in the middle. The reason is that my system is Win7 64. The client client of oracle does not support win7 64. Even the system of 64 can only download 32-bit client installation. I reuse ghost and restore the system. (In fact, I found that it can be completely uninstalled later)
2. I have always installed ecplise on my computer, but there is no Java web development environment. Let me talk about the installation method below.
ecplise menu bar ----Help-->Install New Software
Then enter: http://download.eclipse.org/releases/kepler
After waiting for a while, the javaWeb installation option will appear: Then select the last item of web...Check it and then finish the next step. Below is a screenshot
At this point, our javaweb development environment is installed, which contains servlets...
3. Then download the apache-tomcat server: I downloaded 7, and then decompressed to any path
4. Create a servlet environment
1: Open ecplise
2: Create apache-tomcat server (servlet will be used, I will talk about the creation method below)
Menu bar file----->new----->other---->Server (click on the folder)--Server and then directly next and select Apache to select the corresponding version of the server you downloaded. If you are 6.7.7. Such versions are generally selected for 6 or 7......... next and select the folder path of the folder you just unzipped apache-tomcat in Browser (below is a screenshot)
3: Create severlet
Menu bar file----->new----->other---->Web-->Dynamic Web Project is created and then directly next. After jumping, you can just take a name and finish it. The following is a screenshot.
After creation: we need to create the .java file of the servlet. I will directly upload the image
At this point, our Servlet has been created.
4: Run the Servlet project we created
java Resources--->src--->package name---.java(servlet file) Right-click run as
Select the server in the list: If the server status is Stopped at this time, we need to restart the machine.
Then, after completing the servlet, you can start the servlet.
5: Import oracle driver package ojdbc14.jar and directly enter the query without knowing the path. The following screenshot
The above is a screenshot of the deployment assembly. Click Java Build Path Entries and select the jar you just created and import it.
6: Connect to local Oracle database to query the data output of a table
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {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 {Class.forName("oracle.jdbc.driver.OracleDriver");//Load the driver conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "SCOTT", "Lh19870426");//Create the connection stmt = conn.createStatement();//Create statementrs = stmt.executeQuery("select * from ORACLETESTDATABASE"); //Get the result set while(rs.next()){//Tranquility of the result set out.println("<tr>");out.println("<td>" + rs.getString("name") + "</td>");//Get out the column value out.println("</tr>");} out.println("</table>");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();} finally {try {if(rs != null) {rs.close();rs = null;}if(stmt != null) {stmt.close();stmt= null;}if(conn != null) {conn.close();conn = null;}} catch (SQLException e) {e.printStackTrace();}}}Output screenshot: