I haven't written a blog for a long time. I may be too lazy and unwilling to spend my time on it. But I still know the importance of writing a blog in my heart. So from today on, I will share with you some small examples I learned Java WEB. I personally don't like the obscure concepts in books, so I spent more time on some application examples. I think this learning method is very suitable for me, from simple to traditional, from shallow to deep
Let's talk nonsense, start our first example:
servlet implements Oracle basic addition, deletion, modification and search
Development environment: JDK7 +Myeclipse10 +tomcat7.0 +oracle 11g
First, attach the database creation script:
create table student( id VARCHAR2(50) PRIMARY KEY NOT NULL, name varchar2(50) NOT NULL, calssGrent varchar2(50) NOT NULL , result varchar(12) NOT NULL ); insert into student(id,name,calssGrent,result) values(perseq.nextval,'Zhang San','33','98')
The following figure shows the code structure diagram and function demonstration interface that are not badly done just to implement functions:
MMC_01.Java
Page //The main interface provides methods for adding, modifying and deleting
package org.lsy.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import oracle.jdbc.oracore.TDSPatch; public class MMC_01 extends HttpServlet { // The driver is in the jar package of the jdbc driver configured in the classpath before // The connection address is provided separately by each database manufacturer, so you need to remember separately public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:LIUSY"; // The user name for connecting to the database public static final String DBUSER = "scott"; // The password for connecting to the database public static final String DBPASS = "tiger"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Set the encoding of the request and response, otherwise garbled code will easily appear on the page request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;UTF-8"); //Output stream PrintWriter out = response.getWriter(); //Database connection code try { Connection conn = null; // Object indicating the connection to the database Statement stmt = null; // Indicates the update operation of the database String sql="insert into student(id,name,calssGrent,result) values('67','Liu 12yu','33','98')"; System.out.println(sql); // 1. Use the Class class to load the driver Class.forName("oracle.jdbc.driver.OracleDriver"); // 2. Connect to the database conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); // 3. Statement interface needs to be instantiated through the Connection interface stmt = conn.createStatement(); //Use rs to get query results. It is best not to use * for SQL statements here * I have fewer fields, so I just want to be lazy, -, - ResultSet rs=stmt.executeQuery("select *from student"); out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">"); out.println("<HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); //Output table header out.print("<table align='center' border='1'><tr><td>Name" + "</td><td>Class</td><td>Score</td><td>Modify</td><td>Delete</td></tr>"); while(rs.next()) { //Get ID in the database String id=rs.getString("id"); //Output a column of elements out.print("<tr><td>"+rs.getString("name") +"</td><td>"+rs.getString("calssGrent")+"" + "</td><td>"+rs.getString("result")+"</td>" + "<td><a href='UpdatePage?id="+id+"'>Modify</a></td>" + "<td><a href='delete?id="+id+"'>Delete</a></td></tr>"); } out.println("<td><a href=AddPage>Add data</a></td>"); out.println("</BODY>"); out.println("</HTML>"); conn.close(); } catch (Exception e) { e.printStackTrace(); } out.flush(); out.close(); } }UpdatePage.java //Get the data to be modified and submit the modified data to Update
package org.lsy.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.crypto.provider.RSACipher; public class UpdatePage extends HttpServlet { // The connection address is provided separately by each database manufacturer, so you need to remember separately public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:LIUSY"; // The user name for connecting to the database public static final String DBUSER = "scott"; // The password for connecting to the database public static final String DBPASS = "tiger"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id"); //Set the encoding of the request and response, otherwise garbled code will easily appear on the page request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;UTF-8"); PrintWriter out = response.getWriter(); try { Connection conn = null; // The object that represents the connection to the database Statement stmt = null; // The database update operation// 1. Use the Class class to load the driver Class.forName("oracle.jdbc.driver.OracleDriver"); // 2. Connect to the database conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); // 3. The Statement interface needs to be instantiated through the Connection interface Stmt = conn.createStatement(); // Use rs to get the query results. It is best not to use the SQL statement here * I have fewer fields, so I'm lazy, -, - ResultSet rs=stmt.executeQuery("select *from student where id='"+id+"'"); while (rs.next()) { out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); //Enter the form of the score and submit it to Update out.print(" <form action='Update' method='post'> "); out.print("<table align='center'> <tr><td>Name: <input type='text' value='"+rs.getString("name")+"' name='name' size=20 ></td></tr>"); out.print("<tr><td>Class: <input type='text' value='"+rs.getString("calssGrent")+"' name='grent' size=20 ></td></tr>"); out.print("<tr><td>Score: <input type='text' value='"+rs.getString("result")+"' name='result' size=20 ></td></tr>"); //To be used as a condition for querying the database, the ID must be passed over here out.print("<input type='hidden' value='"+id+"' name='id'>"); out.print("<tr><td><input type='submit' value='modify'><input type='reset' value='reset'></td></tr></table></form>"); out.print(" </BODY>"); out.println("</HTML>"); } out.flush(); out.close(); } catch (Exception e) { e.printStackTrace();// TODO: handle exception } } } Update.java receives the data to be modified and updates the database
package org.lsy.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Update extends HttpServlet { // The driver is the jar package of the jdbc driver configured in the classpath before // The connection address is provided separately by each database manufacturer, so you need to remember separately public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:LIUSY"; // The user name for connecting to the database public static final String DBUSER = "scott"; // The password for connecting to the database public static final String DBPASS = "tiger"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;UTF-8"); PrintWriter out = response.getWriter(); Connection conn = null; // The object representing the connection to the database Statement stmt = null; // The database update operation// Get the parameter String id=request.getParameter("id"); System.out.println(id); String name=request.getParameter("name"); String grant=request.getParameter("grent"); String result=request.getParameter("result"); //Update SQL String sqlString="update student set name='"+name+"' ," + "calssGrent='"+grent+"',result='"+result+"' where id='"+id+"' "; // 1. Use the Class class to load the driver Class.forName("oracle.jdbc.driver.OracleDriver"); // 2. Connect to the database conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); // 3. The Statement interface needs to be instantiated through the Connection interface stmt = conn.createStatement(); int pd=stmt.executeUpdate(sqlString); if(pd!=0) { out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" Updated successfully<br><a href=MMC_01>View list</a> "); out.println(" </BODY>"); out.println("</HTML>"); } out.flush(); out.close(); stmt.close(); conn.close(); } catch (Exception e) { // TODO: handle exception } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }addPage .java, add data page
package org.lsy.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AddPage extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Set the encoding of the request and response, otherwise garbled code will easily appear on the page request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;UTF-8"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); //Enter the form of the score and submit it to add out.print(" <form action='Add' method='post'> "); out.print("<table align='center'> <tr><td>Name: <input type='text' name='name' size=20 ></td></tr>"); out.print("<tr><td>Class: <input type='text' name='grent' size=20></td></tr>"); out.print("<tr><td>Score: <input type='text' name='result' size=20></td></tr>"); out.print("<tr><td><input type='submit' value='add'><input type='reset' value='reset'></td></tr></table></form>"); out.println("<tr><td><a href=MMC_01>View list</a></td></tr>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }add Receive the transmission from the addpage and update the database
package org.lsy.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.org.apache.xerces.internal.impl.io.UTF8Reader; import oracle.jdbc.oracore.TDSPatch; public class Add extends HttpServlet { // The driver is in the jar package of the jdbc driver configured in classpath before // The connection address is provided separately by each database manufacturer, so you need to remember separately public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:LIUSY"; // Username for connecting to the database public static final String DBUSER = "scott"; // Password for connecting to the database public static final String DBPASS = "tiger"; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doGet(req, resp); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Set the encoding of the request and response, otherwise garbled code will easily appear on the page request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;UTF-8"); //Output stream PrintWriter out = response.getWriter(); //Database connection code try { Connection conn = null; // Object that represents the connection to the database pstmt = null; // Indicates the update operation of the database String nameString=request.getParameter("name"); String grantString=request.getParameter("grent"); String resultString=request.getParameter("result"); System.out.print(nameString); String sql="insert into student(id,name,calssGrent,result) values(perseq.nextval,'"+nameString+"','"+grentString+"','"+resultString+"')"; // 1. Use the Class class to load the driver Class.forName("oracle.jdbc.driver.OracleDriver"); // 2. Connect to the database conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); pstmt=conn.createStatement(); int pd=pstmt.executeUpdate(sql); if (pd!=0) { out.println("Added successfully"); out.println("<br><a href=MMC_01>View List</a>"); } conn.close(); } catch (Exception e) { e.printStackTrace(); } out.flush(); out.close(); } } web.xml configuration file
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet-name>MMC_01</servlet-name> <servlet-class>org.lsy.servlet.MMC_01</servlet-class> </servlet> <servlet> <servlet-name>delete</servlet-name> <servlet-class>org.lsy.servlet.delete</servlet-class> </servlet> <servlet> <servlet-name>Add</servlet-name> <servlet-class>org.lsy.servlet.Add</servlet-class> </servlet> <servlet> <servlet-name>AddPage</servlet-name> <servlet-class>org.lsy.servlet.AddPage</servlet-class> </servlet> <servlet> <servlet-name>UpdatePage</servlet-name> <servlet-class>org.lsy.servlet.UpdatePage</servlet-class> </servlet> <servlet> <servlet-name>Update</servlet-name> <servlet-class>org.lsy.servlet.Update</servlet-class> </servlet> <servlet-mapping> <servlet-mapping> <servlet-name>MMC_01</servlet-name> <url-pattern>/MMC_01</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-mapping> <servlet-name>delete</servlet-name> <url-pattern>/delete</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Add</servlet-name> <url-pattern>/Add</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AddPage</servlet-name> <url-pattern>/AddPage</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-mapping> <servlet-name>UpdatePage</servlet-name> <url-pattern>/UpdatePage</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-mapping> <servlet-name>Update</servlet-name> <url-pattern>/Update</url-pattern> </servlet-mapping> </web-app>
I am not an old bird, I have been learning. Please prove if there are any mistakes. The above code has many duplicate parts and many unreasonable parts. Compared with the database connections are directly exposed to the operation servlet. In order to see more intuitively, I did not change it...
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.