Im trying to read username and password from .txt file in Java.
Format of file is as following:
user1:pass1
user2:pass2
user3:pass3
My code can't properly read passwords, any hints?
EDIT: also missing last password because last \n is missing, any way to repair it instead of adding extra newline to txt file?
try {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int c;
String user = "";
String pass = "";
char helper = 0;
while(( c = bufferedReader.read()) != -1 ) {
System.out.println((char)c);
if((char)c == '\n') {
ftpServer.addUser(user, pass);
//System.out.printf("%s", pass);
user = "";
pass = "";
helper = 0;
} else {
if ((char) c == ':') {
helper = ':';
}
if (helper == 0) {
user += (char) c;
}
if (helper == ':') {
if ((char) c != ':')
pass += (char) c;
}
}
}
bufferedReader.close();
}
If using JAVA: 8 and 8+ you could use stream on Files - java.nio.files
Path path = Paths.get(filename);
try (Stream<String> lines = Files.lines(path)) {
lines.forEach(s -> {
if (s.contains(":")) {
String[] line = s.split(":");
String userName = line[0];
String pass = line[1];
System.out.println("User name: " + userName + " password: " + pass);
}
});
} catch (IOException ex) {
// do something or re-throw...
}
Or use BufferedReader
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("/Users/kants/test.txt"));
String lineInFile = reader.readLine();
while (lineInFile != null) {
if (lineInFile.contains(":")) {
String[] line = lineInFile.split(":");
String userName = line[0];
String pass = line[1];
System.out.println("User name: " + userName + " password: " + pass);
}
lineInFile = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
NOTE: You must add further null checks and try-catch handlings. Above code is just to show you the logic.
I dont think you need an additional loop
public static void main(String[] args) throws IOException {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(
"path/to/file"));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
// read next line
line = reader.readLine();
if (line.contains(":")) {
String user = line.split(":")[0];
String pass = line.split(":")[1];
System.out.println("User is " + user + "Password is " + pass);
} }
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
prints
user1:pass1
User is user2Password is pass2
user2:pass2
User is user3Password is pass3
user3:pass3
Here is an elaborated implementation -
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
public class ReadFileAsStream {
public static void main(String... arguments) {
CredentialsParser parser;
try {
parser = new CredentialsParser("c:/temp/credtest.txt");
Credential[] credentials = parser.getCredentials();
for (Credential credential : credentials) {
System.out.println(String.format("%s : %s", credential.getUsername(), credential.getPassword()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//Parser for the credentials file format
class CredentialsParser {
private Set<Credential> credentials = new HashSet<>();
String filename = null;
public CredentialsParser() {
super();
}
public CredentialsParser(String filename) throws IOException {
super();
setFilename(filename);
if (getFilename() != null) {
parseCredentialsFile();
}
}
public Credential[] getCredentials() {
return credentials.toArray(new Credential[credentials.size()]);
}
// method to add credentials
public void addCredential(String entry) {
if (entry.indexOf(':') > -1) {
String[] values = entry.split(":");
credentials.add(new Credential(values[0], values[1]));
}
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
protected void parseCredentialsFile() throws IOException {
// read file into stream, try-with-resources
try (Stream<String> stream = Files.lines(Paths.get(filename))) {
stream.forEach(this::addCredential);
}
}
}
// Value holder for each credential entry
class Credential {
private String username = null;
private String password = null;
public Credential() {
super();
}
public Credential(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Please modify your code to this :
Use split method to make your work easy :)
public class Example {
public static void main(String[] args) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("F://test.txt"));
String line = reader.readLine();
while (line != null) {
String[] lineParts = line.split(":");
System.out.println("user : " + lineParts[0]);
System.out.println("password : " + lineParts[1]);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In test.txt :
user1:pass1
user2:pass2
user3:pass3
Output :
user : user1
password : pass1
user : user2
password : pass2
user : user3
password : pass3
Testcase file. Testcase with fillo query data driven
Suppose when the query (String qry="Select * from Sheet1 where ACC='M' and GEN='F'";) returns 5 data rows for one test case and i want to use the same qry for the next case but with different data row
public class Testcases {
public WebDriver driver;
Fillo fillo;
static ExtentReports report;
ExtentTest test;
//String PAGE_URL = "Your_page_Url";
#BeforeClass public static void allTestsPrep(){
report = new ExtentReports("Report.html",true);
}
#AfterClass public static void allTestCleanUp() {
report.flush();
}
#Before
public void setUp() {
String browserName = getParameter("browser");
if (browserName.equalsIgnoreCase("chrome")){
driver = new ChromeDriver();
}
else if(browserName.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();}
else if (browserName.equalsIgnoreCase("ie")) {
driver = new InternetExplorerDriver();
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
// driver.get(PAGE_URL);
}
#Test
public void Testcase001() throws FilloException, InterruptedException {
String qry="Select * from Sheet1 where ACC='M' and GEN='F'";
test = report.startTest("Testcase 1 - Test findpage");
Useragreement usragr = new Useragreement(driver);
usragr.CheckboxSelectall();
test.log(LogStatus.PASS, "VERIFIED");//report
}
#After
public void close() {
driver.close();
test = null;
}
private String getParameter(String name) {
String value = System.getProperty(name);
if (value == null)
throw new RuntimeException(name + " is not a parameter!");
if (value.isEmpty())
throw new RuntimeException(name + " is empty!");
return value;
}
}
Fillo file
Now the code will be `FILLO.inputText(Phone_Number,"Pno",qry)``;
public class FILLOAPP {
public static String getTestValue(String fieldName, String qry) throws FilloException{
String testString=xlTesting(fieldName,qry);
return testString;
}
public static String xlTesting(String fieldName, String qry) throws FilloException{
String testval=null;
Fillo fillo=new Fillo();
Connection connection=fillo.getConnection("resources/TestData.xlsx");
String sqry=qry;
Recordset recordset=connection.executeQuery(sqry);
while(recordset.next()){
ArrayList<String> dataColl=recordset.getFieldNames();
Iterator<String> dataIterator=dataColl.iterator();
while(dataIterator.hasNext()){
for (int i=0;i<=dataColl.size()-1;i++){
String data=dataIterator.next();
String dataVal=recordset.getField(data);
if (data.equalsIgnoreCase(fieldName)){
String testData=dataColl.get(i);
String testValue= recordset.getField(testData);
testval=testValue;
}
}
break;
}
}
recordset.close();
connection.close();
return testval;
}
public static void inputText(WebElement driver, String fieldName, String qry) throws FilloException{
String fval=getTestValue(fieldName, qry);
driver.sendKeys(fval);
}
}
public String dataRead(String sheetname, String testcaseid, String header) throws IOException {
String value = null;
try {
FileInputStream fis = new FileInputStream(TESTDATA_SHEET_PATH);
book = new XSSFWorkbook(fis);
sheet = book.getSheet(sheetname);
int lastRowNum = sheet.getLastRowNum();
// System.out.println(lastRowNum);
int lastCellNum = sheet.getRow(0).getLastCellNum();
// System.out.println(lastCellNum);
for (int i = 0; i < lastRowNum; i++) {
for (int j = 1; j < lastCellNum; j++) {
Map<String, Map<String, String>> excelmap = new HashMap<String, Map<String, String>>();
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put(sheet.getRow(0).getCell(j).toString(), sheet.getRow(i + 1).getCell(j).toString());
excelmap.put(sheet.getRow(i + 1).getCell(0).toString(), map);
if (excelmap.containsKey(testcaseid)) {
Map<String, String> w = excelmap.get(testcaseid);
if (map.containsKey(header)) {
value = w.get(header).toString();
}
}
}
}
} catch (Exception E) {
}
return value;
}
public void excelWrite(String sheetname, String testcaseid, int columnno, String value) throws Throwable {
File file = new File(TESTDATA_SHEET_PATH);
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheet(sheetname);
// sheetnoOfColumns = sheet.getRow(0).getPhysicalNumberOfCells();
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i < lastRowNum; i++) {
Map<Map<String, String>, String> excelmap = new HashMap<Map<String, String>, String>();
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put(sheet.getRow(0).getCell(0).toString(), sheet.getRow(i + 1).getCell(0).toString());
if (map.containsValue(testcaseid)) {
excelmap.put(map, value);
String w = map.get("TestCase ID");
Map<String, String> fin = new HashMap<String, String>();
fin.put(w, value);
XSSFRow row = sheet.getRow(i + 1);
Cell createcell = row.createCell(columnno);
createcell.setCellValue(value);
}
}
FileOutputStream fileOut = new FileOutputStream(TESTDATA_SHEET_PATH);
workbook.write(fileOut);
fis.close();
fileOut.close();
}
public Map ReadExcelExecution() throws Throwable {
LinkedHashMap<String, Object[]> map = new LinkedHashMap<String, Object[]>();
try {
FileInputStream fis = new FileInputStream(TESTDATA_SHEET_PATH);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheet("IssuePolicy");
int TotalRow = sheet.getLastRowNum();
int noOfColumns = (sheet.getRow(0).getPhysicalNumberOfCells() - 1);
int lastCellNum = sheet.getRow(0).getLastCellNum();
// System.out.println(TotalRow);
// System.out.println(noOfColumns);
// System.out.println(lastCellNum);
map.put("1",
new Object[] { "TestCase ID", "PolicyNo", "IssueDate", "Accumulated Value", "GMWB Type",
"MAWA/2LMAWA", "Remaining MAWA", "Protected Income Payment", "Accumulated Value DOD",
"DataComparison", "Comments" });
for (int i = 1; i <= TotalRow; i++) {
{
String testcaseid = sheet.getRow(i).getCell(0).toString();
String policyno = sheet.getRow(i).getCell(noOfColumns).toString();
String issuedate = sheet.getRow(i).getCell(2).toString();
String values = null;
map.put(testcaseid, new Object[] { testcaseid, policyno, issuedate, values, values, values, values,
values, values, values, values });
}
}
} catch (Exception E) {
}
return map;
}
public void writinginInputFile(String sheetname, Map mapvalue) throws Throwable {
// write excel file and file name is SaveTestNGResultToExcel.xls
FileInputStream fis = new FileInputStream(TESTDATA_SHEET_PATH);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.createSheet(sheetname);
keyset = mapvalue.keySet();
int rownum = 0;
for (String key : keyset) {
Row row = sheet.createRow(rownum++);
Object[] objArr = (Object[]) ReadExcelExecution().get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if (obj instanceof Date)
cell.setCellValue((Date) obj);
else if (obj instanceof Boolean)
cell.setCellValue((Boolean) obj);
else if (obj instanceof String)
cell.setCellValue((String) obj);
else if (obj instanceof Double)
cell.setCellValue((Double) obj);
}
}
try {
FileOutputStream out = new FileOutputStream(new File(TESTDATA_SHEET_PATH));
wb.write(out);
out.close();
System.out.println("Successfully saved Selenium WebDriver TestNG result to Excel File!!!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
}
}
package seleniumproject;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Multiplewindowpopup {
public static void main(String[] args) {
WebDriver driver;
String Userdir = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver",Userdir+"\\Driver\\chromedriver.exe");
driver = new ChromeDriver(); // launch chrome
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.get("http://www.popuptest.com/goodpopups.html");
//find ID of the 2nd link
driver.findElement(By.linkText("Good PopUp #2")).click();
Set<String> windowhandler = driver.getWindowHandles();
Iterator<String> myiterator = windowhandler.iterator();
////////////// //how to get index of parent and child window ???
//now we have two window IDs and moving the window from parent to child.
driver.switchTo().window(childWindow);
System.out.println("child window title"+driver.getTitle());
//closing the child window
driver.close();
// comming back to parentwindow
driver.switchTo().window(parentWindow);
System.out.println("parent window title"+driver.getTitle());
}
}
This question already has answers here:
java.lang.OutOfMemoryError: Java heap space
(12 answers)
Closed 5 years ago.
The following code of servlet receives huge JSON string every minute and near about after 2 hours I always got the OutOfMemoryError: Java Heap Space
public class GetScanAlertServlet extends HttpServlet {
private String scanType = "";
private static final String path = "D:\\Mobile_scan_alerts8180";
private static final String stockFileName = "stock.txt";
private static final String foFileName = "fo.txt";
private static Logger logger = null;
private String currDate = "";
private DateFormat dateFormat;
private StringBuffer stockData;
private StringBuffer foData;
StringBuffer data = new StringBuffer("");
// For average time of received data
private static float sum = 0;
private static float count = 0;
private static float s_sum = 0;
private static float s_count = 0;
private static float fo_sum = 0;
private static float fo_count = 0;
private static final File dir = new File(path);
private static final File stockFile = new File(path + "\\" + stockFileName);
private static final File foFile = new File(path + "\\" + foFileName);
public void init() {
logger = MyLogger.getScanAlertLogger();
if(logger == null) {
MyLogger.createLog();
logger = MyLogger.getScanAlertLogger();
}
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
String strScan = "";
try {
String asof = null;
scanType = request.getParameter("type");
scanType = scanType == null ? "" : scanType;
if(scanType.length() > 0){
if(scanType.equalsIgnoreCase("s")) {
stockData = null;
stockData = new StringBuffer(request.getParameter("scanData"));
stockData = stockData == null ? new StringBuffer("") : stockData;
} else {
foData = null;
foData = new StringBuffer(request.getParameter("scanData"));
foData = foData == null ? new StringBuffer("") : foData;
}
}
asof = request.getParameter("asof");
asof = asof == null ? "" : asof.trim();
// Date format without seconds
DateFormat formatWithoutSec = new SimpleDateFormat("yyyy/MM/dd HH:mm");
dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date tmp = new Date();
// format: yyyy/MM/dd HH:mm:ss
currDate = dateFormat.format(tmp);
//format: yyyy/MM/dd HH:mm
Date asofDate = formatWithoutSec.parse(asof);
Date cDate = formatWithoutSec.parse(currDate);
cDate.setSeconds(0);
System.out.println(asofDate.toString()+" || "+cDate.toString());
int isDataExpired = asofDate.toString().compareTo(cDate.toString());
if(isDataExpired > 0 || isDataExpired == 0) {
if(scanType != null && scanType.length() > 0) {
checkAndCreateDir();
strScan = scanType.equalsIgnoreCase("s") ? "Stock Data Recieved at "+currDate
: "FO Data Recieved at "+currDate;
//System.out.println(strScan);
} else {
strScan = "JSON of scan data not received properly at "+currDate;
//System.out.println("GSAS: received null or empty");
}
} else {
strScan = "GSAS: " + scanType + ": Received Expired Data of "+asofDate.toString()+" at "+cDate.toString();
System.out.println(strScan);
}
scanType = null;
} catch (Exception ex) {
strScan = "Mobile server issue for receiving scan data";
System.out.println("GSAS: Exception-1: "+ex);
logger.error("GetScanAlertServlet: processRequest(): Exception: "+ex.toString());
} finally {
logger.info("GetScanAlertServlet: "+strScan);
out.println(strScan);
}
}
private void checkAndCreateDir() {
try {
boolean isStock = false;
Date ddate = new Date();
currDate = dateFormat.format(ddate);
sum += ddate.getSeconds();
count++;
logger.info("Total Average Time: "+(sum/count));
if(scanType.equalsIgnoreCase("s")){ //For Stock
setStockData(stockData);
Date date1 = new Date();
currDate = dateFormat.format(date1);
s_sum += date1.getSeconds();
s_count++;
logger.info("Stock Average Time: "+(s_sum/s_count));
//file = new File(path + "\\" + stockFileName);
isStock = true;
} else if (scanType.equalsIgnoreCase("fo")) { //For FO
setFOData(foData);
Date date2 = new Date();
currDate = dateFormat.format(date2);
fo_sum += date2.getSeconds();
fo_count++;
logger.info("FO Average Time: "+(fo_sum/fo_count));
//file = new File(path + "\\" +foFileName);
isStock = false;
}
if(!dir.exists()) { // Directory not exists
if(dir.mkdir()) {
if(isStock)
checkAndCreateFile(stockFile);
else
checkAndCreateFile(foFile);
}
} else { // Directory already exists
if(isStock)
checkAndCreateFile(stockFile);
else
checkAndCreateFile(foFile);
}
} catch (Exception e) {
System.out.println("GSAS: Exception-2: "+e);
logger.error("GetScanAlertServlet: checkAndCreateDir(): Exception: "+e);
}
}
private void checkAndCreateFile(File file) {
try{
if(!file.exists()){ // File not exists
if(file.createNewFile()){
writeToFile(file);
}
} else { // File already exists
writeToFile(file);
}
} catch (Exception e) {
System.out.println("GSAS: Exception-3: "+e);
logger.error("GetScanAlertServlet: checkAndCreateFile(): Exception: "+e.toString());
}
}
private void writeToFile(File file) {
FileOutputStream fop = null;
try{
if(scanType.equalsIgnoreCase("s")){ //For Stock
data = getStockData();
} else if (scanType.equalsIgnoreCase("fo")) { //For FO
data = getFOData();
}
if(data != null && data.length() > 0 && file != null){
fop = new FileOutputStream(file);
byte[] contentBytes = data.toString().getBytes();
for(byte b : contentBytes){
fop.write(b);
}
//fop.write(contentBytes);
fop.flush();
} else {
System.out.println("GSAS: Data is null/empty string");
logger.info("GSAS: Data is null or empty string");
}
data = null;
} catch (Exception e) {
System.out.println("GSAS: Exception-4: "+e);
logger.info("GetScanAlertServlet: writeToFile(): Exception: "+e.toString());
} finally {
try {
if(fop != null)
fop.close();
} catch (IOException ex) {
java.util.logging.Logger.getLogger(GetScanAlertServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private String readFromFile(String fileName){
String fileContent = "";
try{
String temp = "";
File file = new File(fileName);
if(file.exists()){
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while((temp = br.readLine()) != null)
{
fileContent += temp;
}
br.close();
} else {
System.out.println("GSAS: File not exists to read");
logger.info("GetScanAlertServlet: File not exists to read");
}
temp = null;
file = null;
} catch (Exception e) {
System.out.println("GSAS: Exception-5: "+e);
logger.error("GetScanAlertServlet: readFromFile(): Exception: "+e.toString());
}
return fileContent;
}
public StringBuffer getStockData() {
//String temp="";
//StringBuffer temp = (StringBuffer)scanDataSession.getAttribute("stock");
//if(temp != null && temp.length() > 0) {
// return temp;
//}
if(stockData != null && stockData.length() > 0){
return stockData;
} else {
stockData = null;
stockData = new StringBuffer(readFromFile(path + "\\"+ stockFileName));
return stockData;
}
}
public StringBuffer getFOData(){
//String temp="";
//StringBuffer temp = (StringBuffer)scanDataSession.getAttribute("fo");
//if(temp != null && temp.length() > 0) {
// return temp;
//}
if(foData != null && foData.length() > 0) {
return foData;
} else {
foData = null;
foData = new StringBuffer(readFromFile(path + "\\" + foFileName));
return foData;
}
}
}
I always get the following exception after every 2 hours when I restart my jboss server and as solution I've also increased Heap size but same problem still exists
ERROR [[GetScanAlertServlet]] Servlet.service() for servlet GetScan
AlertServlet threw exception
java.lang.OutOfMemoryError: Java heap space
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.Writer.write(Unknown Source)
at GetScanAlertServlet.writeToFile(GetScanAlertServlet.java:256)
at GetScanAlertServlet.checkAndCreateFile(GetScanAlertServlet.java:236)
at GetScanAlertServlet.checkAndCreateDir(GetScanAlertServlet.java:202)
at GetScanAlertServlet.processRequest(GetScanAlertServlet.java:135)
at GetScanAlertServlet.doPost(GetScanAlertServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFi
lter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrinc
ipalValve.java:39)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Securit
yAssociationValve.java:153)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValv
e.java:59)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
int.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWor
kerThread.java:112)
at java.lang.Thread.run(Unknown Source)
Though I do not see the problem right away, your code does not properly handle resource allocation/release. The problems do not have to be caused by manipulating large JSON blob.
I just observed you do not free your resources (you open files, but do not close them in finally blocks -- any reason not to?), you would probably do better with StringBuilder for string manipulation or just use some kind of existing library (apache commons (io, string)) to do it for you.
Scheduled executor service should be properly shutted down (maybe use something your container provides: Jboss thread pool).
To start I had to rewrite most of the code. I know it's bad practice on SO to do that, We are here to teach and help. Not do other peoples work. But I could really stand reading the code and made it hard to follow.
Here are the bullet points of issues I found
No finally clauses, So your FileWriter, 'FileReader, andBufferedReader` were never closed if an exception occurred.
Not using static where it could, path and file names never changed. Also your DateFormat never changed so moved that to static
Not sure why you were setting strings to null when the next line was getting it from the request parameters and if it was null changed it to an empty string anyway.
Not sure why you were converting dates to strings to compare them. Dates are comparable.
anyway here is the code hope it helps
public class GetScanAlertServlet extends HttpServlet
{
private static final String PATH = "D:\\Mobile_scan_alerts";
private static final String STOCK_FILE_NAME = "stock.txt";
private static final String FO_FILE_NAME = "fo.txt";
private static final String EMPTY = "";
private static final DateFormat FORMAT_WITHOUT_SEC = new SimpleDateFormat("yyyy/MM/dd HH:mm");
// For average time of received data
private static float SUM = 0;
private static float S_SUM = 0;
private static float FO_SUM = 0;
private static float COUNT = 0;
private static float S_COUNT = 0;
private static float FO_COUNT = 0;
private static Logger LOGGER = null;
private String scanType;
private String stockData;
private String foData;
#Override
public void init()
{
LOGGER = MyLogger.getScanAlertLogger();
if (LOGGER == null)
{
MyLogger.createLog();
LOGGER = MyLogger.getScanAlertLogger();
}
}
/**
* Returns a short description of the servlet.
*
* #return a String containing servlet description
*/
#Override
public String getServletInfo()
{
return "Short description";
}
/**
* Handles the HTTP <code>GET</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
String strScan = EMPTY;
try
{
scanType = getRequestParameter(request, "type");
if (scanType.length() > 0)
{
if (scanType.equalsIgnoreCase("s"))
{
stockData = getRequestParameter(request, "scanData");
}
else
{
foData = getRequestParameter(request, "scanData");
}
}
//format: yyyy/MM/dd HH:mm
Date asofDate = FORMAT_WITHOUT_SEC.parse(getRequestParameter(request, "asof"));
Date currDate = new Date();
currDate.setSeconds(0);
System.out.println(asofDate.toString() + " || " + currDate.toString());
if (asofDate.compareTo(currDate) >= 0)
{
if (scanType != null && scanType.length() > 0)
{
checkAndCreateDir();
strScan =
scanType.equalsIgnoreCase("s") ? "Stock Data Recieved at " + currDate :
"FO Data Recieved at " + currDate;
}
else
{
strScan = "JSON of scan data not received properly at " + currDate;
}
}
else
{
strScan = "GSAS: " + scanType + ": Received Expired Data of " + asofDate.toString()
+ " at " + currDate.toString();
System.out.println(strScan);
}
}
catch (Exception ex)
{
strScan = "Mobile server issue for receiving scan data";
LOGGER.error("GetScanAlertServlet: processRequest(): Exception: " + ex.toString());
}
finally
{
LOGGER.info("GetScanAlertServlet: " + strScan);
out.println(strScan);
out.close();
}
}
private void checkAndCreateDir()
{
try
{
File dir = new File(PATH);
if (!dir.exists())
{
dir.mkdir();
}
File file = null;
SUM += new Date().getSeconds();
COUNT++;
LOGGER.info("Total Average Time: " + (SUM / COUNT));
if (scanType.equalsIgnoreCase("s"))
{ //For Stock
S_SUM += new Date().getSeconds();
S_COUNT++;
LOGGER.info("Stock Average Time: " + (S_SUM / S_COUNT));
file = new File(PATH + System.lineSeparator() + STOCK_FILE_NAME);
}
else if (scanType.equalsIgnoreCase("fo"))
{ //For FO
FO_SUM += new Date().getSeconds();
FO_COUNT++;
LOGGER.info("FO Average Time: " + (FO_SUM / FO_COUNT));
file = new File(PATH + System.lineSeparator() + FO_FILE_NAME);
}
checkAndCreateFile(file);
}
catch (Exception e)
{
//System.out.println("GSAS: Exception-2: "+e);
LOGGER.error("GetScanAlertServlet: checkAndCreateDir(): Exception: " + e.toString());
}
}
private void checkAndCreateFile(File file)
{
try
{
if(!file.exists())
{
file.createNewFile();
}
writeToFile(file);
}
catch (Exception e)
{
LOGGER.error("GetScanAlertServlet: checkAndCreateFile(): Exception: " + e.toString());
}
}
private void writeToFile(File file) throws IOException
{
String data = EMPTY;
if (scanType.equalsIgnoreCase("s"))
{ //For Stock
if (stockData == null)
{
stockData = readFromFile(PATH + System.lineSeparator() + STOCK_FILE_NAME);
}
data = stockData;
}
else if (scanType.equalsIgnoreCase("fo"))
{ //For FO
if (foData == null)
{
foData = readFromFile(PATH + System.lineSeparator() + FO_FILE_NAME);
}
data = foData;
}
FileWriter fileWriter = null;
try
{
if (data != null && data.length() > 0)
{
fileWriter = new FileWriter(file);
fileWriter.write(data.toString());
}
else
{
System.out.println("GSAS: Data is null/empty string");
LOGGER.info("GSAS: Data is null or empty string");
}
}
catch (Exception e)
{
LOGGER.info("GetScanAlertServlet: writeToFile(): Exception: " + e.toString());
}
finally
{
if (fileWriter != null)
{
fileWriter.flush();
fileWriter.close();
}
}
}
private String readFromFile(String fileName) throws IOException
{
String fileContent = EMPTY;
FileReader fr = null;
BufferedReader br = null;
try
{
File file = new File(fileName);
if (file.exists())
{
fr = new FileReader(file);
br = new BufferedReader(fr);
String temp;
while ((temp = br.readLine()) != null)
{
fileContent += temp;
}
}
else
{
System.out.println("GSAS: File not exists to read");
LOGGER.info("GetScanAlertServlet: File not exists to read");
}
}
catch (Exception e)
{
LOGGER.error("GetScanAlertServlet: readFromFile(): Exception: " + e.toString());
}
finally
{
if (fr != null)
{
fr.close();
}
if (br != null)
{
br.close();
}
}
return fileContent;
}
private String getRequestParameter(HttpServletRequest request, String parameter)
{
String str = request.getParameter(parameter);
return str == null ? EMPTY : str.trim();
}
}
I want to write the result of a SQL query to a csv or Excel file and save it in a particular folder. I have following requests:
I would like to know, if this can be achieved using a Java program which can be reused for any SQL query result.
I would also like to know, if this can be used for different databases (Oracle, MySQL, SQL Server, etc.).
I plan to attach the saved file to an email. Is it possible to export SQL query results to an email directly?.
With use of openCSV API, you can export your data in csv file.
CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
Boolean includeHeaders = true;
java.sql.ResultSet myResultSet = .... //your resultset logic here
writer.writeAll(myResultSet, includeHeaders);
writer.close();
Simplest solution.
Main Method
private List<String> resultSetArray=new ArrayList<>();
private String username =""; // Enter DB Username
private String password = ""; // Enter DB password
private String url = ""; // Enter DB URL
Connection connection=DriverManager.getConnection(url,user,pwd);
public static void main(String args[]) throws Exception{
fetchDataFromDatabase("SQL queries", connection);
printToCsv(resultArray);
}
fetchDataFromDatabase
The code below count the number of columns in a table, and store in a result array.
private void fetchDataFromDatabase(String selectQuery,Connection connection) throws Exception{
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(selectQuery);
int numCols = rs.getMetaData().getColumnCount();
while(rs.next()) {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= numCols; i++) {
sb.append(String.format(String.valueOf(rs.getString(i))) + " ");
}
resultSetArray.add(sb.toString());
}
} catch (SQLException e) {
LOGGER.error("Sql exception " + e.getMessage());
}
}
printToCsv
public static void printToCsv(List<String> resultArray) throws Exception{
File csvOutputFile = new File(file_name);
FileWriter fileWriter = new FileWriter(csvOutputFile, false);
for(String mapping : resultArray) {
fileWriter.write(mapping + "\n");
}
fileWriter.close();
}
It's difficult to export result set data from any tool.
ex: while exporting result set data to .csv file it does not export properly when data contains (,)
please refer below java code :
it works perfectly with any any query input and all type of data in result set
package com.demo.export;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class dataExportService {
public void getDefendants(Connection con , String db) throws Exception {
#SuppressWarnings("unused")
Workbook readWorkbook = WorkbookFactory.create(new FileInputStream(".xls file path(C:\\Users\\CEPL\\Desktop\\test.xls)") );
#SuppressWarnings("resource")
Workbook writeWorkbook = new HSSFWorkbook();
Sheet desSheet = writeWorkbook.createSheet("new sheet");
Statement stmt = null;
ResultSet rs = null;
try{
String query ="QUERY";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
Row desRow1 = desSheet.createRow(0);
for(int col=0 ;col < columnsNumber;col++) {
Cell newpath = desRow1.createCell(col);
newpath.setCellValue(rsmd.getColumnLabel(col+1));
}
while(rs.next()) {
System.out.println("Row number" + rs.getRow() );
Row desRow = desSheet.createRow(rs.getRow());
for(int col=0 ;col < columnsNumber;col++) {
Cell newpath = desRow.createCell(col);
newpath.setCellValue(rs.getString(col+1));
}
FileOutputStream fileOut = new FileOutputStream(".xls file path(C:\\Users\\CEPL\\Desktop\\test.xls)");
writeWorkbook.write(fileOut);
fileOut.close();
}
}
catch (SQLException e) {
System.out.println("Failed to get data from database");
}
}
}
Here is an example:
import java.io.*;
import java.sql.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
public class ExcelFile {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "root");
PreparedStatement psmnt = null;
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("Select * from student");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("Roll No");
rowhead.createCell((short) 1).setCellValue("Name");
rowhead.createCell((short) 2).setCellValue("Class");
rowhead.createCell((short) 3).setCellValue("Marks");
rowhead.createCell((short) 4).setCellValue("Grade");
int index = 1;
while (rs.next()) {
HSSFRow row = sheet.createRow((short) index);
row.createCell((short) 0).setCellValue(rs.getInt(1));
row.createCell((short) 1).setCellValue(rs.getString(2));
row.createCell((short) 2).setCellValue(rs.getString(3));
row.createCell((short) 3).setCellValue(rs.getInt(4));
row.createCell((short) 4).setCellValue(rs.getString(5));
index++;
}
FileOutputStream fileOut = new FileOutputStream("c:\\excelFile.xls");
wb.write(fileOut);
fileOut.close();
System.out.println("Data is saved in excel file.");
rs.close();
connection.close();
} catch (Exception e) {
}
}
}
Reference
This is my solution. Code to insert in the main class:
import java.io.*;
import java.sql.*;
import com.company.*;
/**
* Created by MAXNIGELNEGRO
*/
String[] filePath = new String[] {"C:\\Users\\Documents\\MyFile.csv"};
String[] driverDB = new String[] {"oracle.jdbc.driver.OracleDriver"};
String[] stringConnDB = new String[] {"jdbc:oracle:thin:#//127.0.0.1:1881/mydb"};
String[] userDB = new String[] {"pippo"};
String[] passDB = new String[] {"pluto"};
String[] charSep = new String[] {";"};
Boolean column= new Boolean (true);
String[] queryDB = new String[] {"select * FROM MYQUERY"};
try{
System.out.println("---------------File exist?------------" + filePath[0]);
File fileTemp = new File(filePath[0].toString());
if (fileTemp.exists()){
fileTemp.delete();
System.out.println("---------------DELETE FILE------------" + filePath[0] );
}
System.out.println("QUERY: ---->"+ queryDB[0].toString());
exportQueryToCsv exp = new exportQueryToCsv();
exp.exportQueryToCsv(filePath, driverDB, stringConnDB, userDB, passDB, queryDB, column, charSep);
if (fileTemp.exists()){
System.out.println("---File created---" + filePath[0]);
}
}
catch(Exception e){
e.printStackTrace();
}
The core class:
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Created by MAXNIGELNEGRO
*/
public class exportQueryToCsv {
public exportQueryToCsv(){}
public static void exportQueryToCsv (String[] filename, String[] driverDB, String[] connDB
, String[] userDB, String[] passDB, String[] queryDB, Boolean intestaFile
, String[] charSep) throws SQLException, IOException {
Statement stmt=null;
ResultSet rset=null;
Connection conn=null;
try { DBConn connessione = new DBConn();
conn=connessione.connect(driverDB[0],connDB[0],userDB[0],passDB[0]);
conn.setAutoCommit(false);
stmt = conn.createStatement();
rset = stmt.executeQuery(queryDB[0]);
ExportData2CSV csv = new ExportData2CSV();
csv.ExportData2CSV(rset,filename[0],intestaFile,charSep[0]);
csv.createFileCsv();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (stmt != null) {stmt.close();}
if (conn != null) {conn.close();}
if (rset != null) {rset.close();}
}
}
}
This is the class DBConn for the connection to the database:
import java.sql.*;
/**
* Created by MAXNIGELNEGRO
*/
public class DBConn {
public DBConn() {
}
public Connection connect(String driverDB, String db_connect_str, String db_userid, String db_password) {
Connection conn;
try {
Class.forName(driverDB).newInstance();
conn = DriverManager.getConnection(db_connect_str, db_userid, db_password);
} catch (Exception e) {
e.printStackTrace();
conn = null;
}
return conn;
}
}
This is the class for retrieves data from table to resultset and writes to csv file:
package com.company;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
/**
* Created by MAXNIGELNEGRO
*/
public class ExportData2CSV {
public ResultSet rset;
public String filename;
public Boolean columnName;
public String charSep;
public void ExportData2CSV(ResultSet rset, String filename, Boolean columnName, String charSep) {
this.rset = rset;
this.filename = filename;
this.columnName = columnName;
this.charSep = charSep;
}
public void createFileCsv() throws SQLException, IOException {
FileWriter cname = null;
try {
// WRITE COLUMN NAME
ResultSetMetaData rsmd = rset.getMetaData();
cname = new FileWriter(filename);
if (columnName) {
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
cname.append(rsmd.getColumnName(i));
cname.append(charSep);
cname.flush();
}
cname.append(System.getProperty("line.separator"));
}
// WRITE DATA
while (rset.next()) {
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
if (rset.getObject(i) != null) {
String data = rset.getObject(i).toString().replaceAll(charSep, "");
cname.append(data);
cname.append(charSep);
} else {
String data = "null";
cname.append(data);
cname.append(charSep);
}
}
//new line entered after each row
cname.append(System.getProperty("line.separator"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cname != null) {
cname.flush();
cname.close();
}
if (rset != null) {
rset.close();
}
}
}
}
This is the excel data sheet
input excel file containing records
Hi this is the solution you need 3 files 1.input thread 2.output thread 3.data structure 4.main
1.input thread to read the excel and output thread to write the sql out put
2.data structure is to hold and transfer the data
(InputThread.java)
import java.io.*;
public class InputThread extends Thread{
String fp;
InputString is;
String tableName="emp";
String outFile;
InputThread(String FilePath,String nameOfTheTable,String outFileName){
fp=FilePath;
outFile=outFileName;
tableName=nameOfTheTable;
}
public void run(){
File file = new File(fp);
String line;
try{
BufferedReader br = new BufferedReader(new FileReader(file));
if( (line=br.readLine()) != null)
is = new InputString(line);
//transform(is);
InputString tmp = new InputString(createTable(line));
//tmp.next = is;
is = tmp;
//tmp = tmp.next;
for(; (line = br.readLine()) != null; ) {
tmp.next = new InputString(line);
tmp = tmp.next;
transform(tmp);
}
}catch(Exception e){ System.out.println("Error is :"+e); }
//traverse();
new OutputThread(is,outFile).start();
}
void transform(InputString x){
String[] arr = x.getLine().split(",");
String sql = "insert into "+tableName+" values(";
for(int i=0;i<arr.length;i++){
sql+="'"+arr[i]+"'";
if( (i+1) < arr.length) sql+=",";
}
sql+=");";
x.setLine(sql);
}
String createTable(String x){
String[] arr = x.split(",");
String sql = "create database vamsidb "+ "use vamsidb "+"create table "+tableName+"(";
for(int i=0;i<arr.length;i++){
sql+=arr[i]+" varchar(50)";
if( (i+1) < arr.length) sql+=",";
}
sql+=");";
return sql;
}
/*public void traverse(){
InputString tmp = is;
while(is != null){
System.out.println(is.getLine());
is=is.next;
}
}*/
}
(OutputThread.java)
import java.io.*;
public class OutputThread extends Thread{
InputString is;
String outFile;
OutputThread(InputString linkedList,String outFileName){
is=linkedList;
outFile = outFileName;
}
public void run(){
try{
FileOutputStream fos = new FileOutputStream(outFile);
while(is != null){
fos.write(is.getLine().getBytes());
is=is.next;
}
fos.close();
}catch(Exception e){
System.out.println("Error is :"+e);
}
}
}
(Main.java)
public class Main{
public static void main(String[] args){
InputThread it = new InputThread("sasken.csv","emp","output.sql");
it.start();
}
}
(DataStructure.java)
//This class represents the data structure to hold and transform input
//data as a linked list of sql statements
class InputString{
String line;
InputString next;
InputString(String x){
line = x;
}
String getLine(){
return line;
}
void setLine(String x){
line = x;
}
}
output result
You can use JDBC to fetch the records from DB in java and then use Apache POI for exporting the data to CSV/Excel.
Additionally, you can use the desktop API of java to send email using your default email client.
For this to work you need to work write a small code that can take up any query and any driver . The first input should be the driver name as the input to the software that you are writing. Then the software you are writing should be in a position to execute any SQL given to it and give out only rows and columns.
The next task comes to parse the ResultSet that comes from the JDBC of java application. Either you want to write the results into CSV file or EXCEL is based on how good you have the java api to do that.
Writing the output into the CVS is easy and not trival. I have not worked on exporting the data into Excel. I am sure you find jars for that.
The solution is based on a properties file.
Where this options are configured:
The parameters of the database.
Optional extra SQL Where clause.
The parameters of the output files.
The process can start bis 4 threads to download 4 tables at the same time.
If you want to run again, the generated files must be deleted.
A text file (logs.txt) with the process data is also created.
---- PROP FILE : ExportProperties.prop ---------
The solution will write a draft cinfiguration version in:
C:\tmp\test\ExportProperties.prop
## configuration properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:#ldap://localhost
username=user
password=pass
fileExtension=_20200623.csv
columSeparator=;
charsetName=CP1252
## export only 10 rows change to false
only10Rows=true
##tables
tableName.1=USER
tableName.1.sqlWhere= user.name IS NOT NULL
tableName.2=ROLL
tableName.3=FIRMA
--------- The main file --------
public class ExportTable2CSVMain implements Runnable {
static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS|");
static final String folder = "C:\\tmp\\test\\";
static final Properties prop = getProperties();
public static void main(String[] args) {
for (int x = 1; x < 6; x++) {
try {
writieLog(0, "Start thread " + x);
new ExportTable2CSVMain();
Thread.sleep(1000);
} catch (final Exception e) {
e.printStackTrace();
}
}
}
public ExportTable2CSVMain() {
final Thread t = new Thread(this);
t.start(); // start the thread -> run
}
#Override
public void run() {
int pos = 1;
String tableName = prop.getProperty("tableName." + pos);
while (tableName != null) {
try {
export(tableName, pos++);
} catch (final Exception e) {
e.printStackTrace();
}
tableName = prop.getProperty("tableName." + pos);
}
}
private void export(String tableName, int filePos) throws Exception {
final boolean only10Rows = prop.getProperty("only10Rows", "false").equals("true");
String extraWhere = prop.getProperty("tableName."+filePos+".sqlWhere");
if(extraWhere ==null)
extraWhere = prop.getProperty("sqlWhere");
if(extraWhere ==null)
extraWhere = "";
final String sql = "select * from " + tableName + extraWhere
+ (only10Rows ? " FETCH NEXT 10 ROWS ONLY" : "");
final String fileName = folder + tableName + prop.getProperty("fileExtension");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ExportTable2CSV data2csv = null;
try {
data2csv = new ExportTable2CSV(fileName, tableName, filePos);
if (data2csv.toDo()) {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
data2csv.createFileCsv(rs);
}
} catch (final Exception e) {
final int row = data2csv == null ? -1 : data2csv.row;
writieLog(filePos, "SQL", "Error", "rows:" + row, tableName, e.getMessage());
e.printStackTrace();
} finally {
try {
rs.close();
} catch (final Exception e) {
}
try {
stmt.close();
} catch (final Exception e) {
}
try {
conn.close();
} catch (final Exception e) {
}
}
}
public Connection getConnection() throws Exception {
Class.forName(prop.getProperty("driver"));
return DriverManager.getConnection(//
prop.getProperty("url"),
prop.getProperty("username"),
prop.getProperty("password"));
}
static private Properties getProperties() {
File file = new File(folder);
if (!file.exists()) { // if the folder do not exist create it
file.mkdirs();
}
file = new File(folder + "ExportProperties.prop");
if (!file.exists()) {
try {
final PrintWriter out = new PrintWriter(
new BufferedWriter(new FileWriter(folder + "ExportProperties.prop", true)));
out.println(//
"## configuration properties\n" +
"driver=oracle.jdbc.driver.OracleDriver\n" +
"url=jdbc:oracle:thin:#ldap://localhost\n"+
"username=USER\n" +
"password=PASSWORD\n" +
"sqlWhere=\n" +
"fileExtension=_20200619.csv\n" +
"columSeparator=;\n" +
"charsetName=CP1252\n" +
"##tables\n" +
"tableName.1=USER\n" + //
"tableName.2=ROLL\n"
);
out.close();
} catch (final Exception e) {
e.printStackTrace();
}
}
final Properties prop = new Properties();
try {
prop.load(new FileInputStream(folder + "ExportProperties.prop"));
} catch (final IOException e) {
e.printStackTrace();
}
return prop;
}
public static void writieLog(int filePos, String... txt) throws Exception {
final PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(folder + "logs.txt", true)));
String sb = "";
sb += formatter.format(new Date()) + "\t";
sb += filePos == 0 ? "" : "F-" + filePos + "\t";
for (final String s : txt) {
sb += s + "\t";
}
System.out.println(sb);
out.println(sb);
out.close();
}
}
---------------- the ExportTable2CSV file -------
/**
* Created by Jose Manuel Davila (Mel) kailas.mel#gmail.com
*/
public class ExportTable2CSV {
final String fileName;
final String table;
final String columSeparator;
final Boolean columnName = true;
final int filePos;
int row = 0;
int column = 0;
public ExportTable2CSV(String fileName, String table, int filePos) {
this.filePos = filePos;
this.fileName = fileName;
this.table = table;
columSeparator = ExportTable2CSVMain.prop.getProperty("columSeparator", ";");
}
public boolean toDo() throws Exception {
if (new File(fileName).exists()) {// the file exist jet return
return false;
}
writeLine("");
return true;
}
public void createFileCsv(ResultSet rs) throws Exception {
String sb = "";
try {
ExportTable2CSVMain.writieLog(filePos, "FILE", "INI ", table, fileName);
// WRITE COLUMN NAME
final ResultSetMetaData rsmd = rs.getMetaData();
sb = "";
final List<String> list = new ArrayList<String>();
if (columnName) {
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
sb += rsmd.getColumnName(i) + columSeparator;
list.add(rsmd.getColumnName(i));
}
writeLine(sb.toString());
}
// WRITE DATA
while (rs.next()) {
sb = "";
column = 0;
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
final Object obj = rs.getObject(i);
String data = "";
if (obj != null) {
if (obj instanceof String) {
data = obj.toString();
if (data.indexOf(columSeparator) != -1) {
if (data.indexOf("\"") != -1) {
data = data.replaceAll("\"", "'");
ExportTable2CSVMain.writieLog(filePos, "FILE", "WITH comm and ; ", "row:" + row,
"Column:" + list.get(column), table, fileName);
}
data = "\"" + data + "\"";
}
} else {
data = obj.toString();
}
}
sb += data + columSeparator;
column++;
}
writeLine(sb.toString());
row++;
}
ExportTable2CSVMain.writieLog(filePos, "FILE", "END ", "rows:" + row, table, fileName);
} catch (final Exception e) {
ExportTable2CSVMain.writieLog(filePos, "FILE", "Error ", "rows:" + row, table, fileName, e.getMessage());
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
}
}
void writeLine(String line) throws Exception {
if (row > 0 && row % 1000 == 0) {
System.out.println(filePos + " " + row + " working ...");
}
final PrintWriter cname = new PrintWriter(new BufferedWriter((new OutputStreamWriter(
new FileOutputStream(fileName, true), ExportTable2CSVMain.prop.getProperty("charsetName", "CP1252")))));
if (line.equals("")) {
cname.print(line);
} else {
cname.println(line);
}
cname.close();
}
}
--------- POM file pom.xml ----------------
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ExportTable2CSV</groupId>
<artifactId>ExportTable2CSV</artifactId>
<version>1.1.0</version>
<name>ExportTable2CSV</name>
<properties>
<ojdbc8.version>18.3.0.0.0</ojdbc8.version>
</properties>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>${ojdbc8.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
To export to a column separated with tab.
change this prop in properties file :
fileExtension=_mel.txt
columSeparator=\t
This works perfectly fine:
PreparedStatement statement = famuat.prepareStatement(sql query);
ResultSet result = statement.executeQuery();
ResultSetMetaData md = result.getMetaData();
FileWriter fw = new FileWriter(filepath);
int columnCount = md.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
columnheader = md.getColumnName(i);
// System.out.println(columnheader);
if (i > 1) System.out.print(" | ");
if (i > 1) System.out.print(", ");
if (i > 1) System.out.print("\t");
fw.append(columnheader.toUpperCase());
fw.append(",");
System.out.println(columnheader);
}
while (result.next()) {
fw.append("\r\n");
for (int i = 1; i <= columnCount; i++) {
if (i > 1) System.out.print(" | ");
if (i > 1) System.out.print(", ");
if (i > 1) System.out.print("\t");
fw.append(result.getString(i));
fw.append(",");
row = result.getString(i);
System.out.println(row);
}
}
this work for exort to csv and txt
public void ExportTxt(){
FileWriter texto = null;
ResultSet rs2 = null;
ResultSet rs = null;
try {
texto = new FileWriter("texto.txt");
PrintWriter pw = new PrintWriter(texto);
Statement consulta = conn.createStatement();
Statement consulta2 = conn.createStatement();
String query = "SELECT * FROM coches";
rs = consulta.executeQuery(query);
int i=1;
pw.println("----------------");
while (rs.next()) {
pw.println(i + ",Coche: Matricula:" + rs.getString("matricula") + ", Marca:" + rs.getString("marca") + ", Modelo:" + rs.getString("modelo"));
String query2 = "SELECT * FROM reparaciones WHERE coche = '" + rs.getString("matricula") + "'";
rs2 = consulta2.executeQuery(query2);
pw.println("----------------");
while(rs2.next()){
pw.println(rs2.getRow() + ",Reparacion: Fecha entrada:" + rs2.getString("fecha_entrada") + ", Fecha salida:" + rs2.getString("fecha_salida"));
}
i++;
}
rs.close();
rs2.close();
consulta.close();
} catch (IOException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
texto.close();
} catch (IOException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void ExportCSV(){
PrintWriter pw = null;
try {
File archivo = new File("excel.csv");
pw = new PrintWriter((new FileWriter(archivo)));
Statement consulta = conn.createStatement();
String query = "SELECT * FROM clientes";
ResultSet rs = consulta.executeQuery(query);
pw.println("id;nombre;apellidos;direccion;nif;telefono");
while(rs.next()){
pw.println(rs.getInt("id") + ";" + rs.getString("nombre") + ";" + rs.getString("Apellidos") + ";" + rs.getString("direccion") + ";" + rs.getString("nif") + ";" + rs.getString("telefono"));
}
} catch (IOException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
} finally {
pw.close();
}
}
Tried some of the other answers and they didn't really work and/or ask to add libraries, which I like to avoid, especially for a format as dead simple as CSV.
So here's my own take. No special library requirements (other than your SQL driver) so (mostly) vanilla Java 8 or later should work. Also works with beanshell (hence try/finally rather than try-with-resources.) All fields should be escaped correctly, assuming the junit test I created (posted) is correct, so any RFC 4180 compliant CSV reader should be able to parse it without errors and/or getting it wrong, and for the purpose of this code, that includes Microsoft Excel.
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;
import java.io.FileWriter;
// Replace this with your own SQLServerDriver class
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
public class Sandbox {
/**
* Replaces null with an empty string, surrounds values that have commas or
* newlines with quotes, and if a value begins with a quote it escapes any
* quotes with another quote
*
* #param value
* #return
*/
public String csvEscape(String value) {
if (value == null) {
value = "";
} else if (value.contains(",") || value.contains("\n") || value.startsWith("\"")) {
value = "\"" + value.replace("\"", "\"\"") + "\"";
}
return value;
}
/**
* Run an SQL query and dump the output into a CSV file as we receive it
*
* #param url URL to your server in a format that the driver expects, i.e.
* "jdbc:sqlserver://server.com:1433;IntegratedSecurity=true;databaseName=foo"
* #param statement SQL statement, i.e. "SELECT foo FROM bar". Do NOT pass user
* generated strings!
* #param csvPath File path to the csv file, i.e. /foo/bar.csv
* #throws SQLException
* #throws IOException
*/
public void sqlToCsv(String url, String statement, String csvPath) throws SQLException, IOException {
Connection connection = DriverManager.getConnection(url);
Driver driverSelect = new SQLServerDriver();
DriverManager.registerDriver(driverSelect);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(statement);
ResultSetMetaData md = rs.getMetaData();
int colCount = md.getColumnCount();
List<String> cols = new ArrayList<String>();
for (int i = 1; i <= colCount; i++) {
String value = csvEscape(md.getColumnName(i));
cols.add(value);
}
List<String> row = new ArrayList<String>();
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(csvPath, false);
fileWriter.write(String.join(",", cols) + "\n");
while (rs.next()) {
row.clear();
for (int i = 1; i <= colCount; i++) {
String value = csvEscape(rs.getString(i));
row.add(value);
}
fileWriter.write(String.join(",", row) + "\n");
}
} finally {
fileWriter.close();
}
}
#Test
public void testCsvEscape() {
assertEquals("say \"cheese!\"", csvEscape("say \"cheese!\""));
assertEquals("\"\"\"say cheese\"\"!\"", csvEscape("\"say cheese\"!"));
assertEquals("\"cheese\nplease\"", csvEscape("cheese\nplease"));
assertEquals("\"cheese, please\"", csvEscape("cheese, please"));
assertEquals("\"say \"\"cheese,\n please!\"\"\"", csvEscape("say \"cheese,\n please!\""));
assertEquals("\"\"\"say \"\"cheese,\n please!\"\"\"\"\"", csvEscape("\"say \"cheese,\n please!\"\""));
}
}
Yes!
You can connect to the different database types using jdbc and then create an Excel with the results (Seen here).
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestAccessExcel {
public static Connection getConnection() throws Exception {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:excelDB";
String username = "username";
String password = "pass";
Class.forName(driver);
return DriverManager.getConnection(url, username, password);
}
public static void main(String args[]) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
String excelQuery = "select * from [Sheet1$]";
rs = stmt.executeQuery(excelQuery);
while (rs.next()) {
System.out.println(rs.getString("BadgeNumber") + " " + rs.getString("FirstName") + " "
+ rs.getString("LastName"));
}
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}