By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,965 Members | 1,757 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,965 IT Pros & Developers. It's quick & easy.

MYSQL Problem

P: 16
Dear All,

I am facing a very peculiar problem. I am reading a CSV file from my JSP code and trying to insert them into MYSQL database. the program first reads a line and then splits it into words using the split function and then inserts them into different fields of the database. But the problem is that the JSP [age shows that the records have been inserted but actually when we look into the database althugh the records have been inserted but they are empty that is without values.

This is the code that i have written

<%@ page import= "*"%>
<%@ page import="java.util.StringTokenizer"%>
<%@ page import="java.util.*"%>
<%@ page import= "java.util.Date" %>
<%@ page import= "java.text.DateFormat" %>
<%@ page import= "java.text.SimpleDateFormat" %>
<%@ page language="java" %>
<%@ page import="java.lang.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import= ""%>
<%@ page import ="*"%>
<%@ page import ="java.awt.*"%>
<%@ page import ="java.awt.Component.*"%>
<%@ page import ="java.awt.event.*"%>
<%@ page import ="javax.swing.*"%>
<%@ page import ="java.sql.ResultSet.*"%>
<%@ page import ="java.sql.Statement"%>

<%@ page import="java.text.*,*,java.util.*,java.uti l.StringTokenizer,, ileReader,java.sql.*,java.util.StringTokenizer,jav,"%>


File file = new File("C:\\Documents and Settings\\nripraj.INFOBASE\\Desktop\\Earnings_1106 00_0_0_200702.csv");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;

try {

fis = new FileInputStream(file);

// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

// dis.available() returns 0 if the file does not have more lines.

while (dis.available() != 0) {

// this statement reads the line from the file and print it to
// the console.
String line = dis.readLine();
out.println("Data is:"+line);
//int n = line.indexOf("",0);
String token[];

token=line.split(" ");

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date curdate = new Date();

Class.forName("").newInstan ce(); //driver for mysql database.

Connection con= null;

ResultSet rs = null;

con = DriverManager.getConnection("jdbc:mysql://localhost/rptgenerator","generator", "generator");

String dt = dateFormat.format(new Date());
String field1="";
String field2="";
String field3="";
String field4="";
String field5="";
String field6="";

for (int i=0;i<token.length;i++)
if (i==0)
field1=token[i] ;
else if (i==1)
field2=token[i] ;
else if (i==2)
field3=token[i] ;
else if (i==3)
field4=token[i] ;
else if (i==4)
field5=token[i] ;
else if (i==5)
field6=token[i] ;


String query = "insert into xyz values(?,?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, dt);
pstmt.setString(2, field1);
pstmt.setString(3, field2);
pstmt.setString(4, field3);
pstmt.setString(5, field4);
pstmt.setString(6, field5);
pstmt.setString(7, field6);*/
pstmt.setString(1, token[1]);
pstmt.setString(2, "Compare");

int l = pstmt.executeUpdate();
out.println("Updated Records:"+l);


out.println("Input Successful");
// dispose all the resources after using them.


} catch (FileNotFoundException e) {
} catch (IOException e) {
}catch (Exception e) {



Any help will be appreciated..

Thanks & Regards
Nrip Raj Jain
Feb 8 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.