点击提交按钮
时出现异常我的所有代码都运行良好,但是当我添加上传图片的代码时 那么这个错误就会发生
在这些位置我完成了更改 1.在html中 enctype =" multipart / form-data" 新添加 2.在jsp中添加了项目derectory中的保存图像
给我一些解决方案......
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Column' name'不能为空
我的HTML格式
<form method="post" enctype="multipart/form-data" >
<div class="intro">
<h1>Setup your Details</h1>
<h2></h2>
</div>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/slambook","root",
"");
Statement st = con.createStatement();
String user = (String) session.getAttribute("userid");
String qry = "select * from userdetails where `userid`='"+user+"'";
ResultSet result = st.executeQuery(qry);
while(result.next()){
//id = result.getInt("id");
%>
<input type="text" placeholder="Name" name="name" value="<%=result.getString("name")%>" required/>
<input type="number" placeholder="Contact No." name="cno" value="<%=result.getString("cno")%>" required/>
Choose File : <input type="file" name="file" required>
<textarea placeholder="Address" name="address" rows="4"><%=result.getString("address")%></textarea>
<input type="date" name="dob" value="<%=result.getString("dob")%>"/>
<input type="text" placeholder="Dream" name="dream" value="<%=result.getString("dream")%>" required/>
<input type="text" placeholder="Favorite Personality" name="favperson" value="<%=result.getString("favperson")%>" required/>
<input type="text" placeholder="Secret Crush" name="crush" value="<%=result.getString("crush")%>"/>
<%if(result.getString("userid").equals(null))
{
%><input type="submit" name="btnsave" value="Save Details" onclick="form.action='saveuserdetails.jsp';"/>
<input type="submit" name="btnupdate" value="update Details" onclick="form.action='updateuserdetails.jsp';"/>
<input type="reset" value="Clear" style="float: right;"><%
}
else
{%>
<input type="submit" name="btnupdate" value="update Details" onclick="form.action='updateuserdetails.jsp';"/>
<input type="reset" value="Clear" style="float: right;"><%
}
}
}
catch(Exception ex)
{
out.println(ex);
}
%>
这是我的更新jsp
<%@page import="java.io.*"%>
<%@ page import ="java.sql.*,java.io.*,java.util.*" %>
<%
String uid = (String) session.getAttribute("userid");
String name = request.getParameter("name");
String cno = request.getParameter("cno");
//String picaddress = request.getParameter("file");
String address= request.getParameter("address");
String dob= request.getParameter("dob");
String dream= request.getParameter("dream");
String favperson= request.getParameter("favperson");
String crush= request.getParameter("crush");
String saveFile = "";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data")>=0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength)
{
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead +=byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"")+10);
saveFile = saveFile.substring(0,saveFile.indexOf("\n"));
saveFile =
saveFile.substring(saveFile.lastIndexOf("\\")+1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundry = contentType.substring(lastIndex
+1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n",pos)+1;
pos = file.indexOf("\n",pos)+1;
pos = file.indexOf("\n",pos)+1;
int boundryLocation = file.indexOf(boundry,pos)-4;
int startPos = ((file.substring(0,pos)).getBytes()).length;
int endPos = ((file.substring(0, boundryLocation)).getBytes()).length;
File ff =new File("C:/Users/Jarvis/Documents/NetBeansProjects/Slambook/web/ProfileImages/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes,startPos,(endPos-startPos));
fileOut.flush();
fileOut.close();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/slambook",
"root", "");
out.println(address);
PreparedStatement ps = con.prepareStatement("update userdetails set name =?,cno = ?, address = ?,dob = ?,dream = ?,favperson = ?,crush = ?,imgstream = ? where userid = ?");
ps.setString(9, uid);
ps.setString(1, name);
ps.setString(2, cno);
ps.setString(3, address);
ps.setString(4, dob);
ps.setString(5, dream);
ps.setString(6, favperson);
ps.setString(7, crush);
ps.setString(8,ff.getPath());
ps.executeUpdate();
response.sendRedirect("userdetails.jsp");
}
catch(Exception e)
{
out.println(e);
}
}
%>
答案 0 :(得分:0)
为了捕获特定的SQLException,需要使用getSQLState()方法与SQl State进行比较。例如:SQl State 23用于数据完整性违规。
`catch (SQLException ex) {
if (ex.getSQLState().startsWith("23")) {
JOptionPane.showMessageDialog(null, "Duplicate");
}
}`