How to store user name,city,state,image path into database and image into folder using jsp?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.io.*" %>
<%@ page language="java" errorPage="" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.io.output.*"%>
<%@page import="java.util.Iterator,java.util.List" %>
<%@page import="org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.servlet.*" %>
<%@ page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" />
<style type="text/css">
.btn
{
background-color:pink;
border:lime;
color:coffee;
border-size:3;
}
</style>
</head>
<body>
<form name="form1" id="form1" action="" method="post" enctype="multipart/form-data">
<!--<input type="hidden" name="hiddenfield1" value="ok">-->
<h1 align="center">PersonDetail Detail</h1>
<hr color="pink" width="50%" size="5%" align="center">
<br>
<table border="5" align="center" bordercolor="lime">
<tr>
<td>PId</td>
<td><input type="text" name="pid" size="5" ></td>
</tr>
<tr>
<td>PName</td>
<td><input type="text" name="pname" size="50" ></td>
</tr>
<tr>
<td>Introduction</td>
<td><input type="text" name="introduction" size="100" ></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" size="100" ></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone" size="15" ></td>
</tr>
<tr>
<td>Mo</td>
<td><input type="text" name="mobaile" size="15" ></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" size="50" ></td>
</tr>
<tr>
<td>Photo Upload :</td>
<td>
<input type="file" size="26" name="file1" ></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="INSERT" class="btn">
</td>
</tr>
</table>
</form>
<%
int pid=0;
String pname="";
String introduction="";
String address="";
double phone=0;
double mo=0;
String email="";
/* out.println();
out.println("My Page");*/
boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
if (!isMultipartContent) {
/*out.println("You are not trying to upload<br/>");*/
return;
}
/*out.println("You are trying to upload<br/>");*/
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> fields = upload.parseRequest(request);
/*out.println("Number of fields: " + fields.size() + "<br/><br/>");*/
Iterator<FileItem> it = fields.iterator();
if (!it.hasNext())
{
/*out.println("No fields found");*/
return;
}
/*out.println("<table border=\"1\">");*/
while (it.hasNext())
{
FileItem fileItem = it.next();
if(fileItem.getFieldName().equals("pid"))
{
pid=Integer.parseInt(fileItem.getString());
}
else if (fileItem.getFieldName().equals("pname"))
{
pname=fileItem.getString();
}
else if (fileItem.getFieldName().equals("introduction"))
{
introduction=fileItem.getString();
}
else if (fileItem.getFieldName().equals("address"))
{
address=fileItem.getString();
}
else if (fileItem.getFieldName().equals("phone"))
{
phone=Double.parseDouble(fileItem.getString());
}
else if (fileItem.getFieldName().equals("mobaile"))
{
mo=Double.parseDouble(fileItem.getString());
}
else if (fileItem.getFieldName().equals("email"))
{
email=fileItem.getString();
}
else
{
/*out.println("hi u not success"); */
}
/*out.println("<tr>");*/
boolean isFormField = fileItem.isFormField();
if (isFormField)
{
/*out.println("<td>regular form field</td><td>FIELD NAME: " + fileItem.getFieldName() + "<br/>STRING: " + fileItem.getString() );
out.println("</td>");*/
}
else
{
String s = fileItem.getName().substring(fileItem.getName().lastIndexOf("\\")+1);
fileItem.write(new File("F:\\Applicationcalendar\\EventManagementt\\personphoto\\" + s ));
out.println(s);
fileItem.getOutputStream().close();
/*out.println("</td>");*/
try
{
/*
out.println("pid====>"+pid);
out.println("pname====>"+pname);
out.println("inroduction====>"+introduction);
out.println("address====>"+address);
out.println("phone====>"+phone);
out.println("mo====>"+mo);
out.println("email====>"+email);
*/
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.200.171:3306/eventcalendar","ecadmin","ecroot");
PreparedStatement ps=cn.prepareStatement("insert into resource_person values(?,?,?,?,?,?,?,?)");
FileInputStream fin=new FileInputStream("F:\\Applicationcalendar\\EventManagementt\\personphoto\\"+s);
/*out.println(fin.available()); */
ps.setInt(1,pid);
ps.setString(2,pname);
ps.setString(3,introduction);
ps.setString(4,address);
ps.setDouble(5,phone);
ps.setDouble(6,mo);
ps.setString(7,email);
ps.setBinaryStream(8,fin,fin.available());
int i=ps.executeUpdate();
/* System.out.println(i+" records affected");*/
if(i==1)
{
%>
<html>
<head>
<script type="text/javascript">
alert("Record Insert Succssfully ");
</script></head>
<body>
</body>
</html>
<%
}
else
{
%>
<html>
<head>
<script type="text/javascript">
alert("Record Insert Succssfully ");
</script></head>
<body>
</body>
</html>
<%
}
cn.close();
}
catch(Exception e)
{
out.println(e.toString());
}
%>
<%
}
/*out.println("</tr>"); */
}
/*out.println("</table>");*/
} catch (FileUploadException e) {
e.printStackTrace();
}
%>
</body>
</html>
From:Raval Ronak
Mca
Gujarat Vidhypith
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.io.*" %>
<%@ page language="java" errorPage="" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.io.output.*"%>
<%@page import="java.util.Iterator,java.util.List" %>
<%@page import="org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.servlet.*" %>
<%@ page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" />
<style type="text/css">
.btn
{
background-color:pink;
border:lime;
color:coffee;
border-size:3;
}
</style>
</head>
<body>
<form name="form1" id="form1" action="" method="post" enctype="multipart/form-data">
<!--<input type="hidden" name="hiddenfield1" value="ok">-->
<h1 align="center">PersonDetail Detail</h1>
<hr color="pink" width="50%" size="5%" align="center">
<br>
<table border="5" align="center" bordercolor="lime">
<tr>
<td>PId</td>
<td><input type="text" name="pid" size="5" ></td>
</tr>
<tr>
<td>PName</td>
<td><input type="text" name="pname" size="50" ></td>
</tr>
<tr>
<td>Introduction</td>
<td><input type="text" name="introduction" size="100" ></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" size="100" ></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone" size="15" ></td>
</tr>
<tr>
<td>Mo</td>
<td><input type="text" name="mobaile" size="15" ></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" size="50" ></td>
</tr>
<tr>
<td>Photo Upload :</td>
<td>
<input type="file" size="26" name="file1" ></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="INSERT" class="btn">
</td>
</tr>
</table>
</form>
<%
int pid=0;
String pname="";
String introduction="";
String address="";
double phone=0;
double mo=0;
String email="";
/* out.println();
out.println("My Page");*/
boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
if (!isMultipartContent) {
/*out.println("You are not trying to upload<br/>");*/
return;
}
/*out.println("You are trying to upload<br/>");*/
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> fields = upload.parseRequest(request);
/*out.println("Number of fields: " + fields.size() + "<br/><br/>");*/
Iterator<FileItem> it = fields.iterator();
if (!it.hasNext())
{
/*out.println("No fields found");*/
return;
}
/*out.println("<table border=\"1\">");*/
while (it.hasNext())
{
FileItem fileItem = it.next();
if(fileItem.getFieldName().equals("pid"))
{
pid=Integer.parseInt(fileItem.getString());
}
else if (fileItem.getFieldName().equals("pname"))
{
pname=fileItem.getString();
}
else if (fileItem.getFieldName().equals("introduction"))
{
introduction=fileItem.getString();
}
else if (fileItem.getFieldName().equals("address"))
{
address=fileItem.getString();
}
else if (fileItem.getFieldName().equals("phone"))
{
phone=Double.parseDouble(fileItem.getString());
}
else if (fileItem.getFieldName().equals("mobaile"))
{
mo=Double.parseDouble(fileItem.getString());
}
else if (fileItem.getFieldName().equals("email"))
{
email=fileItem.getString();
}
else
{
/*out.println("hi u not success"); */
}
/*out.println("<tr>");*/
boolean isFormField = fileItem.isFormField();
if (isFormField)
{
/*out.println("<td>regular form field</td><td>FIELD NAME: " + fileItem.getFieldName() + "<br/>STRING: " + fileItem.getString() );
out.println("</td>");*/
}
else
{
String s = fileItem.getName().substring(fileItem.getName().lastIndexOf("\\")+1);
fileItem.write(new File("F:\\Applicationcalendar\\EventManagementt\\personphoto\\" + s ));
out.println(s);
fileItem.getOutputStream().close();
/*out.println("</td>");*/
try
{
/*
out.println("pid====>"+pid);
out.println("pname====>"+pname);
out.println("inroduction====>"+introduction);
out.println("address====>"+address);
out.println("phone====>"+phone);
out.println("mo====>"+mo);
out.println("email====>"+email);
*/
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.200.171:3306/eventcalendar","ecadmin","ecroot");
PreparedStatement ps=cn.prepareStatement("insert into resource_person values(?,?,?,?,?,?,?,?)");
FileInputStream fin=new FileInputStream("F:\\Applicationcalendar\\EventManagementt\\personphoto\\"+s);
/*out.println(fin.available()); */
ps.setInt(1,pid);
ps.setString(2,pname);
ps.setString(3,introduction);
ps.setString(4,address);
ps.setDouble(5,phone);
ps.setDouble(6,mo);
ps.setString(7,email);
ps.setBinaryStream(8,fin,fin.available());
int i=ps.executeUpdate();
/* System.out.println(i+" records affected");*/
if(i==1)
{
%>
<html>
<head>
<script type="text/javascript">
alert("Record Insert Succssfully ");
</script></head>
<body>
</body>
</html>
<%
}
else
{
%>
<html>
<head>
<script type="text/javascript">
alert("Record Insert Succssfully ");
</script></head>
<body>
</body>
</html>
<%
}
cn.close();
}
catch(Exception e)
{
out.println(e.toString());
}
%>
<%
}
/*out.println("</tr>"); */
}
/*out.println("</table>");*/
} catch (FileUploadException e) {
e.printStackTrace();
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.io.*" %>
<%@ page language="java" errorPage="" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.io.output.*"%>
<%@page import="java.util.Iterator,java.util.List" %>
<%@page import="org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.servlet.*" %>
<%@ page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" />
<style type="text/css">
.btn
{
background-color:pink;
border:lime;
color:coffee;
border-size:3;
}
</style>
</head>
<body>
<form name="form1" id="form1" action="" method="post" enctype="multipart/form-data">
<!--<input type="hidden" name="hiddenfield1" value="ok">-->
<h1 align="center">PersonDetail Detail</h1>
<hr color="pink" width="50%" size="5%" align="center">
<br>
<table border="5" align="center" bordercolor="lime">
<tr>
<td>PId</td>
<td><input type="text" name="pid" size="5" ></td>
</tr>
<tr>
<td>PName</td>
<td><input type="text" name="pname" size="50" ></td>
</tr>
<tr>
<td>Introduction</td>
<td><input type="text" name="introduction" size="100" ></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" size="100" ></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone" size="15" ></td>
</tr>
<tr>
<td>Mo</td>
<td><input type="text" name="mobaile" size="15" ></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" size="50" ></td>
</tr>
<tr>
<td>Photo Upload :</td>
<td>
<input type="file" size="26" name="file1" ></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="INSERT" class="btn">
</td>
</tr>
</table>
</form>
<%
int pid=0;
String pname="";
String introduction="";
String address="";
double phone=0;
double mo=0;
String email="";
/* out.println();
out.println("My Page");*/
boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
if (!isMultipartContent) {
/*out.println("You are not trying to upload<br/>");*/
return;
}
/*out.println("You are trying to upload<br/>");*/
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> fields = upload.parseRequest(request);
/*out.println("Number of fields: " + fields.size() + "<br/><br/>");*/
Iterator<FileItem> it = fields.iterator();
if (!it.hasNext())
{
/*out.println("No fields found");*/
return;
}
/*out.println("<table border=\"1\">");*/
while (it.hasNext())
{
FileItem fileItem = it.next();
if(fileItem.getFieldName().equals("pid"))
{
pid=Integer.parseInt(fileItem.getString());
}
else if (fileItem.getFieldName().equals("pname"))
{
pname=fileItem.getString();
}
else if (fileItem.getFieldName().equals("introduction"))
{
introduction=fileItem.getString();
}
else if (fileItem.getFieldName().equals("address"))
{
address=fileItem.getString();
}
else if (fileItem.getFieldName().equals("phone"))
{
phone=Double.parseDouble(fileItem.getString());
}
else if (fileItem.getFieldName().equals("mobaile"))
{
mo=Double.parseDouble(fileItem.getString());
}
else if (fileItem.getFieldName().equals("email"))
{
email=fileItem.getString();
}
else
{
/*out.println("hi u not success"); */
}
/*out.println("<tr>");*/
boolean isFormField = fileItem.isFormField();
if (isFormField)
{
/*out.println("<td>regular form field</td><td>FIELD NAME: " + fileItem.getFieldName() + "<br/>STRING: " + fileItem.getString() );
out.println("</td>");*/
}
else
{
String s = fileItem.getName().substring(fileItem.getName().lastIndexOf("\\")+1);
fileItem.write(new File("F:\\Applicationcalendar\\EventManagementt\\personphoto\\" + s ));
out.println(s);
fileItem.getOutputStream().close();
/*out.println("</td>");*/
try
{
/*
out.println("pid====>"+pid);
out.println("pname====>"+pname);
out.println("inroduction====>"+introduction);
out.println("address====>"+address);
out.println("phone====>"+phone);
out.println("mo====>"+mo);
out.println("email====>"+email);
*/
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection("jdbc:mysql://192.168.200.171:3306/eventcalendar","ecadmin","ecroot");
PreparedStatement ps=cn.prepareStatement("insert into resource_person values(?,?,?,?,?,?,?,?)");
FileInputStream fin=new FileInputStream("F:\\Applicationcalendar\\EventManagementt\\personphoto\\"+s);
/*out.println(fin.available()); */
ps.setInt(1,pid);
ps.setString(2,pname);
ps.setString(3,introduction);
ps.setString(4,address);
ps.setDouble(5,phone);
ps.setDouble(6,mo);
ps.setString(7,email);
ps.setBinaryStream(8,fin,fin.available());
int i=ps.executeUpdate();
/* System.out.println(i+" records affected");*/
if(i==1)
{
%>
<html>
<head>
<script type="text/javascript">
alert("Record Insert Succssfully ");
</script></head>
<body>
</body>
</html>
<%
}
else
{
%>
<html>
<head>
<script type="text/javascript">
alert("Record Insert Succssfully ");
</script></head>
<body>
</body>
</html>
<%
}
cn.close();
}
catch(Exception e)
{
out.println(e.toString());
}
%>
<%
}
/*out.println("</tr>"); */
}
/*out.println("</table>");*/
} catch (FileUploadException e) {
e.printStackTrace();
}
%>
</body>
</html>