码迷,mamicode.com
首页 > 数据库 > 详细

JSP与oracle数据库交互案例

时间:2014-06-30 16:29:53      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:jsp连接oracle数据库   jsp响应oracle数据库   jsp和oracle数据库交互   

本案例为咖啡销售情况录入查询系统

一、数据输入系统:

1.  设计输入信息页面

bubuko.com,布布扣

代码如下:

<html>
  <head>
   </head>
   
  <body>
   <center><H1>咖啡管理系统——录入系统</H1></center>
          欢迎来到录入系统<br />
         <form action="inputaction.jsp" method="post" name="form1" id="form1">
         <center>
咖啡名称:<input type="text" style="width:150px" name="cof_name" id="cof_name"><br />
供应商ID:<input type="text" style="width:150px" name="sup_id" id="sup_id"><br />
商品价格:<input type="text" style="width:150px" name="price" id="price"><br />
销售量:<input type="text" style="width:150px" name="sales" id="sales"><br />
总量:<input type="text" style="width:150px" name="total" id="total"><br />
    <input type="submit" value="提交数据">  
 </center> 
     </form>
  </body>
</html>

2. 设计处理oracle数据库输入请求网页inputaciton.jsp

(1)页面如下:

bubuko.com,布布扣

 

(2)数据库设计如下:

SQL> desc coffees;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 COF_NAME                                       VARCHAR2(32)
 SUP_ID                                             NUMBER(38)
 PRICE                                              FLOAT(126)
 SALES                                              NUMBER(38)
 TOTAL                                              NUMBER(38)

(3)处理数据库输入代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page
    import="java.sql.Connection"
 import="java.sql.ResultSet"
 import="java.sql.SQLException"
 import="java.sql.Statement"
 %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>      </head>
 
  <body>
       <center><H1>咖啡管理系统——录入系统</H1></center>
          正在执行录入<br />
 <%
 
       Class.forName("oracle.jdbc.driver.OracleDriver");
    java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  
   String url = "jdbc:oracle:thin:@100.100.100.20:1521:dbsid2";
  String user="scott";
  String password="tiger";
  Connection con=java.sql.DriverManager.getConnection(url, user, password);
   //取得登录用户名
  String cof_name = request.getParameter("cof_name");
  String sup_id =request.getParameter("sup_id");
  String price = request.getParameter("price");
  String sales =  request.getParameter("sales");
  String total = request.getParameter("total");
  String sql="insert into coffees values( ‘"+cof_name+"‘,"+sup_id+","+price+","+sales+","+total+")";
  Statement smt=con.createStatement();
        int rs=smt.executeUpdate(sql); 
  %>  
   录入成功 <br>
   <form action="output.jsp" name="form3" id="form3">
   <input type="submit" value="查询结果">
   </form>
  </body>
</html>

二、查询系统

1. 输入请求页面output.jsp

bubuko.com,布布扣

代码如下:

<html>
  <head>  </head>
   <body>

   <center><H1>咖啡管理系统—查询系统</H1></center>
         <form action="action.jsp" method="post" name="form2" id="form2">
         <center>
咖啡名称:<input type="text" style="width:150px" name="cof_name_1" id="cof_name_1"><br />
<input type="submit" value="提交查询">   
           </center>
       </form>
  </body>
</html>

2.  查询请求的处理action.jsp

(1)查询结果页面

bubuko.com,布布扣

(2)处理数据库查询代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page
    import="java.sql.Connection"
 import="java.sql.ResultSet"
 import="java.sql.SQLException"
 import="java.sql.Statement"
 %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  </head>
 
  <body>
  <center>
  <table border="1">
 <%
 
       Class.forName("oracle.jdbc.driver.OracleDriver");
    java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  
        String url = "jdbc:oracle:thin:@100.100.100.20:1521:dbsid2";
  String user="scott";
  String password="tiger";
  Connection con=java.sql.DriverManager.getConnection(url, user, password);
  
  
   //取得登录用户名
  String cof_name_1 = request.getParameter("cof_name_1");
//     String sql="select * from coffees where cof_name="+"‘" +cof_name_1 +"‘";
        String sql="select * from coffees where cof_name="+"‘"+cof_name_1+"‘";
  Statement smt=con.createStatement();
        ResultSet rs=smt.executeQuery(sql);
         while(rs.next()){
        String s1=rs.getString(1);
        int s2=rs.getInt(2);
        float s3=rs.getFloat(3);
        int s4=rs.getInt(4);      
        int s5=rs.getInt(5);
          %>
       
         <tr>
         <td><%=s1 %></td>
         <td><%=s2 %></td>
         <td><%=s3 %></td>
         <td><%=s4 %></td>
         <td><%=s5 %></td>
         </tr>
        <%} %>
       
       
 </table>
 查询成功 <br>
 </center>
  </body>
</html>

 

JSP与oracle数据库交互案例,布布扣,bubuko.com

JSP与oracle数据库交互案例

标签:jsp连接oracle数据库   jsp响应oracle数据库   jsp和oracle数据库交互   

原文地址:http://blog.csdn.net/clark_xu/article/details/35991203

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!