Java MySQL和JSP数据库与Glassfish服务器4的连接

Java MySQL和JSP数据库与Glassfish服务器4的连接,java,mysql,jsp,glassfish,Java,Mysql,Jsp,Glassfish,这是我的密码 **insert.html** <html> <head> <title>Donate Blood !</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, init

这是我的密码

    **insert.html**
        <html>
            <head>
                <title>Donate Blood !</title>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
            </head>
            <body>
                <form action="store.jsp" method="POST">
                    Name: <input type="text" name="nam"><br>
                    Rollno: <input type="text" name="rno"><br>
                    Blood Group: <input type="text" name="grp"><br>
                    <input type="submit" value="Add me !">
                </form>
            </body>
        </html>

**Store.jsp**
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
            String name = request.getParameter("nam");
            String roll = request.getParameter("rno");
            String group = request.getParameter("grp");

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/blood","root","");
            Statement st = con.createStatement();
            int i=st.executeUpdate("insert into blood(Name,Rollno,BloodGroup) values('+name+','+roll+','+group+')");
        %>
**insert.html**
献血!
名称:
罗尔诺:
血型:
**Store.jsp**
我的数据库。。

我在netbeans 8.0中运行此代码。我得到了错误日志


它显示数据对于血型来说太长了,但我只是在那个文本框中给出了“A+”。出什么事了。由于我是JSP新手,请帮助我。提前谢谢

查询必须按以下方式执行:
int i=st.executeUpdate(“插入血液(名称、卷号、血型)值(“+Name+”、“+roll+”、“+group+”)”)”

尝试改用PreparedStatement以避免字符串连接错误

PreparedStatement st = con.prepareStatement("insert into blood(Name,Rollno,BloodGroup) values(?,?,?)");
st.setString(1, name);
st.setString(2, roll);
st.setString(3, group);
st.executeUpdate();
例外情况:

com.mysql.jdbc.MysqlDataTruncation:数据截断:数据太长..


也就是说,您试图插入到
BloodGroup
列中的数据大于您在创建表时指定的列数据大小

因此,解决方案是:

更改
BloodGroup
列的大小,如:

ALTER TABLE blood MODIFY BloodGroup varchar(some value more than 5);

您试图插入到
BloodGroup
列中的数据很大,那么您在创建表时指定了大小。请检查我的答案以解决此问题。