Flask接口签名sign原理与实例代码浅析
230
2023-06-19
JavaWeb应用实例:用servlet实现oracle 基本增删改查
很久没有写博客了,可能是太懒散,不愿意把时间花在这上面,可是我心里还是知道写博客的重要性的 ,所以从今天开始 ,我将与大家分享一下我学java WEB写的一些小实例 ,我个人是不太喜欢书本上的晦涩的概念的,所以我花了更多的时间在一些应用实例上,我觉得这样的学习方式很适合我,由简到繁,由浅入深
废话不多话,开始我们的第一个实例:
servlet实现Oracle 基本增删改查
开发环境:JDK7 +Myeclipse10+tomcat7.0+oracle 11g
首先附上数据库创建脚SbpZVor本:
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("
"
while(rs.next())
{
//获取数据库中的ID
String id=rs.getString("id");
//输出一列元素
out.print("
+"
"
"
"
}
out.println("
out.println(" ");
out.println("");
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(" ");
out.println("");
out.println("
out.println("
//输入成绩的表单,提交至Update
out.print("
out.print("
name='name' size=20 >
out.print("
out.print("
//为了作为查询数据库的条件 ,这里要把ID传过去
out.print("");
out.print("
out.print(" ");
out.println("");
}
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(" ");
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("
out.print("
out.print("
out.print("
out.println("
out.println(" ");
out.println("");
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("
查看列表");
}
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
out.flush();
out.close();
}
}
web.xml 配置文件
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://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">
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://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">
本人并非老鸟,一直处于学习中,有什么错误的地方还请指证,上面的代码有很多重复的部分,很多不合理的地方 ,比较数据库连接直接暴露于操作servlet中 ,为了看的更直观 ,我就没有更改……
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~