Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的记录没有插入数据库?_Java_Mysql_Jsp_Servlets - Fatal编程技术网

Java 我的记录没有插入数据库?

Java 我的记录没有插入数据库?,java,mysql,jsp,servlets,Java,Mysql,Jsp,Servlets,我尝试使用Textbox将数据插入数据库,有时它只使用Internet Explorer将数据插入数据库,而不使用Mozilla firefox 真奇怪 我的数据有时会上传,但只使用Internet Explorer,有时会停止工作。请帮帮我这是什么意思 这是我的密码: <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page

我尝试使用Textbox将数据插入数据库,有时它只使用Internet Explorer将数据插入数据库,而不使用Mozilla firefox

真奇怪

我的数据有时会上传,但只使用Internet Explorer,有时会停止工作。请帮帮我这是什么意思

这是我的密码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
<script type="text/javascript" >
$(function() {
    $(".update_button").click(function() {

    var boxval = $("#content").val();
    var dataString = 'content='+ boxval;

    if(boxval=='')
    {
        alert("Please Enter Some Text");
    }
    else
    {
    $.ajax({
        type: "POST",
        url: "demo.jsp",
        data: dataString,
        cache: false
        });
    }
    });
});
</script>

</head>
<body>
    <div>
        <form method="post" name="form" action="">
            <h3>What are you doing?</h3>
            <textarea name="content" id="content" ></textarea><br />
            <input type="submit" value="Update" name="submit" class="update_button" />
        </form>
    </div>

    <%
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/poststatus", "root", "1234");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("select * from messages order by msg_id desc");
            while(rs.next()){
                String msg=rs.getString("msg");
    %>
            <ol id="update" class="timeline"><%=msg %></ol>
<%
            }
    }
    catch(Exception e){
        e.printStackTrace();
    }
%>  

</body>
</html>
是的,我可以从数据库中获取数据,正如你们看到的我的代码一样。但我无法插入数据

当然,我们将非常感谢您的帮助

编辑:

这是我创建的表:

create database if not exists poststatus;
use poststatus;

create table messages(
    msg_id INT auto_increment,
    msg VARCHAR(1000) NOT NULL,
    primary key(msg_id)
);

发送AJAX请求的update按钮也是表单的submit按钮,因此当您单击它时,它开始提交表单,该表单将从当前页面导航,在运行AJAX请求之前切断代码

将单击处理程序的开头修改为:

$(".update_button").click(function(e) {
    e.preventDefault();
调用该事件将阻止表单提交,从而使AJAX函数有机会运行

此外,Java代码中存在严重问题;由于该值是通过字符串连接插入的,因此它会创建一个明显的SQL注入漏洞,如果有人无意中键入撇号,该漏洞也将不起作用。更改:

Statement st=con.createStatement();
int i=st.executeUpdate("insert into messages(msg) values('"+content+"')");
致:


通过success:failure:和console.log获取ajax响应..我确实使用了。。。但它什么也没说…:将url的响应返回到ajax,并更新您使用$.ajax调用尝试的代码。。
Statement st=con.createStatement();
int i=st.executeUpdate("insert into messages(msg) values('"+content+"')");
PreparedStatement st = con.prepareStatement("insert into messages(msg) values(?)");
st.setString(1, content);
int i = st.executeUpdate();