Reputation: 135
I want to upload file CSV to MySQL with servlet and I have code like this and I got an error from my code.
package ServToDb;
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import com.opencsv.CSVReader;
import Connection.Database;
@WebServlet("/ImportCSVtoDB")
public class ImportCSVtoDB extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ImportCSVtoDB() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String target_file ; // fileThatYouWantToFilter
File folderToScan = new File("C:/Users/DELL/Music/"); // this is the path we are scanning
File[] listOfFiles = folderToScan.listFiles();// this is the list of files returned in this folder
for (int j = 0; j < listOfFiles.length; j++) { // for each file in the folder
if (listOfFiles[j].isFile()) {
target_file = listOfFiles[j].getName();
if (target_file.equals("blog.csv")) { // look for the file. You can also check if it ends with csv etc.
try
{
Database db = new Database();
String loadQuery = "LOAD DATA LOCAL INFILE 'C:/Users/DELL/Music/csvtest.csv' INTO TABLE"
+ "importcsv FIELDS TERMINATED BY ','LINES TERMINATED BY '\n' (fname, sex) "; //remember to change the path here as well
Statement stmt = db.dbConn().createStatement();
stmt.execute(loadQuery);
}
catch (Exception e)
{
e.printStackTrace();
}
try (CSVReader reader = new CSVReader(new FileReader("C:/Users/DELL/Music/" + target_file), ',');)
{
Database db = new Database();
String insertQuery = "Insert into importcsv (fname, sex) values (?,?)";//write your insertion query
PreparedStatement pstmt = db.dbConn().prepareStatement(insertQuery);
String[] rowData = null;// create empty array which will hold values from csv
while((rowData = reader.readNext()) != null)
{
int i = 0;
for (String data : rowData)
{
i++;
if (i<3) {
pstmt.setString(i, data);
if (i == 2) {
pstmt.addBatch();// add batch
}
if (i % 30 == 0)// insert when the batch size is 10
{
pstmt.executeBatch(); // execute queries
}
}
}
}
}
catch (Exception e) {
e.printStackTrace();}
}
}
}
out.println("success");
}
}
but i get error like this.
The constructor CSVReader(Reader, char) is deprecated
from line 60
try (CSVReader reader = new CSVReader(new FileReader("C:/Users/DELL/Music/" + target_file), ',');)
How can I make this code running please help me, Thank you in advance for any help. I'm not looking to have this done for me, I'm just stuck and need help finding my way.
Upvotes: 2
Views: 4901
Reputation: 188
Here is the exact solution for deprecated code:
CSVParser csvParser = new CSVParserBuilder().withSeparator(',').build();
CSVReader reader = new CSVReaderBuilder(new FileReader("C:/Users/DELL/Music/" +
target_file)).withCSVParser(csvParser).build();
Upvotes: 5
Reputation: 140457
The real answer here:
Instead: you turn to the javadoc for the class you are using (probably here), to there find:
Deprecated. Please use CSVReaderBuilder instead.
And that builder class is documented here, including an usage example.
Beyond that: if you really get an error (like your IDE telling you it is an error), then that is because your configured your IDE setup in a way that this warning is flagged as error (so this is again something that you could influence yourself easily).
Finally: the really real answer here is to not put down code you do not understand. It seems you got code from somewhere, and you started using it, without doing any research what exactly that code is doing. That is an inefficient approach. You want to understand each and character in your source code. If you don't understand something, then you research that, until you understand it. That is how you learn programming.
Upvotes: 6
Reputation: 7
You could just use a Scanner unless you're specifically trying to use that CSV reader package?
Scanner scanner = new Scanner("fileName.csv"); //open the file
while(scanner.hasNextLine()) { //Iterate through the file
String line = scanner.nextLine(); //grab the next line
String[] lineParts = line.split(","); //split based on commas and do something with the data
}
scanner.close(); //close the file
Upvotes: -2