0

我是 JSP 的新手。我正在创建一个显示通知批准的表。它的功能是显示管理员是否批准记录。但是,当我单击“接受”按钮时,没有任何变化(页面和数据库都保持为 0)。我的代码有错误吗??请帮忙。先感谢您。

我的 JSP 文件中的 Javascript:

<script language="JavaScript">
function submitaccept(){
    String connectionURL = "jdbc:mysql://localhost/ams";
    Connection connection =null;
    PreparedStatement pstatement = null;
    var sqlstr = "UPDATE notification SET ack_flag = 1 WHERE notification_id = 001;";
    connection = DriverManager.getConnection(connectionURL, "root", "");
    pstatement =connection.prepareStatement(sqlstr);
    pstatement.executeUpdate();
    location.reload(true);
}

我的JSP文件的调用函数:

    <c:choose>
        <c:when test="${row.ack_flag=='0'}">

        <a href="javascript:submitaccept()" >
            Approve</a> | 

        <a href="notification.jsp" > 
            Reject </a>
        </c:when>

        <c:when test="${row.ack_flag=='1'}">
            Approved
        </c:when>
        <c:otherwise>
            Rejected
        </c:otherwise>
    </c:choose>
4

5 回答 5

0

您不需要;在更新声明中提供它会认为价值001;001删除它

var sqlstr = "UPDATE notification SET ack_flag = 1 WHERE notification_id = 001";
于 2013-09-04T07:38:28.867 回答
0

你的连接有问题。您正在使用 javascript 连接到数据库,并且您已经编写了 JDBC 的连接代码。

您可以在此处找到如何使用 javascript 连接到数据库服务器

于 2013-09-04T07:39:28.677 回答
0

虽然您已经收到了对问题 ID 的回答,但想指出您是从 Java 脚本代码中进行 DB 连接的。这意味着包括您的连接参数的代码将被发送到可能不是您想要存档的客户端。

而是确保有一个 Servlet 被调用并在那里做你的数据库工作。您的 JSP 通常应该只获取数据,将其交给 servlet 并重新接收数据以显示它,但不包含诸如数据库连接之类的业务逻辑代码。

虽然我还没有验证这个例子:http ://www.java-samples.com/showtutorial.php?tutorialid=619我认为它可以让你尽可能快地归档你想做的事情并且没有你的连接传递给客户端的参数。

于 2013-09-04T07:41:01.213 回答
0

我从来没有做过这样的事情,但我想每当我尝试使用像jsp或php这样的javascript和服务器脚本时

我总是使用ajax

所以我的概念是这样的

每当您按下按钮时,它都会调用 javascript 函数,如您的情况“submitaccept()”

如果我是你,我会尝试像这样使用 ajax

function submitaccept(){
    //code for getting the notificationid you would like to update
    notifId = document.getElementById('..').value;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var x = xmlhttp.responseText;

            //whatever you want to do after your update is done , where x is the response from updateNotif.jsp

        }
    }
    xmlhttp.open("GET","updateNotif.jsp?notif="+notifId,false);
    xmlhttp.send();
}

然后,我将制作 updateNotif.jsp

request.getResponse("通知")

并使用您的代码更新它

希望它可以帮助你:)

于 2013-09-04T07:42:37.443 回答
0

我不知道你的 JSP ,它与我的风格不同,所以你需要从我的改变它

在这里,让我用我的风格为你做一个新的

试着把它复制到你的新项目中,学习这个概念,这样你就可以解决你的问题

我给你做两页

索引.jsp

<html>
<head>
<script> function change(ackFlag){
    var notifId = document.getElementById('myNotifId').value;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var x = xmlhttp.responseText;
            document.getElementById('myNotification').value = x;
        }
    }
    xmlhttp.open("GET","updateNotif.jsp?af="+ackFlag+"&notif="+notifId,false);
    xmlhttp.send();  } </script>
</head>
<body>
    <% String notifID = "001"; String ackFlag = "1"; %>

    <input type='hidden' value='<% out.print(notifID); %>' id='myNotifId' />
    <input id='myNotification' value='<% if(ackFlag.equals("1"))out.print("Accepted");else out.print("Rejected") %>' />
    <input type='button' value='Accept' onclick='change(1)' />
    <input type='button' value='Reject' onclick='change(0)' />
</body>
</html>

updateNotif.jsp

<%
    String notifID = request.getParameter("notif");
    String af = request.getParameter("af");

    //this is the start of your code, i just copied from your code 

    String connectionURL = "jdbc:mysql://localhost/ams";
    Connection connection =null;
    PreparedStatement pstatement = null;
    var sqlstr = "UPDATE notification SET ack_flag = "+af+" WHERE notification_id = " + notifID;
    connection = DriverManager.getConnection(connectionURL, "root", "");
    pstatement =connection.prepareStatement(sqlstr);
    pstatement.executeUpdate();

    //this is the end of your code, once again, this is from your code

    if(af.equals("1"))
        out.print("Accepted");
    else
        out.print("Rejected");
%>

哦,顺便说一句,我还没有编译它,所以如果你对我的代码有任何问题,请再次发布在这里,但我很确定它会起作用,所以玩得开心:)

于 2013-09-05T09:19:43.453 回答