0
Я попытался загрузить файл excel как .csv extention и файл успешно загружен , и я получил данные листа excel в массиве , но я не могу перейти в базу данных mysql, соединение устанавливает, но данные не pass.i не получают никакой ошибки во время выполнения, но даже отдельные данные не проходят , пожалуйста, помогите мне!данные массива не вставлены в базу данных mysql
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%
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);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
System.out.println("saveFile=" + saveFile);
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
System.out.println("saveFile: " + saveFile);
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;
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;
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
%>
<b>File <% out.println(saveFile); %> has been uploaded and inserted into Database.</b>
<%
Connection con=null;
Statement pst=null;
String line = null;
String value=null;
ResultSet rs=null;
try
{
\t
\t StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new FileReader(saveFile));
String id="",name="",age="",address="",pincode="",status="";
while ((line = input.readLine()) != null)
{
\t String arr[]= line.split(",");
\t for(int i=0 ;i<arr.length;i++)
\t {
\t \t if(i==0)
\t \t {
\t \t id=arr[i];
\t \t name="";
\t \t age="";
\t \t address="";
\t \t pincode="";
\t \t status="";
\t \t }
\t \t if(i==1)
\t \t {
\t \t \t name=arr[i];
\t \t \t age="";
\t \t \t address="";
\t \t \t pincode="";
\t \t \t status="";
\t \t }
\t \t if(i==2)
\t \t {
\t \t \t age=arr[i];
\t \t \t address="";
\t \t \t pincode="";
\t \t \t status="";
\t \t }
\t \t if(i==3)
\t \t {
\t \t \t address=arr[i];
\t \t \t pincode="";
\t \t \t status="";
\t \t }
\t \t if(i==4)
\t \t {
\t \t \t pincode=arr[i];
\t \t \t status="";
\t \t }
\t \t if(i==5)
\t \t {
\t \t \t status=arr[i];
\t \t }
\t \t
\t \t
\t \t if(id!=""&&name!=""&&age!=""&&address!=""&&pincode!=""&&status!="")
\t \t {
\t \t System.out.println("ID: "+id+" Name:"+name+" Age:"+age+" Address:"+address+" pincode:"+pincode+" status:"+status);
\t \t
\t \t }
\t \t
\t \t /* Class.forName("com.mysql.jdbc.Driver");
\t Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/movie", "root", "mysql");
\t int appid=Integer.parseInt(id);
\t System.out.println("appid:"+appid);
\t
\t System.out.println("name:"+name);
\t
\t int cage=Integer.parseInt(age);
\t System.out.println("age:"+age);
\t
\t System.out.println("address:"+address);
\t
\t int pin=Integer.parseInt(pincode);
\t System.out.println("pin:"+pin);
\t
\t int stat=Integer.parseInt(status);
\t System.out.println("status:"+stat);
\t
\t String progress="open";
\t System.out.println("progress:"+progress);
\t
\t SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
\t String CurruntDate = sdf.format(new Date());
\t \t System.out.println("Currunt date:" +CurruntDate);
\t \t
\t \t HttpSession session1=request.getSession(false);
\t \t String userid=(String)session1.getAttribute("userid");
\t \t System.out.println("session userid:"+userid);
\t \t
\t \t PreparedStatement ps1=(PreparedStatement) con.prepareStatement("insert into transaction values("+appid+",'"+name+"',"+cage+",'"+address+"',"+pin+","+stat+",'"+CurruntDate+"','"+progress+"','"+userid+"')");
\t rs=(ResultSet) ps1.executeQuery();
\t // pst=con.createStatement();
\t // pst.executeUpdate("insert into transaction values("+appid+",'"+name+"',"+cage+",'"+address+"',"+pin+","+stat+",'"+CurruntDate+"','"+progress+"','"+userid+"')");
*/
\t
\t }
contents.append(line);
}
int appid=Integer.parseInt(id);
System.out.println("appid:"+appid);
System.out.println("name:"+name);
int cage=Integer.parseInt(age);
System.out.println("age:"+age);
System.out.println("address:"+address);
int pin=Integer.parseInt(pincode);
System.out.println("pin:"+pin);
int stat=Integer.parseInt(status);
System.out.println("status:"+stat);
String progress="open";
System.out.println("progress:"+progress);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String CurruntDate = sdf.format(new Date());
\t System.out.println("Currunt date:" +CurruntDate);
\t
\t HttpSession session1=request.getSession(false);
\t String userid=(String)session1.getAttribute("userid");
\t System.out.println("session userid:"+userid);
\t
\t
\t
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/movie";
String username="root";
String password="mysql";
String query="insert into transaction(applicationid, customername, customerage, customeraddress, pincode, status, edate, progress, euserid) VALUES("+id+",'"+name+"',"+age+",'"+address+"',"+pincode+","+status+",'"+CurruntDate+"','"+progress+"','"+userid+"') ";
Connection conn=DriverManager.getConnection(url,username,password);
System.out.println("connection established");
//Statement stmt=conn.createStatement();
// stmt.executeQuery(query);
\t // System.out.println("ID: "+id+" Name:"+name+" Age:"+age+" Address:"+address+" pincode:"+pincode+" status:"+status);
\t \t \t
\t
\t PreparedStatement ps1=(PreparedStatement) con.prepareStatement("insert into transaction(applicationid, customername, customerage, customeraddress, pincode, status, edate, progress, euserid) VALUES("+id+",'"+name+"',"+age+",'"+address+"',"+pincode+","+status+",'"+CurruntDate+"','"+progress+"','"+userid+"') ");
ps1.executeUpdate();
// pst=con.createStatement();
// pst.executeUpdate("insert into userone values(111,'vikas','agartala')");
//pst.executeQuery("insert into transaction(applicationid, customername, customerage, customeraddress, pincode, status, edate, progress, euserid) VALUES("+id+",'"+name+"',"+age+",'"+address+"',"+pincode+","+status+",'"+CurruntDate+"','"+progress+"','"+userid+"') ");
pst.close();
}
catch(Exception e)
{}
}
%>
спасибо за помощь, но когда я установить типы данных переменных он дает ошибку, что дает мне несоответствие типов данных и всякий раз, когда все типы данных задаются как строка, тогда это дает мне ошибку, подобную этой CurruntDat e не может решить переменную ошибку, что я могу сделать? –
Установите типы данных в соответствии с типами данных, которые вы установили в своей базе данных –