I have a Java app that reads data from an Oracle database and then moves it to a MySQL database for research purposes. The data comes from an application where end users can basically enter free text. It appears that when the text is entered they are hitting returns that is causing a /.br/ to appear in the text. MySQL doesn't like the /.br/. I have tried several ways to remove the /.br/ with no luck
clinDisp = clinDisp.replaceAll("<.br */>", "");
clinDisp = clinDisp.replace("/.br/", "");
clinDisp = clinDisp.replace("<.br>", "");
No matter what I try the .br is always there and the insert for this row breaks. Also, if it matters I don't care if the line feed is lost, just something that will allow the String to insert.
Related
I am developing a Java app, as a learning tool for myself mainly, using a java database tutorial, and for some reason, the resultSet update is not actually updating my database.
Details about the project: I am using the Netbeans IDE and connecting to a Derby database with 5 tables (each with between 3 and 6 columns). The database viewer window is supposed to load all of the records into resultSet's and set the text in each blank in the window to those stored records. This process and the buttons to move the cursor to First, Previous, Next, and Last all work perfectly. The part I am currently working on is making the Update button get the text in the blanks and update the records in the database, accordingly.
Now to the problem: the records don't update. When I run the project and change a value, then press Update, the value doesn't revert back to the original value or anything (since I'm not setting the text again in the updateButton method), but when I click next and then previous, the text is back to its original value. Additionally, the values are not updated when I close the application and query the records in the database. I also don't get any exceptions, and Netbeans tells me the build is successful.
Relevant code for the update method (and yes, I'm aware it's probably inefficient, but I'm trying to just get it working before I break it trying to make it look better):
private void btnUpdateRecordActionPerformed(java.awt.event.ActionEvent evt) {
try {
// get text from fields and convert to appropriate data type
// in times
String wakeTime = fieldWakeTime.getText();
Time wakeTimeTm = java.sql.Time.valueOf(wakeTime);
String outTime = fieldOutTime.getText();
Time outTimeTm = java.sql.Time.valueOf(outTime);
String inTime = fieldOutTime.getText();
Time inTimeTm = java.sql.Time.valueOf(inTime);
String sleepTime = fieldOutTime.getText();
Time sleepTimeTm = java.sql.Time.valueOf(sleepTime);
// in weights
String morningPre = fieldPreWeight.getText();
double morningPreDoub = Double.parseDouble(morningPre);
String morningPost = fieldPostWeight.getText();
double morningPostDoub = Double.parseDouble(morningPost);
String nightWeight = FieldNightWeight.getText();
double nightWeightDoub = Double.parseDouble(nightWeight);
// in meals
String fullMeals = fieldFullMeals.getText();
int fullMealsInt = Integer.parseInt(fullMeals);
String snacks = fieldSnacks.getText();
int snacksInt = Integer.parseInt(snacks);
String sodas = fieldSodas.getText();
int sodasInt = Integer.parseInt(sodas);
String alcohol = fieldAlcohol.getText();
double alcoholDoub = Double.parseDouble(alcohol);
String desserts = fieldDesserts.getText();
int dessertsInt = Integer.parseInt(desserts);
// in ratings
String morningMood = fieldMorningMood.getText();
double morningMoodDoub = Double.parseDouble(morningMood);
String nightMood = fieldNightMood.getText();
double nightMoodDoub = Double.parseDouble(nightMood);
String activityRating = fieldActivityRating.getText();
double activityRatingDoub = Double.parseDouble(activityRating);
// uses rs's to update db columns
// in times
rsTimes.updateTime("WAKE_TIME", wakeTimeTm);
rsTimes.updateTime("OUT_TIME", outTimeTm);
rsTimes.updateTime("IN_TIME", inTimeTm);
rsTimes.updateTime("SLEEP_TIME", sleepTimeTm);
// in weights
rsWeights.updateDouble("MORNING_PRE", morningPreDoub);
rsWeights.updateDouble("MORNING_POST", morningPostDoub);
rsWeights.updateDouble("NIGHT_WEIGHT", nightWeightDoub);
// in meals
rsMeals.updateInt("FULL_MEALS", fullMealsInt);
rsMeals.updateInt("SNACKS", snacksInt);
rsMeals.updateInt("SODAS", sodasInt);
rsMeals.updateDouble("ALCOHOL", alcoholDoub);
rsMeals.updateInt("DESSERTS", dessertsInt);
// in ratings
rsRatings.updateDouble("MORNING_MOOD", morningMoodDoub);
rsRatings.updateDouble("NIGHT_MOOD", nightMoodDoub);
rsRatings.updateDouble("ACTIVITY_RATING", activityRatingDoub);
// updates rows
rsTimes.updateRow();
rsRatings.updateRow();
rsWeights.updateRow();
rsMeals.updateRow();
} catch (SQLException err) {
JOptionPane.showMessageDialog(this, err.getMessage());
}
}
When searching out solutions to this problem I've come across a great deal of variations on suggestions to con.setAutoCommit(false); and then commit the changes at the end of the method block, which I have tried, as well as explicitly con.setAutoCommit(true); to see if it wasn't defaulting to that for some reason, neither of which worked. (For reference, I did an earlier version of this app using, following the tutorial much more closely and using just 1 of the resultSet's and 1 of my tables and was able to get the update button working.) Also, my prepared statements have the resultSet's set to TYPE_SCROLL_INSENSITIVE and CONCUR_UPDATABLE, which I believe to be the correct options.
Link to the github repository of the project: link. The relevant file is MetrikaViewer.java, not DataManip.java, and the README (if you happened to need more information I didn't give you or like self-deprecating snark) is in the master branch, which is not where the rest of the code is (the Metrika branch) because of some problems I had using git (with an almost 100% chance of being caused by my own stupidity).
Really, as a still very new Java learner, I would not be at all surprised if this was caused by some weird misunderstanding of how something works on my part, but after a week of scratching my head, I just can't seem to find it.
Edit: As per Axel, I added updateRow's to the method. Now the changes update within the app while running, but it does not update the database. (I've updated the code above, accordingly, and the github repo should reflect the changes made.)
I have never used these updateXXX() methods, but as I understand the documentation, you have to issue a call to either rs.updateRow() or rs.insertRow() after updating the columns.
Also, you have to make sure to create your statement with ResultSet.CONCUR_UPDATABLE (since by default, readonly is used).
Runnning sql query to export the contents to CSV file i notice that certain columns do not get displayed properly.in my cast date timestamp is not properly displayed in cell
Code as below :
COPY (select hostname as "Host Name",devicetype as "Device Type",platform as "Model",Ipaddress as "IP Address",swversion as "Software Version",configuredTime as "Configured",activeTime as "Active",cluster as "Clusters",location as "Location",macaddress as "Mac Address",devicepool as "Device Pool" from (select getmodelinfo.ipaddress,max(getmodelinfo.hostname) as
hostname, max(getmodelinfo.macaddress) as macaddress,
max(getmodelinfo.devicetype) as devicetype,
max(getmodelinfo.platform) as platform,
max(getmodelinfo.swversion) as swversion, min(getmodelinfo.day_end_date) as configuredtime,
max(getmodelinfo.active_end_date) as activetime,
max(getmodelinfo.ucmclustername)
as cluster, max(getmodelinfo.ucmlocation) as location,
max(getmodelinfo.ucmdevicepool) as devicepool from (select
pcwh_inv.uniquedeviceid,pcwh_inv.ipaddress,
pcwh_inv.endpointmodel, pcwh_inv.hostname, pcwh_inv.devicetype, pcwh_inv.platform,
pcwh_inv.macaddress, pcwh_inv.version as
swversion,pcwh_inv.deployed_day_end_date as day_end_date,
pcwh_inv.lastupdated_day_end_date as active_end_date,
pcwh_inv.ucmclustername,pcwh_inv.ucmlocation,pcwh_inv.ucmdevicepool
from pcwh_inventory_20160410 pcwh_inv,
(select pcwh_inv.uniquedeviceid, max(pcwh_inv.lastupdated_day_end_date) as
lasttime from pcwh_inventory_20160410 pcwh_inv where pcwh_inv.ipaddress
notnull group by
pcwh_inv.uniquedeviceid)gettime where pcwh_inv.endpointmodel = 'SX' and
pcwh_inv.uniquedeviceid = gettime.uniquedeviceid and
pcwh_inv.lastupdated_day_end_date = gettime.lasttime and pcwh_inv.mgmtstatus not in ('Deleted')
)getmodelinfo group by ipaddress) M) TO '/opt/emms/emsam/export/raj2.csv' DELIMITER ',' CSV HEADER
Please find below screenshot of the result I get on running query without exporting to csv , on export to csv (image below) , image of cell content when i click on particular row content it shows correctly though(bottom most image)
It seems to me an issue with formatting csv file could you please let me know how I could do this from query level to display exact contents as in db column?
I believe nothing is wrong with your code, but that Excel is treating that column as something other than a date, this happens often. It can be resolved by selecting all of the affected cells, right clicking, and changing the format to text through the subsequent menus.
If this CSV is going to be manipulated further by another application, I don't think you will have an issue, as the data is seemingly fine.
If for whatever reason you need it to be correctly displayed in Excel dynamically, you may need to look into something like this.. Java - Excel Cell Type
The problem is straight forward there is no error with CSV format.Actually the data entries done by you are in the wrong way u can't have a space in a data entry that will cause a problem.
So in the 6th and 7th column u can see there is a space between the data entries so due to which problem is taking place.
To prevent that just add a new column for date.
It will solve all the problems and the data will be entered in your database.
Can any one suggest how to insert two lines in a single cell? I.e., I need to enter the first line and then the second line should start from new line in the single column cell. Suppose I have a column defined as varchar(100), I need to store the string core java,j2EE will service request as,
core java
j2EE will service request
in a single colomn.
When I retrieve back from the database and displayed on a JSP page it should display in two lines.
I am trying to retrieve Japanese content from database and display using jsp. Is Japanese content (which is in utf-8 format)causing any the problem so that <br /> tag is not parsing as line break.It is coming as string <br /> when I display it on screen.
A newline character ('\n')is still a character, so there's no problem inserting it:
Connection conn = ...;
PreparedStatement ps =
conn.prepareStatement
("INSERT INTO my_table VALUES (?)");
ps.setString (1, "j2EE will service request\nin a single coloumn.");
ps.executeUpdate();
Notes:
Different platforms have different line separators, so depending on how exactly this data is going to be consumed, using System.getProperty("line.separator") may be more appropriate.
For clarity's sake, again, this code omits resource management (e.g., closing the statement) and error-handling code.
I am working with a 3rd product called JPOS and it has an XMLPackager whereby I get a string from this packager that contains a record in an XML format such as:
<MACHINE><B000>STRING_VALUE</B000><B002>STRING_VALUE</B002><B003>STRING_VALUE</B003><B004>STRING_VALUE</B004><B007>STRING_VALUE</B007><B011>STRING_VALUE</B011><B012>STRING_VALUE</B012><B013>STRING_VALUE</B013><B015>STRING_VALUE</B015><B018>STRING_VALUE</B018><B028>STRING_VALUE</B028><B032>STRING_VALUE</B032><B035>STRING_VALUE</B035><B037>STRING_VALUE</B037><B039>STRING_VALUE</B039><B041>STRING_VALUE</B041><B043>STRING_VALUE</B043><B048>STRING_VALUE</B048><B049>STRING_VALUE</B049><B058>STRING_VALUE</B058><B061>STRING_VALUE</B061><B063>STRING_VALUE</B063><B127>STRING_VALUE</B127></MACHINE>
I have a SQL server table that contains a column for each of the listed. Not that it matters but I could potentially have thru defined with specific STRING_VALUEs. I'm not sure what is the best way to go about this in Java. My understanding is that SQL Server can take an XML string (not document) and do an insert. Is it best to parse each value and then put into a list that populate each value into? This is the first time I've used an XML file and therefore trying to get some help/direction.
Thanks.
Sorry, one of my colleagues was able to help and provide a quick answer. I'll try it from my Java code and it looks like it should work great. Thanks anyway.
Here is the SP that she created whereby I can pass in my XML string and bit value:
CREATE PROCEDURE [dbo].[sbssp_InsertArchivedMessages]
(
#doc varchar(max),
#fromTo bit
)
AS
BEGIN
DECLARE #idoc int, #lastId int
EXEC sp_xml_preparedocument #idoc OUTPUT, #doc
INSERT INTO [dbo].[tblArchivedMessages]
SELECT * FROM OPENXML(#idoc, '/MACHINE', 2) WITH [dbo].[tblArchivedMessages]
SET #lastId = (SELECT IDENT_CURRENT('tblArchivedMessages'))
UPDATE [dbo].[tblArchivedMessages]
SET FromToMach = #fromTo
WHERE ID = #lastId
END
GO
Regards.
Here is the problem:
for your reference:
database entries 1,2 and 3 are made using jython 2.2.1 using jdbc1.2.
database entry 4 is made using vb the old to be replace program using odbc.
We have found that if I copy and paste both jython and vb MailBody entries to wordpad directly from that SQL Server Enterprise Manager software it outputs the format perfectly with correct line returns. if I compare the bytes of each file with a hex editor or KDiff3 they are binary identically the same.
There is a 3rd party program which consumes this data. Sadly that 3rd party program reads the data and for entries 1 to 3 it displays the data without line returns. though for entry 4 it correctly formats the text. As futher proof we can see in the picture, the data in the database is displayed differently.
Somehow the line returns are preserved in the database for the vb entries but the jython entries they are overlooked. if I click on the 'MailBody' field of entry 4 i can press down i can see the rest of the email. Whereas the data for jython is displayed in one row.
What gives, what am i missing, and how do I handle this?
Here is a snippet of the code where I actually send it to the database.
EDIT: FYI: please disregard the discrepancies in the 'Processed' column, it is irrelevant.
EDIT: what i want to do is make the jython program input the data in the same way as the vb program. So that the 3rd party program will come along and correctly display the data.
so what it will look like is every entry in 'MailBody' will display "This is a testing only!" then next line "etc etc" so if I was to do a screendump all entries would resemble database entry 4.
SOLVED
add _force_CRLF to the mix:
def _force_CRLF(self, data):
'''Make sure data uses CRLF for line termination.
Nicked the regex from smtplib.quotedata. '''
print data
newdata = re.sub(r'(?:\r\n|\n|\r(?!\n))', "\r\n", data)
print newdata
return newdata
def _execute_insert(self):
try:
self._stmt=self._con.prepareStatement(\
"INSERT INTO EmailHdr (EntryID, MailSubject, MailFrom, MailTo, MailReceive, MailSent, AttachNo, MailBody)\
VALUES (?, ?, ?, ?, ?, ?, ?, cast(? as varchar (" + str(BODY_FIELD_DATABASE) + ")))")
self._stmt.setString(1,self._emailEntryId)
self._stmt.setString(2,self._subject)
self._stmt.setString(3,self._fromWho)
self._stmt.setString(4,self._toWho)
self._stmt.setString(5,self._format_date(self._emailRecv))
self._stmt.setString(6,self._format_date(self._emailSent))
self._stmt.setString(7,str(self._attachmentCount))
self._stmt.setString(8,self._force_CRLF(self._format_email_body()))
self._stmt.execute()
self._prepare_inserting_attachment_data()
self._insert_attachment_data()
except:
raise
def _format_email_body(self):
if not self._emailBody:
return "could not extract email body"
if len(self._emailBody) > BODY_TRUNCATE_LENGTH:
return self._clean_body(self._emailBody[:BODY_TRUNCATE_LENGTH])
else:
return self._clean_body(self._emailBody)
def _clean_body(self,dirty):
'''this method simply deletes any occurrence of an '=20' that plagues my output after much testing this is not related to the line return issue, even if i comment it out I still have the problem.'''
dirty=str(dirty)
dirty=dirty.replace(r"=20","")
return r"%s"%dirty
I suggest to add a debug output to your program, dumping character codes before insertion in DB. There are chances that Jython replace CrLf pair with single character and doesn't restore it when written to DB.
You should look at the quopri module (and others regarding email) so you don't have to use dirty tricks as _clean_body