Writing the resultset to csv file - java

I have a method getstaffinfo, which has 3 parameter (var_1, connection, filewriter fw), the var_1 value is read from a text file. So the method will be called as many times based on all the var_1 value passed from text file . approx ( 15000)
public static String getstaffid(String var_1, Connection connection,
FileWriter fw) throws SQLException, Exception
// Create a statement
{
String record = null;
ResultSet rs = null;
Statement stmt = connection.createStatement();
boolean empty = true;
try {
rs = stmt
.executeQuery("select username, firstname, lastname, middlename, street, city, stateorprovince, ziporpostalcode, countryorregion, fax, phone, extension, mobile, pager, title, primaryemail, secondaryemail, officename, description, comments, suspendeddate, userdata, employeeid, createuser, updateuser, createdate, updatedate, employeetype, servicedeskticketnumber, startdate, enddate, manager, businessapprover, technicalapprover, delegate, location, jobcodes, customproperty1, customproperty2, customproperty3, customproperty4, customproperty5, customproperty6, customproperty7, customproperty8, customproperty9, customproperty10 from globalusers where username = '"+ var_1 + "'");
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
ArrayList<String> records = new ArrayList<String>();
while (rs.next()) {
empty = false;
//record = rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6) + " " + rs.getString(7) + " " + rs.getString(8) + " " + rs.getString(9) + " " + rs.getString(10) + " " + rs.getString(11) + " " + rs.getString(12) + " " + rs.getString(13) + " " + rs.getString(14) + " " + rs.getString(15) + " " + rs.getString(16) + " " + rs.getString(17) + " " + rs.getString(18) + " " + rs.getString(19) + " " + rs.getString(20) + " " + rs.getString(21) + " " + rs.getString(22) + " " + rs.getString(23) + " " + rs.getString(24) + " " + rs.getString(25) + " " + rs.getString(26) + " " + rs.getString(27) + " " + rs.getString(28) + " " + rs.getString(29) + " " + rs.getString(30) + " " + rs.getString(31) + " " + rs.getString(32) + " " + rs.getString(33) + " " + rs.getString(34) + " " + rs.getString(35) + " " + rs.getString(36) + " " + rs.getString(37) + " " + rs.getString(38) + " " + rs.getString(39) + " " + rs.getString(40) + " " + rs.getString(41) + " " + rs.getString(42) + " " + rs.getString(43) + " " + rs.getString(44) + " " + rs.getString(45) + " " + rs.getString(46) + " " + rs.getString(47);
for (int i = 1; i <= columns; i++) {
String value = rs.getString(i);
records.add(value);
}
for (int j = 0; j < records.size(); j++) {
record = records.get(j) + ",";
}
fw.append(record);
}
/*fw.append(rs.getString(1));
fw.append(',');
fw.append(rs.getString(2));
fw.append(',');
fw.append(rs.getString(3));
fw.append('\n'); */
} finally {
fw.flush();
rs.close();
stmt.close();
}
return record;
}
As you can see, am executing a query for 47 values, which could be null or it can have some value.
Then i iterate through this 47 column, take the value and store it to an array list. Then i iterate the array list and write all the values to the string record with comma seperated value. Which is written to a csv file.
But it does not work fine. Any inputs would be appreciated...

You may have already solved the problem. Just let you know that I tried to use your code just now and found the issue was here:
record = records.get(j) + ",";
You should use something like this:
record = record + records.get(j) + ",";
Also change String to StringBuffer will improve the performance.

You didn't write the exact problem you face, but there is one for sure: you never write a line break into the file, so all data gets in one line.
while (rs.next()) {
... // your code, with the for loops
fw.append(record); //writing out the line, from your code
fw.append("\r\n"); //line break -- add this line
} //this is the end of the "while(rs.next())" loop
...

Related

Sqlite update operations can I add value existing value?

"UPDATE " + DatabaseFıelds.TABLE_NAME +
" SET " + DatabaseFıelds.SUBJECT_PROGRESS + " = " +
point + " WHERE " + DatabaseFıelds.SUBJECT_ID + " = " + id;
this query updates my value, but I want it to add to existing value. How can I fix this problem ?
I guess you want to add the value of point to the existing value of DatabaseFıelds.SUBJECT_PROGRESS, right? You can do it like this:
"UPDATE " + DatabaseFıelds.TABLE_NAME +
" SET " + DatabaseFıelds.SUBJECT_PROGRESS + " = " +
DatabaseFıelds.SUBJECT_PROGRESS + " + " + point +
" WHERE " + DatabaseFıelds.SUBJECT_ID + " = " + id;
it's works Thank you forpas"UPDATE " + DatabaseFıelds.TABLE_NAME +
" SET " + DatabaseFıelds.SUBJECT_PROGRESS + " = " +
DatabaseFıelds.SUBJECT_PROGRESS + " + " + point +
" WHERE " + DatabaseFıelds.SUBJECT_ID + " = " + id;

Losing focus on SwingX JCOMBO BOX

I want to achieve AutoSuggest ComboBox 2 problems I am facing
1st getting an array of id and name both want the name only.
Array i am receiving
2nd losing focus on typing the text.
Please help
database Query
public SentenceList getAutoProductList() {
return new StaticSentence(s, "SELECT "
+ "P.ID, "
+ "P.REFERENCE, "
+ "P.CODE, "
+ "P.CODETYPE, "
+ "P.NAME, "
+ "P.PRICEBUY, "
+ "P.PRICESELL, "
+ "P.CATEGORY, "
+ "P.TAXCAT, "
+ "P.ATTRIBUTESET_ID, "
+ "P.STOCKCOST, "
+ "P.STOCKVOLUME, "
+ "P.IMAGE, "
+ "P.ISCOM, "
+ "P.ISSCALE, "
+ "P.ISKITCHEN, "
+ "P.PRINTKB, "
+ "P.SENDSTATUS, "
+ "P.ISSERVICE, "
+ "P.ATTRIBUTES, "
+ "P.DISPLAY, "
+ "P.ISVPRICE, "
+ "P.ISVERPATRIB, "
+ "P.TEXTTIP, "
+ "P.WARRANTY, "
+ "P.STOCKUNITS "
+ "FROM PRODUCTS P "
+ "ORDER BY NAME", null, ProductInfoExt.getSerializerRead());
}
Auto Complete JcomboBox Code
SentenceList sentProductNames = dlSales.getAutoProductList();
m_JComboProductName.setEditable(true);
ComboBoxValModel m_jProductNameModel = new ComboBoxValModel();
editor = (JTextComponent) m_JComboProductName.getEditor().getEditorComponent();
List productList = null;
try {
productList = sentProductNames.list();
} catch (Exception e) {
}
productList.add(0, null);
m_jProductNameModel = new ComboBoxValModel(productList);
m_JComboProductName.setModel(m_jProductNameModel);
AutoCompleteDecorator.decorate(m_JComboProductName);
System.out.println("product List "+ productList);
if(m_JComboProductName.getItemCount()>0){
m_JComboProductName.setSelectedIndex(0);
}

Sqlite Database not updating using java

I am trying to update my new database but for some reason after executing my Prepared Statement the data remains the same. It returns no errors or any exceptions. Please have a look at my code and see if you can help me. Thanks in advance.
try {
connect();
PreparedStatement update = con.prepareStatement("UPDATE generalInfo SET "
+ "site" + " = '"
+ siteTt + "' , "
+ "area" + " = '"
+ areaTt + "' , "
+ "unit" + " = '"
+ unitTt + "' , "
+ "unitName" + " = '"
+ unitNameTt + "' , "
+ "drawing" + " = '"
+ drawingTt + "' , "
+ "system" + " = '"
+ systemTt + "' , "
+ "stream" + " = '"
+ streamTt + "' , "
+ "product" + " = '"
+ productTt + "' , "
+ "equipmentLoc" + " = '"
+ equipLocTt + "' , "
+ "specificLoc" + " = '"
+ specificLocTt + "' , "
+ "camOperator" + " = '"
+ camTechTt + "' , "
+ "camSerial" + " = '"
+ camSerialTt + "' , "
+ "gasSurveyOperator" + " = '"
+ surveyTechTt + "' , "
+ "gasSurveySerial" + " = '"
+ surveySerialTt + "' , "
+ "equipmentDesc" + " = '"
+ equipDescTt + "' , "
+ "equipmentType" + " = '"
+ equipTypeTt + "' , "
+ "equipmentSize" + " = '"
+ equipSizeTt + "' , "
+ "equipmentID" + " = '"
+ equipIDTt + "' , "
+ "[Maintenance type]" + " = '"
+ repairTt + "' , "
+ "measurementPosition" + " = '"
+ sourceTt + "'"
+ " WHERE leakerID = " + Integer.parseInt(leakerIDCombo.getSelectedItem().toString())+";");
update.closeOnCompletion();
update.executeUpdate();
con.close();
System.out.println("saved");
} catch (SQLException ex) {
Logger.getLogger(RefineryData.class.getName()).log(Level.SEVERE, null, ex);
}
never forgett to commit your changes:
con.setAutoCommit(true);
update.closeOnCompletion();
update.executeUpdate();
con.close();
or
update.closeOnCompletion();
update.executeUpdate();
con.commit();
con.close();

[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression

Need help with this update query on JAVA, just started learning this but having problems
Getting following error upon execution
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing
operator) in query expression 'B.AWHERE ID =4'.
and data is not updating in MS Access database file
public void update(Student s)
{
int w = Integer.parseInt(s.getID());
String query = "UPDATE Student SET ID =" + w + "," + "FirstName =" + s.getFirstName() + "," + "LastName =" + s.getLastName() + "," + "Address =" + s.getAddress() + "," + "Gender =" + s.getGender() + "," + "DOB =" + s.getDOB() + "," + "Degree =" + s.getDegree() + "WHERE ID =" + w;
try
{
stmt.executeUpdate(query);
}
catch(SQLException e)
{
System.out.println("Problem in Query");
e.printStackTrace();
}
}
Change your UPDATE statement to be like below
String query = "UPDATE Student SET "FirstName = '" + s.getFirstName() +
"'," + "LastName = '" + s.getLastName() +
"'," + "Address = '" + s.getAddress() +
"'," + "Gender = '" + s.getGender() +
"'," + "DOB = '" + s.getDOB() +
"'," + "Degree = '" + s.getDegree() +
"' WHERE ID = " + w;
But your query won't make much sense cause, you are setting ID = w and also checking WHERE ID = w
You are missing spaces and apostrophes for string values in your whole query E.g.
"Degree =" + s.getDegree() + "WHERE ID ="
is being evaluated to
"Degree =BAWHERE ID ="
which is invalid syntax.
EDIT: I can only guess that your Student object returns strings in the getter methods so try this.
String query = "UPDATE Student SET ID =" + w + ","
+ "FirstName =\"" + s.getFirstName() + "\","
+ "LastName =\"" + s.getLastName() + "\","
+ "Address =\"" + s.getAddress() + "\","
+ "Gender =\"" + s.getGender() + "\","
+ "DOB =\"" + s.getDOB() + "\","
+ "Degree =\"" + s.getDegree() + "\" "
+ "WHERE ID =" + w;

sqLite error when using update statement

I am busy using a SQLite database with a java application and after updating the database successfully I get the following error
org.sqlite.jdbc4.JDBC4PreparedStatement#4e1c6f
Below is my update code
PreparedStatement update = con.prepareStatement("UPDATE highLeaker SET " + cyc + " = "
+ dataArray[3] + " , "
+ "gasSurveyOperator" + " = "
+ dataArray[1] + " , "
+ "gasSurveySerial" + " = "
+ dataArray[2] + " , "
+ "loss" + " = "
+ dataArray[4] + " , "
+ "comment" + " = "
+ dataArray[5] + " , "
+ "cycle" + " = '"
+ cycleT + "' , "
+ "date" + " = "
+ dataArray[6]
+ " WHERE leakerID = " + dataArray[0]+";");
System.out.println(update);
update.executeUpdate();
dataRow = CSVFile.readLine(); // Read next line of data.
You printed out the object. Also, I would highly recommend using place holders instead when executing SQL queries.

Categories