单击特定按钮时在DataBase中设置颜色

时间:2016-10-31 05:52:56

标签: javascript java html sql

我有两个按钮:一个点击设置颜色为红色,另一个设置颜色为绿色 我想将该值存储在数据库中,点击该按钮 之后,我想从数据库中获取此颜色,并希望使用<td bgcolor='' align="center" >标记中的get属性设置背景标记。 我怎么能轻易做到?

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
         </br>
        <h3> Please select your location </h3>
            <table border="1" width="50%" height="500" align="center"  >
            <tbody>
                <tr>           
                    <td bgcolor='' align="center" >
                        <jsp:useBean id="bean" scope="session" class="org.functions" />
                        <input type="submit" value="Park Here" color='<jsp:setProperty name="bean" property="color" value="red" />' name="en" onclick="this.parentNode.style.backgroundColor='red'" />
                        <input type="submit" value="Cancel" color='<jsp:setProperty name="bean" property="color" value="green" />' name="en" onclick="this.parentNode.style.backgroundColor='green'" />

                    </td>
<tr>
                    <form action="hello.jsp">
                    <td colspan="2" align="center">
                        <input type="submit" value="Ok" style="width:100px"  />
                    </td>
                    </form>
                </tr>

            </tbody>
        </table>                     
    </body>
</html>

<%-- 
    Document   : hello
    Created on : Jun 19, 2016, 5:49:58 PM
    Author     : 
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1 align="center">This is hello.jsp </h1>
        <jsp:useBean id="mybean" scope="session" class="org.Second" />

        <jsp:setProperty name="mybean" property="name" />
        <jsp:setProperty name="mybean" property="mail" />
        <jsp:useBean id="bean" scope="session" class="org.functions" />

        <%
                   // Second scnd = new Second();
                       int a= mybean.storevalue(); 
                       int b= bean.storeColor();                               
                 %>
                  <jsp:setProperty name="bean" property="color"  />
                  <jsp:setProperty name="bean" property="id"  />
        <table border="1" align="center">
            <thead>  
            <tr>
                <td>From Values</td>
            </tr>
            </thead>
        </table>
        <form action="ExampleDate.jsp" method="get">

            Start Date 
            <input type="date" name="date" id="d"  >

            Start Date 
            <input type="time" name="time"  >       
             <br></br>
        <input type="submit" value="hmmm" name="vxc" />
        </form>      
    </body>           
</html>

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author 
 */
public class functions {
    private String color;
    private String id;

    /**
     * @return the color
     */

    public String getColor() {
        return color;
    }

    /**
     * @param color the color to set
     */
    public void setColor(String color) {
        this.color = color;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    public int storeColor() throws ClassNotFoundException, SQLException{
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        String url = "jdbc:derby://localhost:1527/sample;create=true; user=app; password=app";
        Connection con = DriverManager.getConnection(url);
        PreparedStatement ps = con.prepareStatement("INSERT INTO COLORTABLE(en, id) VALUES(?, ?)");
        ps.setString(1, color);
        ps.setString(2, id);
        int a = ps.executeUpdate();
        if(a==1){
            return a;
        }else{
            return a;
        }
    }        
}

3 个答案:

答案 0 :(得分:0)

你需要在前端

这样做

在后端,您只能存储颜色的值

答案 1 :(得分:0)

一个简单的方法是使用jQuery。您可以使用addClass()例程。如果要恢复上一个按钮的初始值,还可以实现该功能。例如:

//Restore all values
$("input").removeClass();
$("input").addClass("default");

//Set class that has the highlight color
$("#button2").addClass("highlight");

使用此HTML:

<input type="button" id="button1"/>
<input type="button" id="button2"/>

这个CSS:

.default { background-color: blue;}
.highlight { background-color: yellow;}

将onClick添加到元素以调用正确的函数。

答案 2 :(得分:0)

所有事情都是通过略微改变的逻辑完成的,但现在我想要的时候&#34; Park Here&#34;点击它外部区域应该是红色,当&#34;取消&#34;点击它应该是绿色但它显示内部颜色红色和绿色。是否有任何方法可以将背景颜色更改为红色和绿色。 代码在这里

<tbody>
  <tr>
       <td bgcolor="green" align="center" ><select name="place1"  >
       <option onclick="this.parentNode.style.backgroundColor='red' "> Park Here</option>
       <option onclick="this.parentNode.style.backgroundColor='green' "> Cancel </option>
       </select></td>

  </tr>
  </tbody>

这是输出,但我希望外部颜色应该是红色或绿色,就像这个标签所做的那样。 有什么方法吗?请告诉我 I want outer color should be red on click not inner