我有一个静态HTML文档,它将表单发布到Java servlet。然后,servlet获取表单的值并将它们转发到SQL数据库。但是,问题是当我确定该值不为null时,数据库声明值为null。
以下是表格:
<form method="post" action="Handler" target="_blank" enctype="multipart/form-data">
<div class="row uniform 50%">
<div class="6u 12u(mobilep)">
<input type="text" name="name" id="name" placeholder="Username" />
</div>
<div class="6u 12u(mobilep)">
<input type="email" name="email" id="email" placeholder="Email" />
</div>
</div>
<div class="row uniform 50%">
<div class="6u 12u(mobilep)">
<input type="password" name="password" id="password" placeholder="Password" />
</div>
</div>
<div class="row uniform 50%">
<div class="12u">
<textarea name="bio" id="bio" placeholder="Describe yourself" maxlength="200" rows="3"></textarea>
</div>
</div>
<div class="row uniform">
<div class="12u">
<ul class="actions align-center">
<li><input type="submit" value="Send Message" /></li>
</ul>
</div>
</div>
</form>
这是servlet代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Connection c = null;
PrintWriter op = response.getWriter();
response.setContentType("text/html");
try{
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection(DB_URL, "root", "root");
}catch(Exception s){
op.println("<html>");
op.println("<body><h1><strong>"+ "Error: "+ s +"</strong></h1></body>");
op.println("</html>");
}
String username = request.getParameter("name");
String email = request.getParameter("email");
String pass = request.getParameter("password");
String bio = request.getParameter("bio");
String proPicName = "false";
Long time = System.currentTimeMillis();
String sysTime = time.toString();
Statement stmt = null;
try{
stmt = c.createStatement();
stmt.execute("USE dvlpr;");
stmt.execute("INSERT INTO user_tbl (username, email, password, bio, pro_pic, last_on, date_created)" + " VALUES ("+username+", "+email+", "+pass+", "+bio+", "+proPicName+", "+sysTime+", "+sysTime+");");
op.println("<html>");
op.println("<body><h1><strong>Connection made! Username: " + username+ " Email: " + email+ " Your account has been created. We'll keep your password private, too. Thanks!</strong></h1>");
op.println("</body>");
op.println("</html>");
}catch(Exception s){
op.println("<html>");
op.println("<body><h1><strong>"+ "Error: "+ s +"</strong></h1></body>");
op.println("</html>");
}
}
错误代码如下:
错误:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'username'不能为null
为什么返回null?
答案 0 :(得分:1)
参数很好。由于语法原因,查询未正确执行。值本身需要用引号括起来,所以看起来应该是这样的:
VALUES('"+username+"',....
正如您所看到的,我在双引号之前添加了一个'
,之后添加了一个'
,因此VALUES('MyUsername', 'MyPassword',...);
将成为结果字符串的一部分。
就像在任何其他SQL插入中一样,您可以执行stmt.execute("USE dvlpr; INSERT INTO....");
。
此外,您可能希望使用一个执行方法而不是2,因此它将是:
+
此外," VALUES
class Choices(models.Model):
choices = models.CharField(max_length=70)
def __unicode__(self):
return self.choices
class UserProfile(models.Model):
user = models.OneToOneField(MyUser)
about_me = models.TextField(max_length=2000, null=True, blank=True)
country = models.CharField(max_length=100,null=True, blank=True)
choices = models.ManyToManyField(Choices, blank=True)