Here is my code.
**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+')");
%>
My database..
I am running this code in netbeans 8.0. I got the error log as
It shows that data is too long for blood group but i just given 'A+' in that textbox. Whats going wrong. As I am new to JSP please help me. Thanks in advance.
Try using PreparedStatement instead to avoid String concatenation mistakes.
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();
查询必须以int i = st.executeUpdate( “插入血液(Name,Rollno,BloodGroup)值('” + name +“','” + roll +“','” + group +“')” )的形式执行;
The Exception:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long..
is says, the data you trying to insert into BloodGroup
column is larger then you have specified column data size when creating table.
So fix is:
change size of BloodGroup
column like:
ALTER TABLE blood MODIFY BloodGroup varchar(some value more than 5);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.