我得到以下异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 专栏' userid'不能为空
<%@page import="java.io.InputStream"%>
<%@page import="conPackage.MyConnection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.util.Enumeration"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="java.io.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
upload file page...!!!
<%
String rtempfile = File.createTempFile("temp","1").getParent();
MultipartRequest multi = new MultipartRequest(request,rtempfile, 15*1024*1024); // maximum size 15 MB
Enumeration files = multi.getFileNames();
String a=request.getParameter("uname");
String f=request.getParameter("email");
String b=request.getParameter("date");
String c=request.getParameter("gender");
String st="insert into documents(userid,filename, type,date,type1,content, gender) values (?,?,?,?,?,?,?)";
PreparedStatement psmt=MyConnection.getConnection().prepareStatement(st);
String name="";
String fileExtesion="";
File ff =null;
FileInputStream fin =null;
while (files.hasMoreElements())
{
name=(String)files.nextElement();
ff = multi.getFile(name);
fileExtesion = ff.getName().substring(ff.getName().lastIndexOf("."));
boolean fileAllowed = fileExtesion.equalsIgnoreCase(".txt")||
fileExtesion.equalsIgnoreCase(".pdf")||
fileExtesion.equalsIgnoreCase(".doc")||
fileExtesion.equalsIgnoreCase(".docx")||
fileExtesion.equalsIgnoreCase(".xls")||
fileExtesion.equalsIgnoreCase(".xlsx");
if((ff!=null)&&fileAllowed)
{
try
{
fin=new FileInputStream(ff);
psmt.setString(1, a);
psmt.setString(2, ff.getName());
psmt.setString(3, fileExtesion);
psmt.setString(4, b);
psmt.setString(5, f);
psmt.setBinaryStream(6,(InputStream)fin, (int)(ff.length()));
psmt.setString(7, c); // pass the user name or id
boolean sss = psmt.execute();
out.print("uploaded successfully..");
out.print("<br/> Go to <a href='downloadfile.jsp'>Download</a> page");
}
catch(Exception e)
{
out.print("Failed due to " + e);
}
finally
{
// next statement is must otherwise file will not be deleted from the temp as fin using f.
// its necessary to put outside otherwise at the time of exception file will not be closed.
fin.close();
ff.delete();
}
}
else
{
out.print("Please select the correct file...");
}// end of if and else
}// end of while
MyConnection.CloseConnection();
%>
<a href="sub.jsp">Home Page</a>
</body>
</html>
我的HTML代码是:
<%--
Document : sub
Created on : Feb 20, 2015, 12:33:09 PM
Author : hcljava5
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>request</title>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="css/calendarview.css">
<script src="js/calendarview.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
<script type="text/javascript" src="js/prototype.js"></script>
</head>
<body>
<div class="header"></div>
<div class="menu">
<li><a href="sub.jsp">HOME</a></li>
</div>
<div class="content">
<center>
<b style="text-transform:uppercase; font-size:18px; color:#A31919;">Upload your Information</b></center><br>
<fieldset class="login">
<legend></legend>
<form action="uploadfile.jsp" method="post" enctype="multipart/form-data">
<center><table>
<tr>
<th>User ID</th>
<td><input type="text" name="uname" required class="textbox"></td>
</tr>
<tr>
<th>Title</th>
<td><input type="text" name="email" required class="textbox"></td>
</tr>
<tr>
<th>Date</th>
<td> <input type="date" name ="date" required class="textbox"></td>
</tr>
<tr>
<td><b>Select Type</b> </td>
<td>
<select name="gender" required class="textbox">
<option value="Publications">Publications</option>
<option value="Workshops">Workshops</option>
<option value="conferences">conferences</option>
</select>
</td>
</tr>
<tr>
<th>Upload File</th>
<td><input type="file" name="filename" id="filename" accept=".txt, application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/msword" ></td>
</tr>
<tr>
<td><input type="submit" value="SUBMIT" class="myButton" style="position:relative; left:130px;"></td>
<td><input type = "reset" class="myButton" style="position:relative; left:150px;"></td>
</tr>
</table> </center>
</form>
</div>
<div class="footer"></div>
</body>
</html>
我的数据库表是在我的sql中创建的