JavaWeb使用實例:用servlet完成oracle 根本增刪改查。本站提示廣大學習愛好者:(JavaWeb使用實例:用servlet完成oracle 根本增刪改查)文章只能為提供參考,不一定能成為您想要的結果。以下是JavaWeb使用實例:用servlet完成oracle 根本增刪改查正文
很久沒有寫博客了,能夠是太懶散,不情願把時間花在這下面,可是我心裡還是知道寫博客的重要性的 ,所以從明天開端 ,我將與大家分享一下我學Java WEB寫的一些小實例 ,我團體是不太喜歡書本上的流暢的概念的,所以我花了更多的時間在一些使用實例上,我覺得這樣的學習方式很合適我,由簡到繁,由淺入深
廢話不多話,開端我們的第一個實例:
servlet完成Oracle 根本增刪改查
開發環境:JDK7 +Myeclipse10+tomcat7.0+oracle 11g
首先附上數據庫創立腳本:
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,'張三','33','98')
下圖為代碼構造圖和功用演示界面做的比擬蹩腳 只為完成功用:
MMC_01.Java
頁面 //主界面提供添加,修正刪除的辦法
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 { // 驅動順序就是之前在classpath中配置的jdbc的驅動順序的jar包中 // 銜接地址是由各個數據庫消費商獨自提供的,所以需求獨自記住 public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:LIUSY"; // 銜接數據庫的用戶名 public static final String DBUSER = "scott"; // 銜接數據庫的密碼 public static final String DBPASS = "tiger"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設置懇求和呼應的編碼 ,不然頁面上容易呈現亂碼 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;UTF-8"); //輸入流 PrintWriter out = response.getWriter(); //數據庫銜接代碼 try { Connection conn = null; // 表示數據庫的銜接的對象 Statement stmt = null; // 表示數據庫的更新操作 String sql="insert into student(id,name,calssGrent,result) values('67','劉12宇','33','98')"; System.out.println(sql) ; // 1、運用Class類加載驅動順序 Class.forName("oracle.jdbc.driver.OracleDriver"); // 2、銜接數據庫 conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); // 3、Statement接口需求經過Connection接口停止實例化操作 stmt = conn.createStatement(); //用 rs獲取查詢後果 ,這裡的SQL語句最好不要用* 我的字段比擬少所以就偷一下懶,-,- ResultSet rs=stmt.executeQuery("select *from student"); 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("<table align='center' border='1'><tr><td>姓名" + "</td><td>班級</td><td>成果</td><td>修正</td><td>刪除</td></tr>"); while(rs.next()) { //獲取數據庫中的ID String id=rs.getString("id"); //輸入一列元素 out.print("<tr><td>"+rs.getString("name") +"</td><td>"+rs.getString("calssGrent")+"" + "</td><td>"+rs.getString("result")+"</td>" + "<td><a href='UpdatePage?id="+id+"'>修正</a></td>" + "<td><a href='delete?id="+id+"'> 刪除 </a></td></tr>"); } out.println("<td><a href=AddPage>添加數據</a></td>"); out.println(" </BODY>"); out.println("</HTML>"); conn.close(); } catch (Exception e) { e.printStackTrace(); } out.flush(); out.close(); } }
UpdatePage.java //獲取要修正的數據 ,並把修正過的數據提交給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 { // 銜接地址是由各個數據庫消費商獨自提供的,所以需求獨自記住 public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:LIUSY"; // 銜接數據庫的用戶名 public static final String DBUSER = "scott"; // 銜接數據庫的密碼 public static final String DBPASS = "tiger"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id"); //設置懇求和呼應的編碼 ,不然頁面上容易呈現亂碼 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;UTF-8"); PrintWriter out = response.getWriter(); try { Connection conn = null; // 表示數據庫的銜接的對象 Statement stmt = null; // 表示數據庫的更新操作 // 1、運用Class類加載驅動順序 Class.forName("oracle.jdbc.driver.OracleDriver"); // 2、銜接數據庫 conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); // 3、Statement接口需求經過Connection接口停止實例化操作 stmt = conn.createStatement(); //用 rs獲取查詢後果 ,這裡的SQL語句最好不要用* 我的字段比擬少所以就偷一下懶,-,- 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>"); //輸出成果的表單,提交至Update out.print(" <form action='Update' method='post'> "); out.print("<table align='center'> <tr><td>姓名:<input type='text' value='"+rs.getString("name")+"'
name='name' size=20 ></td></tr>"); out.print("<tr><td>班級:<input type='text' value='"+rs.getString("calssGrent")+"' name='grent' size=20></td></tr>"); out.print("<tr><td>成果:<input type='text' value='"+rs.getString("result")+"' name='result' size=20></td></tr>"); //為了作為查詢數據庫的條件 ,這裡要把ID傳過來 out.print("<input type='hidden' value='"+id+"' name='id'>"); out.print("<tr><td><input type='submit' value='修正'><input type='reset' value='重置'></td></tr></table></form>"); out.print(" </BODY>"); out.println("</HTML>"); } out.flush(); out.close(); } catch (Exception e) { e.printStackTrace();// TODO: handle exception } } }
Update.java 接納要修正的數據 ,並更新數據庫
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 { // 驅動順序就是之前在classpath中配置的jdbc的驅動順序的jar包中 // 銜接地址是由各個數據庫消費商獨自提供的,所以需求獨自記住 public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:LIUSY"; // 銜接數據庫的用戶名 public static final String DBUSER = "scott"; // 銜接數據庫的密碼 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; // 表示數據庫的銜接的對象 Statement stmt = null; // 表示數據庫的更新操作 //獲取表單提交的參數 String id=request.getParameter("id"); System.out.println(id); String name=request.getParameter("name"); String grent=request.getParameter("grent"); String result=request.getParameter("result"); //更新SQL String sqlString="update student set name='"+name+"' ," + "calssGrent='"+grent+"',result='"+result+"' where id='"+id+"' "; // 1、運用Class類加載驅動順序 Class.forName("oracle.jdbc.driver.OracleDriver"); // 2、銜接數據庫 conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); // 3、Statement接口需求經過Connection接口停止實例化操作 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(" 更新成功<br><a href=MMC_01>檢查列表</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,添加數據頁面
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 { //設置懇求和呼應的編碼 ,不然頁面上容易呈現亂碼 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>"); //輸出成果的表單,提交至add out.print(" <form action='Add' method='post'> "); out.print("<table align='center'> <tr><td>姓名:<input type='text' name='name' size=20 ></td></tr>"); out.print("<tr><td>班級:<input type='text' name='grent' size=20></td></tr>"); out.print("<tr><td>成果:<input type='text' name='result' size=20></td></tr>"); out.print("<tr><td><input type='submit' value='添加'><input type='reset' value='重置'></td></tr></table></form>"); out.println("<tr><td><a href=MMC_01>檢查列表</a></td></tr>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
add 接納addpage傳來的傳,更新數據庫
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 { // 驅動順序就是之前在classpath中配置的jdbc的驅動順序的jar包中 // 銜接地址是由各個數據庫消費商獨自提供的,所以需求獨自記住 public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:LIUSY"; // 銜接數據庫的用戶名 public static final String DBUSER = "scott"; // 銜接數據庫的密碼 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 { //設置懇求和呼應的編碼 ,不然頁面上容易呈現亂碼 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;UTF-8"); //輸入流 PrintWriter out = response.getWriter(); //數據庫銜接代碼 try { Connection conn = null; // 表示數據庫的銜接的對象 Statement pstmt = null; // 表示數據庫的更新操作 String nameString=request.getParameter("name"); String grentString=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、運用Class類加載驅動順序 Class.forName("oracle.jdbc.driver.OracleDriver"); // 2、銜接數據庫 conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); pstmt=conn.createStatement(); int pd=pstmt.executeUpdate(sql); if (pd!=0) { out.println("添加成功"); out.println("<br><a href=MMC_01>檢查列表</a>"); } conn.close(); } catch (Exception e) { e.printStackTrace(); } out.flush(); out.close(); } }
web.xml 配置文件
<?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-name>MMC_01</servlet-name> <url-pattern>/MMC_01</url-pattern> </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-name>UpdatePage</servlet-name> <url-pattern>/UpdatePage</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Update</servlet-name> <url-pattern>/Update</url-pattern> </servlet-mapping> </web-app>
自己並非老鳥,不斷處於學習中,有什麼錯誤的中央還請指證,下面的代碼有很多反復的局部,很多不合理的中央 ,比擬數據庫銜接直接暴露於操作servlet中 ,為了看的更直觀 ,我就沒有更改……
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持。