469,946 Members | 1,908 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

MYSQL Problem

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= "java.io.*"%>
<%@ 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= "java.io.File"%>
<%@ page import ="java.io.*"%>
<%@ 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.io.*,java.util.*,java.uti l.StringTokenizer,java.io.BufferedReader,java.io.F ileReader,java.sql.*,java.util.StringTokenizer,jav a.io.BufferedReader,java.io.FileReader"%>


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("org.gjt.mm.mysql.Driver").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
0 1425

Post your reply

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

Similar topics

reply views Thread by Lenz Grimmer | last post: by
3 posts views Thread by Kirk Soodhalter | last post: by
10 posts views Thread by Caffeneide | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.