很久没有写博客了,可能是太懒散,不愿意把时间花在这上面,可是我心里还是知道写博客的重要性的 ,所以从今天开始 ,我将与大家分享一下我学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中 ,为了看的更直观 ,我就没有更改……
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持鸟哥教程(niaoge.com)。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#niaoge.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。