很久没有写博客了,可能是太懒散,不愿意把时间花在这上面,可是我心里还是知道写博客的重要性的 ,所以从今天开始 ,我将与大家分享一下我学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(""); out.println(""); out.println(" "); out.println(" "); //输出表头部分 out.print("
姓名" + " | 班级 | 成绩 | 修改 | 删除 |
"+rs.getString("name") +" | "+rs.getString("calssGrent")+"" + " | "+rs.getString("result")+" | " + "修改 | " + "删除 | 添加数据 | "); out.println(" "); out.println(""); conn.close(); } catch (Exception e) { e.printStackTrace(); } out.flush(); out.close(); } }
姓名:
name='name' size=20 > |
班级: |
成绩: |
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(""); out.println(""); out.println(" "); out.println(" "); out.print(" 更新成功
查看列表 "); out.println(" "); out.println(""); } 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(""); out.println(""); out.println(" "); out.println(" "); //输入成绩的表单,提交至add out.print(" "); out.print("
姓名: |
班级: |
成绩: |
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("
查看列表"); } conn.close(); } catch (Exception e) { e.printStackTrace(); } out.flush(); out.close(); } }
web.xml 配置文件
<&#63;xml version="1.0" encoding="UTF-8"&#63;>index.jsp MMC_01 org.lsy.servlet.MMC_01 delete org.lsy.servlet.delete Add org.lsy.servlet.Add AddPage org.lsy.servlet.AddPage UpdatePage org.lsy.servlet.UpdatePage Update org.lsy.servlet.Update MMC_01 /MMC_01 delete /delete Add /Add AddPage /AddPage UpdatePage /UpdatePage Update /Update
本人并非老鸟,一直处于学习中,有什么错误的地方还请指证,上面的代码有很多重复的部分,很多不合理的地方 ,比较数据库连接直接暴露于操作servlet中 ,为了看的更直观 ,我就没有更改……
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。