0

I have crated a java jframe program that allows you to input a csv file and then upload it to a local database however I am having trouble inserting all the data into the database. Any ideas on where I may be going wrong?

 try{
  BufferedReader br=new BufferedReader(new FileReader(filename1));
  String line;

  while((line=br.readLine())!=null){
  String[]value=line.split(",");//Seperator

filename1 is the selected csv file from a jFilechooser.

   String sql="insert into websitehistory (Date, URL, VisitCount) "
      + "values (?,?,?)";


  pst=conn.prepareStatement(sql);
  pst.setString(1, value[0]);
  pst.setString(2, value[1]);
  pst.setString(3, value[2]);  

Here is mysql query of inserting the separated values into the database

This is my table structure:

Date           Varchar(244)
URL            VarChar(244)
VisitCount     VarChar(244)

And the type of data I would like to insert is:

31/01/2014  15:26:00,  https://www.youtube.com/,  13
31/01/2014  15:25:00,  https://www.youtube.com/,  17

Any help is much appreciated

2
  • Check parameters that you add to your prepared statement. Commented Feb 26, 2014 at 14:51
  • 1
    You probably want to use ? instead of each '"+value[i]+"' in your sql query. This way you can later replace ? with correct value using setXXX(number,value). Commented Feb 26, 2014 at 14:51

2 Answers 2

3

Try with this query

String sql="insert into websitehistory (Date, URL, VisitCount) "
      + "values (?, ?, ?)";
Sign up to request clarification or add additional context in comments.

2 Comments

changed it to: String sql="insert into websitehistory (Date, URL, VisitCount) " + "values ( ?, ?, ? )"; but getting the error "check the right syntax to use near '?,?,?)' at line 1. Could it be the data inserted or the format of my database?
It may be useful to provide your table structure and the data your are trying to insert. Please edit your question and add these information.
1

You should have place holders to use with Prepared Statement but not values directly dumped.

This is wrong:

String sql="insert into websitehistory (Date, URL, VisitCount) "
          + "values ('"+value[0]+"','"+value[1]+"','"+value[2]+"')";

Change it to:

String sql="insert into websitehistory (Date, URL, VisitCount) "
          + "values ( ?, ?, ? )";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.