JavaWeb应用实例:用servlet实现oracle 基本增删改查
程序员文章站
2024-03-09 08:51:11
很久没有写博客了,可能是太懒散,不愿意把时间花在这上面,可是我心里还是知道写博客的重要性的 ,所以从今天开始 ,我将与大家分享一下我学java web写的一些小实例&nbs...
很久没有写博客了,可能是太懒散,不愿意把时间花在这上面,可是我心里还是知道写博客的重要性的 ,所以从今天开始 ,我将与大家分享一下我学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中 ,为了看的更直观 ,我就没有更改……
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。