JSP数据库操作经典简例

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>  </title>
<style type="text/css">
<!--
body {
 background-color: #9999CC;
}
.style2 {font-size: 14}
.style3 {
 font-size: 14px;
 color: #33FF33;
}
.style5 {font-size: 12px; color: #CC3300; }
.style6 {font-size: 12px}
.newstyle {
 font-family: Arial, Helvetica, sans-serif;
 font-size: 12px;
 font-style: normal;
 color: #663366;
 background-color: #66CCFF;
 height: 23px;
 width: 50px;
 display: inline-table;
 margin: auto;
}
.newstyleCopy {
 font-family: Arial, Helvetica, sans-serif;
 font-size: 12px;
 font-style: normal;
 color: #663366;
 background-color: #6699CC;
 height: 25px;
 width: 100px;
 display: inline-table;
 margin: auto;
 border: none;
}
.style7 {color: #FF9900}
-->
</style>

<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*" %>
<%!
 boolean flag=true;
 //boolean bQuery=false;
 //boolean bInsert=false;
 //boolean bDelete=false;
%>
  <title>  </title>
 
  <script language="javascript">
   function newwin(urll)
   {
    var newwin=window.open(urll,"newwin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=450");
    newwin.focus();
    return false;
   }
  
   function change()
   {
    switch (form.select.value)
    {
     case "query":
      form.query.disabled=false;form.insert.disabled=true;
      form.update.disabled=true;form.deletee.disabled=true;
      break;
     case "insert":
      form.insert.disabled=false;form.query.disabled=true;
      form.update.disabled=true;form.deletee.disabled=true;
      break;
     case "update":
      form.update.disabled=false;form.insert.disabled=true;
      form.query.disabled=true;form.deletee.disabled=true;
      break;
     case "deletee":
      form.deletee.disabled=false;form.insert.disabled=true;
      form.update.disabled=true;form.query.disabled=true;
      break;
     default:;
    }
   
   }
  </script>

</head>

<body>
<script language="javascript">
 
   function queryCheck()
   {
    //if(document.form.no.value="/="")
    //{
     //alert("请输入学号!");
     //document.form.no.focus();
    //}
    //if(document.form.no.value=="")
    // document.cookie="bQuery=false";
    //else
    // document.cookie="bQuery=true";
    //window.location.reload();
    //window.location="index.jsp"";
   
   }
  
  </script>
 
 
<div align="center"><form name="form" method="post" action="index.jsp">
  <table width="564" height="108" border="1" cellspacing="0" bordercolor="#660033" bgcolor="#6666CC" id="table">
    <tr bgcolor="#009999">
      <td height="43" colspan="5" class="style2"><div align="center" class="style3">数据库操作(查询,添加,更新,删除)</div></td>
    </tr>
    <tr bgcolor="#00CCFF">
      <td width="100" height="33" class="style5">学号</td>
      <td width="100" height="33"><div align="center" class="style5">姓名</div></td>
      <td width="100" height="33"><div align="center" class="style5">语文</div></td>
      <td width="100" height="33"><div align="center" class="style5">数学</div></td>
      <td width="100" height="33"><div align="center" class="style5">英语</div></td>
    </tr>
<%
 Connection conn=null;
 Statement stat=null;
 ResultSet rs=null;
 
 
 String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
 
 try
    {
     Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    }
    catch(ClassNotFoundException ce)
    {
     //out.println(ce.getMessage());
     ce.printStackTrace();
    }   
    try
    {
     conn=DriverManager.getConnection(url,"sa","administrator");
     stat=conn.createStatement();
      rs=stat.executeQuery("select * from grade");
 
   String query=request.getParameter("select");
   String no=request.getParameter("no");
   String wname=request.getParameter("name");
   String newname="";
   try
   {
       newname=new String(wname.getBytes("ISO-8859-1"));
   }
   catch(Exception e)
   {
   }
  
   //String newname=new String(wname.getBytes("ISO-8859-1"));
   String chinese=request.getParameter("chinese");
   String math=request.getParameter("math");
   String english=request.getParameter("english"); 
   String strInsert="INSERT INTO grade VALUES('"+no+"','"+newname+"','"+chinese+"','"+math+"','"+english+"')";
   String strUpdate="UPDATE  grade SET 语文="+chinese+",数学="+math+",英语="+english+"where 学号="+no;
   String strDelete="Delete from grade where 学号="+no;
   //String strquery=new String("insert");
  
   注释段开始
    try{
   if(query==null)
    rs=stat.executeQuery("select * from grade");
  
   
   if(query.equals("query"))
   {
    if(request.getParameter("no")==null || request.getParameter("no").equals(""))
     rs=stat.executeQuery("select * from grade");   
    else
     rs=stat.executeQuery("select * from grade where 学号='"+no+"'");
   }
  
   if(query.equals("insert"))
   {
    stat.executeUpdate(strInsert);   
    rs=stat.executeQuery("select * from grade");
   }
   if(query.equals("update"))
   {
    stat.executeUpdate(strUpdate);   
    rs=stat.executeQuery("select * from grade");
   }
   if(query.equals("deletee"))
   {
    stat.executeUpdate(strDelete);  
    rs=stat.executeQuery("select * from grade");
   }
   }
   catch(Exception e)
   {
   }
  
   注释段结束
  
        while(rs.next())
        {
   
   %>
 <tr>
    <td width="100" height="33"><span class="style6"><%=rs.getString("学号")%></span></td>
    <td width="100" height="33"><div align="center" class="style6"><%=rs.getString("姓名")%></div></td>
    <td width="100" height="33"><div align="center" class="style6"><%=rs.getString("语文")%></div></td>
    <td width="100" height="33"><div align="center" class="style6"><%=rs.getString("数学")%></div></td>
    <td width="100" height="33"><div align="center" class="style6"><%=rs.getString("英语")%></div></td>
    </tr>
   <%
        }
   
    }
    catch(SQLException e)
    {
    
     e.printStackTrace();
    }
    finally
    {
     stat.close();
     conn.close();
    
    }
   
%>
 <tr>
    <td width="100" height="33"><span class="style6">--------</span></td>
    <td width="100" height="33"><div align="center" class="style6">--------</div></td>
    <td width="100" height="33"><div align="center" class="style6">--------</div></td>
    <td width="100" height="33"><div align="center" class="style6">--------</div></td>
    <td width="100" height="33"><div align="center" class="style6">--------</div></td>
    </tr>
 <tr>
    <td height="33" colspan="5"><div align="center" class="style6"></div>     
      <div align="center" class="style6 style7">--------请在下面输入你所要操作的信息--------</div></td>
   </tr>
 <tr>
    <td width="100" height="33">
         <div align="center">
           <input name="no" type="text" class="newstyleCopy" id="no">
          </div></td>
    <td width="100" height="33">
         <div align="center">
           <input name="name" type="text" class="newstyleCopy" id="name">
          </div></td>
    <td width="100" height="33">
         <div align="center">
           <input name="chinese" type="text" class="newstyleCopy" id="chinese">
          </div></td>
    <td width="100" height="33">
         <div align="center">
           <input name="math" type="text" class="newstyleCopy" id="math">
          </div></td>
    <td width="100" height="33">
         <div align="center">
           <input name="english" type="text" class="newstyleCopy" id="english">
          </div></td>
 </tr>
 
 <tr>
    <td height="33" colspan="5"><div align="center" class="style6"></div>     
    <div align="center" class="style6">
   <select name="select" class="newstyleCopy" onChange="change()" >
    <option value=""></option>
    <option value="query">查询</option>
    <option value="insert">添加</option>
    <option value="update">更新</option>
    <option value="deletee">删除</option>
      </select> 
     <input disabled="true" name="button" type="submit" class="newstyle" id="query"  value="查询">       
      <input disabled="true" name="Submit" type="submit" class="newstyle"  id="insert" value="添加">
 
   <input disabled="true" name="Submit" type="submit" class="newstyle"  id="update" value="修改">  
   <input disabled="true" name="Submit" type="submit" class="newstyle"  id="deletee" value="删除">
    </div>      <div align="center" class="style6"></div></td>
    </tr>
 
 
   
  </table>
</form>
</div>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值