Share on Google+Share on Google+

farah
insert data
0 Answer(s)      6 years and 6 months ago
Posted in : Java Beginners
i've got a problem to insert my data to database.i can upload my multipart file but not text data.Please help me to solve this .Attached herewith my source code..

a)index.jsp

<%@ page import="java.util.Date,java.text.DateFormat,java.text.SimpleDateFormat"%>
<%
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();

%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Page Insert</title>
</head>

<body>
<form action="formpost.jsp" method="post" ENCTYPE="multipart/form-data" >
<table width="361" border="2" align="center">

<tr>
<th scope="row">TRANSACTION</th>

<td><input type="text" name="transaction" readonly="true" value="<%=dateFormat.format(date)%>">
</tr>
<tr>
<th scope="row">VALID START </th>
<td><select name="start" size="1" >
<option>equal</option>
<option>before</option>
<option>after</option>
<option>meet</option>
<option>met_by</option>
</select>
<input type="text" name="textfield" size="15">
</tr>
<tr>
<th scope="row">VALID END </th>
<td><select name="end" size="1" id="end">
<option>equal</option>
<option>before</option>
<option>after</option>
<option>meet</option>
<option>met_by</option>
</select>
<input type="text" name="textfieldd" size="15">
</tr>
<tr>
<th> UPLOAD FILE </th>
<td><input type="file" name="file"></td>
</tr>
</table>


<p align="center">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</p>

</form>
</body>
</html>

b)formpost.jsp


<[email protected] import="java.text.*,java.io.*,java.sql.*" %>
<% //obtain parameter from the client
String transaction=request.getParameter("transaction");
String start=request.getParameter("start");
String textfield=request.getParameter("textfield");
String end=request.getParameter("end");
String textfieldd=request.getParameter("textfieldd");

try{

Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "daisy");
PreparedStatement st =
con.prepareStatement("insert into images values(?,?,?,?,?)");

st.setString(2, transaction);
st.setString(3, start);
st.setString(4, textfield);
st.setString(5, end);
st.setString(6, textfieldd);
st.executeUpdate();
} catch (Exception e) {
System.out.println(e.getMessage());
}


%>

<% //to get the content type information from JSP Request Header
String contentType = request.getContentType();
//here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
//we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}

String file = new String(dataBytes);
//for saving the file name
String 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 boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
//extracting the index of file
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

// creating a new file with the same name and writing the content in new file
String folder = "C:/Program Files/Apache Software Foundation/Apache Tomcat 6.0.20/webapps/ROOT/upload/web/";
FileOutputStream fileOut = new FileOutputStream(folder+saveFile);
//out.print("Saved here: " + saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
}
%>

c)table created as below:-

(
id INT not null primary key,
transaction VARCHAR(15),
startoperator VARCHAR(15),
startvalid VARCHAR(15),
endoperator VARCHAR(15),
endvalid VARCHAR(15)
)

Ads

View Answers









Related Tutorials/Questions & Answers:
insert data
insert data  i've got a problem to insert my data to database.i can...("insert into images values(?,?,?,?,?)"); st.setString(2... and as well as the passed data from mulitpart/form-data is greater than or equal to 0
insert and delete data in database
insert and delete data in database  insert and delete data in database from servlets through JDBC   Hi Friend, Please visit the following links: Insert Data Delete Data Thanks
Advertisements
single query to insert the data
single query to insert the data  How to insert data for all HTML fields in single MYSQL query
insert data into database
insert data into database  hi,thanks for reply just i am doing student information project,frontend is jsp-servlet and backend is msaccess2007. i... the data into the jsp page that data stored into the database.Here the error
Insert Data From File
Insert Data From File       Insert Data From File is  used to insert data from file... to insert data from file. To understand this example, we create a table 'Stu_Table
How to insert clob data??
How to insert clob data??  Can any one tell me in details how... to insert more than 4000 characters.I have heard that clob fields can hold 4 gb of data and need pl/sql to avail that feature.Can any one tell me how to create
database is connected but not insert the data
database is connected but not insert the data  hi, i am getting connected to database.Retrive the data also but cannot insert the data into database...=con.prepareStatement("insert into studentmaster(slno) values('"+batch+"')"); i
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
insert data into database
insert data into database  type Exception report message...)Go to the start->Control Panel->Administrative Tools-> data sources. 2... selecting the driver, click finish button. 4)Then give Data Source Name
EJB Insert data
.style1 { color: #FFFFFF; } EJB Insert data... describes you the way  to insert data into the database using EJB. The steps involved in inserting data are as :- 1)Create an interface named
Insert Data From File
Insert Data From File       Insert Data From File is  used to insert data from file... to insert data from file. To understand this example, we create a table 'Stu_Table
To insert maximum number of data in database.
To insert maximum number of data in database.  how to change string data type into clob data type in jsp   Hi Friend, Please visit the following link: http://www.roseindia.net/jdbc/insert-clob-example.shtml Thanks
How to insert data into MySQL Table?
How to insert data into MySQL Table?  Hi, How to insert the data...   Hi, The insert into query is used to insert the data into MySQL... ) Then we can use the following query to insert the data: insert into email(first_name
FAILED TO INSERT DATA FROM FUNCTION();
FAILED TO INSERT DATA FROM FUNCTION();  HELLO, I HAVE A PROBLEM TO INSERT DATA FROM OUTPUT FROM FUNCTION()... I WANT TO STORE THE OUTPUT IN DATABASE, BUT FAILED TO INSERT... before this.... ......................... function
insert and retrive data and time - SQL
insert and retrive data and time  hello everybody . Is there any method in java so that we can insert current system time and then it can... default NULL, PRIMARY KEY (`id`) ) To insert current date and time
insert data in the database using checkbox
insert data in the database using checkbox  i am fetching data from... each row but the problem is that i am not fetching data in any textboxes so how should i insert only checked data into database on submission.   We
Insert file data into database
Insert file data into database In this section, you will learn how to insert.... Now to insert this data into the database, we have established a database connection. Then using the insert query, we have inserted whole file data
how to insert data from netbeans into databse
how to insert data from netbeans into databse  how to insert data from netbeans into databse   Please visit the following link: http://www.roseindia.net/webservices/web-services-database.shtml
Insert Mysql Blob data
Insert Mysql Blob data   In this section, we will discuss about how to insert a blob data into a database table. A Blob stores a binary large... which points to the Blob data, data is not directly stored in the row
Question about "Insert text file data into Database"
Question about "Insert text file data into Database"  Hey I was reading the tutorial "Insert text file data into Database", (awesome btw), and noticed that both a FileInputStream, a DataInputStream and a BufferedReader
Java insert file data to JTable
Java insert file data to JTable In this section, you will learn how to insert text file data into JTable. Swing has provide useful and sophisticated set...); } }  Through the above code, you can insert the file data into JTable
how to insert data into database using jsp & retrive
how to insert data into database using jsp & retrive  Hello, I have created 1 html page which contain username, password & submit button. in my oracle10G database already contain table name admin which has name, password
EJB Insert data
.style1 { color: #000000; } EJB Insert data... describes you the way  to insert data into the database using EJB. The steps involved in inserting data are as :- 1)Create an interface named
how to insert, retrieve data from,to db(code)....
how to insert, retrieve data from,to db(code)....  Hi..... i ve created login page n in that a registration form also.... my doubt is-when... that data will b stored in db(m using sybase). 2.also hw to retrieve the data from
insert excel value in to oracle data base
insert excel value in to oracle data base  Hi All I am using... org.apache.poi.poifs.filesystem.POIFSFileSystem; public class Insert...(); PreparedStatement stat=con.prepareStatement("insert into login(username,password1
what is the jsp coding to insert a data in database tables
what is the jsp coding to insert a data in database tables  Dear Sir, I Want to know the coding for insert the data from jsp to oracle database.. my... departmentname and departmentid and click submit i want to save this data in my
how to insert, retrieve data from,to db(code)....
how to insert, retrieve data from,to db(code)....  Hi, i have created... register his details,(for ex,username,password,dob,gender) 1.how that data will b stored in db(m using sybase). 2.also hw to retrieve the data from db. cn u
how to insert data in database using html+jsp
how to insert data in database using html+jsp  anyone know what... = null; // declare a resultset that uses as a table for output data from... table. String QueryString = "insert into xxqc_announcement(PID,msg
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads