首页 > 数据库 >jsp调用数据库

jsp调用数据库

时间:2023-06-04 18:56:31浏览次数:74  
标签:调用 return val 数据库 request preparedStatement jsp import servlet

deviceInfos.jsp

<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.*"%>
<%@ page import="java.sql.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="utf-8" %>
<html>
    <head>
        <title>jsp作业3</title>
        <meta charset="utf-8">
        <script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
        <link rel="stylesheet" type="text/css" href="my_css.css">
        	<script type="text/javascript">
            function deleteRow(r)
            {
            	
       			if(confirm("您确定需要删除吗?"))
       			{
       				var nowChangingRow = r.parentNode.parentNode.rowIndex;
                    var table=document.getElementById("equipmentTable");
                    var nowRow=table.rows[nowChangingRow].cells;
       				location.href="${pageContext.request.contextPath}/EquipmentDeleteServlet?deviceCode="+nowRow[2].innerHTML;
       			}
            }
            function isRightTime(installationTime)
            {
                var i;
                if(installationTime.length<19)
                {
                    alert("输入的时间字符串过短!");
                    return 0;
                }
                /*下面是判断是否是数字*/
                for(i=0;i<=3;++i)if(installationTime[i]<'0'||installationTime[i]>'9')return 0;
                if(installationTime[4]!='-')return 0;
                for(i=5;i<=6;++i)if(installationTime[i]<'0'||installationTime[i]>'9')return 0;
                if(installationTime[7]!='-')return 0;
                for(i=8;i<=9;++i)if(installationTime[i]<'0'||installationTime[i]>'9')return 0;
                if(installationTime[10]!=' ')return 0;
                for(i=11;i<=12;++i)if(installationTime[i]<'0'||installationTime[i]>'9')return 0;
                if(installationTime[13]!=':')return 0;
                for(i=14;i<=15;++i)if(installationTime[i]<'0'||installationTime[i]>'9')return 0;
                if(installationTime[16]!=':')return 0;
                for(i=17;i<=18;++i)if(installationTime[i]<'0'||installationTime[i]>'9')return 0;
                if(installationTime.length>19)
                {
                    alert("输入的时间字符串过长!");
                    return 0;
                }
                
                var nian=0,yue=0,ri=0,shi=0,fen=0,miao=0;
                for(i=0;i<=3;++i)nian=nian*10+(installationTime[i]-'0');
                for(i=5;i<=6;++i)yue=yue*10+(installationTime[i]-'0');
                for(i=8;i<=9;++i)ri=ri*10+(installationTime[i]-'0');
                for(i=11;i<=12;++i)shi=shi*10+(installationTime[i]-'0');
                for(i=14;i<=15;++i)fen=fen*10+(installationTime[i]-'0');
                for(i=17;i<=18;++i)miao=miao*10+(installationTime[i]-'0');
                if(yue>12)
                {
                    alert("月份不能超过12!");
                    return 0;
                }
                if((yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)&&ri>31)
                {
                    alert("该月份日期不能超过31!");
                    return 0;
                }
                if((yue==4||yue==6||yue==9||yue==11)&&ri>30)
                {
                    alert("该月份日期不能超过30!");
                    return 0;
                }
                if(yue==2)
                {
                    if((nian%100!=0&&nian%4==0)||(nian%100==0&&nian%400==0))//闰年
                    {
                        if(ri>29)
                        {
                            alert("这一年2月日期不能超过29!");
                            return 0;
                        }
                    }
                    else
                    {
                        if(ri>28)
                        {
                            alert("这一年2月日期不能超过28!");
                            return 0;
                        }
                    }
                }
                if(shi>23)
                {
                    alert("小时不能超过23!");
                    return 0;
                }
                if(fen>59)
                {
                    alert("分钟不能超过59!");
                    return 0;
                }
                if(miao>59)
                {
                    alert("秒数不能超过59!");
                    return 0;
                }
                return 1;
            }
            function confirmInput1() 
            {
                if ( $("#equipmentName1").val() == null || $("#equipmentName1").val() == "" ) 
                {
                    alert("设备名称不能为空!");
                    $("#equipmentName1").focus();
                    return false;
                } 
                if ( $("#equipmentCode1").val() == null || $("#equipmentCode1").val() == "" ) 
                {
                    alert("设备编码不能为空!");
                    $("#equipmentCode1").focus();
                    return false;
                } 
                if ( $("#installationTime1").val() == null || $("#installationTime1").val() == "" ) 
                {
                    alert("安装时间不能为空!");
                    $("#installationTime1").focus();
                    return false;
                } 
                if ( isRightTime( $("#installationTime1").val() )==0) 
                {
                    alert("安装时间格式必须是yyyy-MM-dd HH:mm:ss");
                    $("#installationTime1").focus();
                    return false;
                } 
                
                if(confirm("您确定需要新增吗?"))
       			{
       				location.href="${pageContext.request.contextPath}/EquipmentAddServlet?siteName1="+$("#siteName1").val()
       						+"&equipmentName1="+$("#equipmentName1").val()
       						+"&equipmentCode1="+$("#equipmentCode1").val()
       						+"&equipmentCategory1="+$("#equipmentCategory1").val()
       						+"&equipmentType1="+$("#equipmentType1").val()
       						+"&installationTime1="+$("#installationTime1").val()
       						+"&equipmentStatus1="+$("#equipmentStatus1").val();	
       			}
                
                $("#siteName1").val("");
                $("#equipmentName1").val("");
                $("#equipmentCode1").val("");
                $("#equipmentCategory1").val("");
                $("#equipmentType1").val("");
                $("#installationTime1").val("");
                $("#equipmentStatus1").val("");
                return true;
            }
            function changeDispaly(r)
            {
                nowChangingRow = r.parentNode.parentNode.rowIndex;
                var table=document.getElementById("equipmentTable");
                var nowRow=table.rows[nowChangingRow].cells;
                //$("#changeTable").toggle();
                $("#changeTable").show();
                
                
                $("#siteName2").val(nowRow[0].innerHTML);
                $("#equipmentName2").val(nowRow[1].innerHTML);
                $("#equipmentCode2").val(nowRow[2].innerHTML);
                $("#equipmentCategory2").val(nowRow[3].innerHTML);
                $("#equipmentType2").val(nowRow[4].innerHTML);
                $("#installationTime2").val(nowRow[5].innerHTML);
                $("#equipmentStatus2").val(nowRow[6].innerHTML);
            }
            var nowChangingRow;
            function confirmInput2() 
            {
                if ( $("#equipmentName2").val() == null || $("#equipmentName2").val() == "" ) 
                {
                    alert("设备名称不能为空!");
                    $("#equipmentName2").focus();
                    return false;
                } 
                if ( $("#equipmentCode2").val() == null || $("#equipmentCode2").val() == "" ) 
                {
                    alert("设备编码不能为空!");
                    $("#equipmentCode2").focus();
                    return false;
                } 
                if ( $("#installationTime2").val() == null || $("#installationTime2").val() == "" ) 
                {
                    alert("安装时间不能为空!");
                    $("#installationTime2").focus();
                    return false;
                } 
                if ( isRightTime( $("#installationTime2").val() )==0) 
                {
                    alert("安装时间格式必须是yyyy-MM-dd HH:mm:ss");
                    $("#installationTime2").focus();
                    return false;
                } 
                
                if(confirm("您确定需要修改吗?"))
       			{
       				location.href="${pageContext.request.contextPath}/EquipmentEditServlet?siteName2="+$("#siteName2").val()
       						+"&equipmentName2="+$("#equipmentName2").val()
       						+"&equipmentCode2="+$("#equipmentCode2").val()
       						+"&equipmentCategory2="+$("#equipmentCategory2").val()
       						+"&equipmentType2="+$("#equipmentType2").val()
       						+"&installationTime2="+$("#installationTime2").val()
       						+"&equipmentStatus2="+$("#equipmentStatus2").val();
       			}
                $("#changeTable").hide();
                return true;
            }

            

            $(function() //初始化
            {
                $("#changeTable").toggle();
            });

            
            </script>
        </link>

    </head>

    <body>
        <table id="equipmentTable" border="1">
            <caption>设备信息</caption>
            <thead>
                <tr>
                    <th>站点名称</th><th>设备名称</th><th>设备编码</th><th>设备分类</th><th>设备类型</th><th>安装时间</th><th>设备状态</th><th>操作</th>
                </tr>
					<% Connection con;
					    Statement sql; 
					    ResultSet rs;
					    try
					    {  
					    	Class.forName("com.mysql.jdbc.Driver");
					    }
					    catch(Exception e)
					    {
					       out.println("忘记把MySQL数据库的JDBC-数据库驱动程序复制到JDK的扩展目录中");
					    }
						try 
						{ 
							String uri= "jdbc:mysql://localhost:3306/deviceinfo";
					          String user="test";
					          String password="123456";
						con=DriverManager.getConnection(uri,user,password);
					          sql=con.createStatement();
					          rs=sql.executeQuery("SELECT * FROM deviceinfo ");
					          while(rs.next())
					          { 
					            	out.print("<tr>");
					           	out.print("<td>"+rs.getString("siteName")+"</td>");
					           	out.print("<td>"+rs.getString("deviceName")+"</td>");
					           	out.print("<td>"+rs.getString("deviceCode")+"</td>");
					           	out.print("<td>"+rs.getString("deviceCategory")+"</td>");
					           	out.print("<td>"+rs.getString("deviceType")+"</td>");
					           	out.print("<td>"+rs.getString("installTime")+"</td>");
					              out.print("<td >"+rs.getString("deviceState")+"</td>");
					              out.print("</td><td><a href='#' onclick='changeDispaly(this);'>修改</a> <a href='#' onclick='deleteRow(this);'>删除</a></td></tr>");
					            out.print("</tr>") ; 
					          }
					          out.print("</table>");
					          con.close();
					    }
					    catch(SQLException e)
						{ 
					          out.print(e);
					    }
					 %> 	
            </thead>
        </table>


        <form method="post" id="addTable">
            <fieldset>
            <legend>新增</legend>
            站点名称(*):
                    <select id="siteName1" name="siteName1">  
                    <option value="昌都生态监测站">昌都生态监测站</option>
                    <option value="申扎生态监测站">申扎生态监测站</option>
                    <option value="日喀则生态监测站">日喀则生态监测站</option>
                    </select>
                    <br />  
            设备名称(*):<input type="text" value="" id="equipmentName1" name="equipmentName1"><br /> 
            设备编码(*):<input type="text" value="" id="equipmentCode1" name="equipmentCode1"><br />
            设备分类(*):
                    <select id="equipmentCategory1" name="equipmentCategory1">  
                    <option value="土壤">土壤</option>
                    <%
                    	for(int i=1;i<=10;++i)out.print("<option value="+i+">"+i+"</option>");
					%> 
                    </select>
                    <br /> 
            设备类型(*):
                    <select id="equipmentType1" name="equipmentType1">  
                    <option value="冻土检测">冻土检测</option>
                    <%
                    	for(int i=1;i<=10;++i)out.print("<option value="+i+">"+i+"</option>");
					%> 
                    </select>
                    <br /> 
            安装时间(*):<input type="text" value="" id="installationTime1" name="installationTime1"><br /><br /> 
            设备状态(*)
                    <select id="equipmentStatus1" name="equipmentStatus1">  
                    <option value="正常">正常</option>
                    <option value="异常">异常</option>
                    </select>
                    <br /> 
                <input type="button" id="button1" value="确定" onclick="confirmInput1();" />
            </fieldset>
        </form>
		<form method="POST" id="changeTable">
            <fieldset>
            <legend>修改</legend>
            站点名称(*):
                    <select id="siteName2">  
                    <option value="昌都生态监测站">昌都生态监测站</option>
                    <option value="申扎生态监测站">申扎生态监测站</option>
                    <option value="日喀则生态监测站">日喀则生态监测站</option>
                    </select>
                    <br />  
            设备名称(*):<input type="text" value="" id="equipmentName2"><br /> 
            设备编码(*):<input type="text" value="" id="equipmentCode2"><br />
            设备分类(*):
                    <select id="equipmentCategory2">  
                    <option value="土壤">土壤</option>
                    <%
                    	for(int i=1;i<=10;++i)out.print("<option value="+i+">"+i+"</option>");
					%> 
                    </select>
                    <br /> 
            设备类型(*):
                    <select id="equipmentType2">  
                    <option value="冻土检测">冻土检测</option>
                    <%
                    	for(int i=1;i<=10;++i)out.print("<option value="+i+">"+i+"</option>");
					%> 
                    </select>
                    <br /> 
            安装时间(*):<input type="text" value="" id="installationTime2"><br /><br /> 
            设备状态(*)
                    <select id="equipmentStatus2">  
                    <option value="正常">正常</option>
                    <option value="异常">异常</option>
                    </select>
                    <br /> 
                <input type="button" id="button2" value="确定" onclick="confirmInput2();" />
            </fieldset>
        </form>


    </body>

</html>

EquipmentAddServlet

package myServlet;
import java.io.*;
import java.sql.*;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/EquipmentAddServlet")
public class EquipmentAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public EquipmentAddServlet() 
    {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		Connection con = null;
	    Statement sql; 
	    ResultSet rs;
	    try
	    {  
	    	Class.forName("com.mysql.jdbc.Driver");
	    }
	    catch(Exception e)
	    {
	    	
	    }
		try 
		{ 
			String uri= "jdbc:mysql://localhost:3306/deviceinfo";
		    String user="test";
		    String password="123456";
			con=DriverManager.getConnection(uri,user,password);
		    sql=con.createStatement();
			request.setCharacterEncoding("utf-8");

			String addOperate = "insert into deviceinfo(siteName,deviceName,deviceCode,deviceCategory,deviceType,installTime,deviceState) values (?,?,?,?,?,?,?)";
	        PreparedStatement preparedStatement = con.prepareStatement(addOperate);
	        preparedStatement.setString(1, request.getParameter("siteName1"));
	        preparedStatement.setString(2, request.getParameter("equipmentName1"));
	        preparedStatement.setString(3, request.getParameter("equipmentCode1"));
	        preparedStatement.setString(4, request.getParameter("equipmentCategory1"));
	        preparedStatement.setString(5, request.getParameter("equipmentType1"));
	        preparedStatement.setString(6, request.getParameter("installationTime1"));
	        preparedStatement.setString(7, request.getParameter("equipmentStatus1"));
	        preparedStatement.executeUpdate();
	        
		}
	    catch(SQLException e)
		{ 
	         
	    }

		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		response.sendRedirect(request.getHeader("referer"));
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

EquipmentDeleteServlet

package myServlet;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/EquipmentDeleteServlet")
public class EquipmentDeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public EquipmentDeleteServlet() 
    {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		request.setCharacterEncoding("utf-8");
		
		String deviceCode=request.getParameter("deviceCode");
		
		Connection con = null;
	    Statement sql; 
	    ResultSet rs;
	    try
	    {  
	    	Class.forName("com.mysql.jdbc.Driver");
	    }
	    catch(Exception e)
	    {
	    	
	    }
		try 
		{ 
			String uri= "jdbc:mysql://localhost:3306/deviceinfo";
		    String user="test";
		    String password="123456";
			con=DriverManager.getConnection(uri,user,password);
		    sql=con.createStatement();
			request.setCharacterEncoding("utf-8");
			
			
			String deleteOperate = "delete from deviceinfo where deviceCode =?";
	        PreparedStatement preparedStatement = con.prepareStatement(deleteOperate);
	        preparedStatement.setString(1,deviceCode);
	        preparedStatement.executeUpdate();
		}
	    catch(SQLException e)
		{ 
	         
	    }
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		response.sendRedirect(request.getHeader("referer"));
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

EquipmentEditServlet

package myServlet;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/EquipmentEditServlet")
public class EquipmentEditServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public EquipmentEditServlet() 
    {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		request.setCharacterEncoding("utf-8");
			
		Connection con = null;
	    Statement sql; 
	    ResultSet rs;
	    try
	    {  
	    	Class.forName("com.mysql.jdbc.Driver");
	    }
	    catch(Exception e)
	    {
	    	
	    }
		try 
		{ 
			String uri= "jdbc:mysql://localhost:3306/deviceinfo";
		    String user="test";
		    String password="123456";
			con=DriverManager.getConnection(uri,user,password);
		    sql=con.createStatement();
			request.setCharacterEncoding("utf-8");
			
	        
	        String changeOperate = "update deviceinfo set siteName = ? ,deviceName = ?,deviceCode=?,deviceCategory=?,deviceType=?,installTime=?,deviceState=? WHERE deviceCode =?";
	        PreparedStatement preparedStatement = con.prepareStatement(changeOperate);
	        preparedStatement.setString(1, request.getParameter("siteName2"));
	        preparedStatement.setString(2, request.getParameter("equipmentName2"));
	        preparedStatement.setString(3, request.getParameter("equipmentCode2"));
	        preparedStatement.setString(4, request.getParameter("equipmentCategory2"));
	        preparedStatement.setString(5, request.getParameter("equipmentType2"));
	        preparedStatement.setString(6, request.getParameter("installationTime2"));
	        preparedStatement.setString(7, request.getParameter("equipmentStatus2"));
	        preparedStatement.setString(8,request.getParameter("equipmentCode2"));
	        preparedStatement.executeUpdate();
		}
	    catch(SQLException e)
		{ 
	         
	    }
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		response.sendRedirect(request.getHeader("referer"));
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

标签:调用,return,val,数据库,request,preparedStatement,jsp,import,servlet
From: https://www.cnblogs.com/wljss/p/17456098.html

相关文章

  • 【Windows】TreeSoft数据库管理系统 TreeDMS 和 TreeNMS
    官方地址:http://www.treesoft.cn/dms.html#learningTreeSoft数据库管理系统TreeDMS支持MySQL,MariaDB,Oracle,PostgreSQL,SQLServer,DB2,MongoDB,Hive,SAPHANA,Sybase,Caché,Informix,Impala,ElasticSearch,clickHouse,cassandra,AmazonRedshift,达梦DM,金仓Kin......
  • Ofbiz安装, 调试 和 迁移数据库
    如何将OFBIZ(opentaps)默认数据库迁移至mysql[url]http://blog.sina.com.cn/s/blog_4a4820f9010008oo.html[/url]OFBiz12.4数据库转变成Mysql[url]http://wxynxyo.iteye.com/blog/1955648[/url]Ofbiz的安装[url]http://tomhat.iteye.com/blog/1330491[......
  • 数据库管理工具远程连接MySQL实例服务失败Host ... is not allowed to connect to thi
    MySQL社区版数据库8.0版本添加账号、分配权限、删除账号MySQL安全连接失败问题排查......
  • oracle一个不可不懂的数据库,来一波视频
    Spring常用注解redis视频集合,看完这些别说不会redis第0讲.开山.wmv第1讲.oracle基础介绍.wmv第2讲.oralce安装.wmv第3讲.oracle的基本使用.wmv第4讲.oracle用户管理(1).wmv第5讲.oracle用户管理(2).wmv第6讲.oracle表的管理(1).wmv第7讲.oracle表的管理(2).wmv第8讲.oracle表查询(1......
  • 虚谷数据库语法问题
    使用V11版本1、插入多条数据问题需要把插入数据的中间逗号去掉你图上的这个用法我们在v12的发行版上已经支持了,你那边报错是因为你现在使用的是v11吧INSERTINTOCLASS(CLASSID,CLASSNAME)VALUES(333,'666')(777,'888');2、连接字符串concat函数CONCAT(S1,s2)concat的参数......
  • 4、数据库:MySQL部署 - 系统部署系列文章
          MySQL数据库在其它博文中有介绍,包括学习规划系列。今天就讲讲MySQL的部署事情。一、先下载MySQL数据库;到下面这个网址去下载数据库,这里下载的社区版:https://dev.mysql.com/downloads/installer/二、安装数据库;打开EXE文件;       ......
  • Django迁移数据库编码错误
    mysql编码错误问题出现在用django的admin组件向数据库添加数据时发生的编码错误(1366,"Incorrectstringvalue:'\\xE5\\x8D\\x83\\xE7\\x89\\x9B...'forcolumn'title'atrow1")以为django可以进行数据库的迁移,但是你还是要去手动在mysql中创建一个数据库名,所以在创建的......
  • Spring RestTemplate 调用天气预报接口乱码的解决
    SpringRestTemplate调用天气预报接口可能遇到中文乱码的问题,解决思路如下。问题出现我们在网上找了一个免费的天气预报接口http://wthrcdn.etouch.cn/weather_mini?citykey=101280601。我们希望调用该接口,并将返回的数据解析为JSON格式。核心业务逻辑如下:privateWeatherRespo......
  • c++函数调用压栈过程
    c++函数调用,栈内情况如下图所示:首先主函数将被调函数所需参数从右至左压入栈中然后再将主函数地址即返回地址EIP压入栈中再将主函数栈基址EBP压入栈中,此时构造被调函数栈,将当前ESP值mov给EBP,即被调函数栈从此处开始上图ida反汇编代码,可以看到对变量的使用,参数(argc,argv,env......
  • property 用于以访问属性的方式调用函数
    property是Python内置的功能,常用来修饰类方法,用于以访问属性的方式调用函数。描述符对象为了能够实现访问属性就调用某个函数,这里将利用描述符对象作为本文的实现起点,当某个类定义了__get__方法后,通过其方法名称可以直接调用__get__proptery主要依赖于描述符的机制。p......