Java脚本数据库插入

时间:2016-11-24 17:43:55

标签: javascript html ajax jsp

我有一个数据表,我添加了一个新行并将其提交给JSP以便插入数据库。我在JSP中遇到以下错误。这段代码有什么问题。

function addRow(table,row)
    {
         var input = $('input', row);
         var params = "id="+input[0].value+"&name="+input[1].value+"&title="+input[2].value+"&code="+input[3].value+"&email="+input[4].value+"&band="+input[5].value+"&location="+input[6].value+"&manager="+input[7].value+"&school="+input[8].value+"&major="+input[9].value+"&years="+input[10].value+"&edu="+input[11].value+"&skills="+input[12].value;
         var settings = {
                  "async": true,
                  "crossDomain": true,
                  "timeout":0,

                  "url": "insertdb.jsp?"+params,
                  "method": "POST",

                  "headers": {
                    "content-type": "application/json",
                    "cache-control": "no-cache"

                  },
                  "processData": false
                }
         $.ajax(settings).done(function (response) {
                var json = JSON.parse(response);
                if(json.responseHeader.status == "0")
                {
                alertify.success("Sucessfully modified the schema field");
                location.reload();
                //alertify.set({ delay : 5000 });

                }
                else
                {
                alertify.error("Error! Please try again");
                }
                  console.log(response);
                });
    }

JS - 单击Save按钮提交给JSP的Ajax代码

<sql:update dataSource="${myDS}" var="result">
        insert into emp_profile1 (EMPID, NAME, JOB_TITLE, JOB_CODE, EMAIL, BAND, LOCATION, MANAGER, SCHOOL, MAJOR, YEARS_EXP, EDUCATION, SKILLS) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);

    <sql:param value="${param.id}" />
    <c:out value="${param.name}"></c:out>
    <sql:param value="${param.name}" />
    <sql:param value="${param.title}" />
    <sql:param value="${param.code}" />
    <sql:param value="${param.email}" />
    <sql:param value="${param.band}" />
    <sql:param value="${param.location}" />
    <sql:param value="${param.manager}" />
    <sql:param value="${param.school}" />
    <sql:param value="${param.major}" />
    <sql:param value="${param.years}" />
    <sql:param value="${param.edu}" />
    <sql:param value="${param.skills}" />
</sql:update>
response.setContentType("text/html");
<c:if test="${result>=1}">
response.getWriter().print("{'success': true}");

</c:if>

我的JSP

INFO - The node is registered to the hub and ready to use
INFO - SessionCleaner initialized with insideBrowserTimeout 0 and clientGoneTimeout 1800000 polling every 180000
INFO - Executing: [new session: Capabilities [{browserName=safari, version=, platform=ANY}]])
INFO - Creating a new session for Capabilities [{browserName=safari, version=, platform=ANY}]
INFO - Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
INFO - Detected dialect: OSS
INFO - Done: [new session: Capabilities [{browserName=safari, version=, platform=ANY}]]
INFO - Executing: [implicitly wait: 15000])
INFO - Done: [implicitly wait: 15000]
INFO - Executing: [get: TEST_PAGE_URL]
INFO - Done: [get: TEST_PAGE_URL]
INFO - Executing: [implicitly wait: 15000])
INFO - Done: [implicitly wait: 15000]
INFO - Executing: [set window position])
WARN - Exception thrown
org.openqa.selenium.NoSuchWindowException: A request to use a window could not be satisfied because the window could not be found. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 15 milliseconds

after some system information :

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
    at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteWindow.setPosition(RemoteWebDriver.java:852)
    at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWindow.setPosition(EventFiringWebDriver.java:628)
    at org.openqa.selenium.remote.server.handler.SetWindowPosition.call(SetWindowPosition.java:53)
    at org.openqa.selenium.remote.server.handler.SetWindowPosition.call(SetWindowPosition.java:27)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:176)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
WARN - Exception: A request to use a window could not be satisfied because the window could not be found. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 15 milliseconds

1 个答案:

答案 0 :(得分:0)

SQL错误消息为我们提供了足够的信息,问题可能是:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   第4行的'[object HTMLInputElement]'附近有根本原因

重要信息是:

  

'[object HTMLInputElement]'

这样的字符串不应该在sql查询中。而不是这应该是以sql编写的名称 - 值对。

所以问题在于html表单的序列化。 您可以在行中手动进行序列化:

var params = "id="+input[0].value+"&name="+input[1].value+"&title="+input[2].value+"&code="+input[3].value+"&email="+input[4].value+"&band="+input[5].value+"&location="+input[6].value+"&manager="+input[7].value+"&school="+input[8].value+"&major="+input[9].value+"&years="+input[10].value+"&edu="+input[11].value+"&skills="+input[12].value;

我建议通过调用:

来检查创建的params
console.log(params);

更好的方法是使用jQuery serialize()

相关问题