Java: resultSet update does not update database - java

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).

Related

Android Studio project not reading correctly from SQLite database?

I've been struggling with this for days now and can't work out what's wrong. The thing is, I am not getting errors at the moment. Im just not producing the result I hoped for. I am trying to read from a specific record in my sqlite database where the data is a week ago today - I am trying to get the weather on this day, which is in my database. Here is my DB code for the method I am using:
public long homepageTB(){
LocalDate now = LocalDate.now();
LocalDate lastWeek = now.minusDays(7+now.getDayOfWeek().getValue()-1);
LocalDate currentDate = now.minusDays(now.getDayOfWeek().getValue());
String[] lweek = new String[1];
lweek[0] = String.valueOf(lastWeek);
String lastwk = String.valueOf(lastWeek);
long rv = -1; //<<<< default return to indicate no such row
SQLiteDatabase DB=this.getReadableDatabase();
Cursor cursor=DB.rawQuery("select weather from dailyQuiz1 where date =?",lweek);
if (cursor.moveToFirst()) {
rv = cursor.getLong(cursor.getColumnIndex("weather"));
}
System.out.println(lweek[0]);
System.out.println(rv);
return rv;
}
At the moment, this correctly returns the data week ago in format: 2022-01-31
and returns rv: -1 (which means it can't find the record). However, the record is there in my database:
So realistically I don't want -1 to be returned as I would want the variable in another class I am calling it from to now hold the value of the weather, which you can see in my database is 'Rainy'. Any ideas what I've done wrong or what is a better approach? I feel it will be something so obvious but I've been trying so many things for the past few days :( Thanks for the help
With the exception that you will have an issue with the returned value being 0 if the data is found
i.e. you are getting a long from a text value when using rv = cursor.getLong(cursor.getColumnIndex("weather")) so instead of crashing when trying to convert Rainy to a long 0 is given.
What you have shown works as expected. That leaves the issue to probably be that the database does not in fact contain a row where the date column does equal the String 2022-01-31.
You need to ensure that what is in the actual database being used is as expected (your image appears to be from a tool rather than the actual database and often issues are due to differences).
So in Android Studio run the App and get to a point where it is awaiting for user interaction. The click on App Inspection do you see the same data? e.g. it should look like (not all columns have been included in the example):-
If not then your issue is that the data is not being added as expected and that needs to be corrected.
Otherwise click on the Query Icon
Enter and run SELECT length(date), * FROM dailyquiz1 WHERE date like '2022-01%' e.g. :-
if the first column returned is > 10 then there is an issue with the data that is being stored, as it contains additional data.
if the first column returned is < 10 then you are storing data that is shorter than it should be.
if no data is returned then the data stored is even further away from what it should be.

SQLite query returns only one row JDBC JAVA database

I have read the previous questions but none of them seem to match my problem although they might seem similar at first. :/_
So, I am working on a local database on Java(JDBC). When I press a button I should be getting the result of a "SELECT" query. So far so good, but for some reason which my beginner brain does not understand I keep getting only one row from the query. I have even run the same exact query on "DB Browser for SQLite" and it returns the correct result (1+ rows) .
So this is the method I am using to get the result of the query:
public ResultSet returnBill(int no) throws SQLException{
String sql = "SELECT * FROM billList WHERE no = " + no + " ;";
ResultSet thisSet = stmt.executeQuery(sql); // stmt is a 'Statement' type variable
return thisSet;
}
The method does not crash but it only returns the very first row of a query which should return more than 2 ( while (thisSet.next()) RUNS ONCE). I run other "SELECT" queries on the program which are supposed to return more than one rows and they all work fine so it's not a matter of not being able to start/close the connection etc.
Below is the method being used:
int number = table.getModel().getValueAt(rows, 0);
ResultSet thisSet = db.returnBill(number);
while (thisSet.next()){
String name = thisSet.getString("name");
int quantity = thisSet.getInt("quantity");
// do something with the returned data
}
So I get this magical number from a table (of course I made sure it's not 0, -1 etc.) and I run a query using that number. You could think of the structure of the table consisting of columns :
number | name | quantity |
where 'number' is nonzero.
I understand that probably using this method to run a query on a DB might not be safe or might post security threats but it's not the case right now. I have been working on this project for quite a long time already and I have been through many silly mistakes and I think this is yet one of them. Any help is APPRECIATED ! :D
So yes, it was a silly mistake as I expected.
So I had previously initiated a variable
Database db = new Database();
which opened the database for 2 queries (the SELECT query and an UPDATE query on another table as shown) which would then be closed at the end of the following code.
When I removed this UPDATE query however the loop executed the correct amount of times. So it seems like the SQLite JDBC is somehow prone to running a SELECT and UPDATE query on the same Statement (as far as my super mega* brain perceives it.)
So I created 2 connections at the very beginning and closed them at the end using one of them for the SELECT and the other one for the UPDATE query:
Database db = new Database(); // open database
Database db2 = new Database(); // open another creepy one :/
int number = table.getModel().getValueAt(rows, 0);
ResultSet thisSet = db.returnBill(number);
while (thisSet.next()){
String name = thisSet.getString("name");
int quantity = thisSet.getInt("quantity");
// do something with the returned data
// --------> STUPIDO <----------
//** Now executing an UPDATE query on db2 :
// ex.: UPDATE anotherTable SET amount = (current+ "+ quantity+") WHERE name= '" + name+ "' ;";
}
db.closeConn(); // close db ++
db2.closeConn(); // closes db 2
I don't know if this is the best approach but it solved my problem, so I'm leaving it so probably it could help. Any suggestions though would be welcomed :D

Removing a /.br/ from a String in Java

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.

infinite loop "hangs" after some iterations in java code during mysql query

I have a long piece of code in java which uses selenium webdriver and firefox to test my website. Pardon me if I can't reproduce it here. It has an infinite while loop to keep doing its function repeatedly. Thats what its supposed to do. Also, I don't use multi threading.
Sometimes, it gets stuck. I use a windows system and the code runs on command prompt. When it gets stuck, no errors or exceptions are thrown. Its something like "it hangs" (only the window in which the code runs hangs). Then I have to use CTRL + C . Sometimes it resumes working after that, other times it gets terminated and I restart it. It works fine but after some loops it "hangs" again. Also, I've noticed that its usually during the execution of one of the methods querying mysql database.
The code runs an infinite loop. Each time, it queries the mysql database, fetches a value(whose 'status' field is not 'done') from a particular table (one value in each loop) and proceeds with testing with this value.At the end of the loop, the table is updated (the column 'status' is set to 'done' for that value). When there are no new values having 'status' not equal to 'done' in that particular table, it should ideally display "NO NEW VALUE". However, after all the values have been used, it simply takes up the last used value (even though its status is updated to 'done' at the end of previous loop) and goes ahead. I then have to terminate the execution and run the code again. This time when the infinite loop begins, it queries the database and correctly displays "NO NEW VALUE", queries again, displays the message again and so on(which is what it should do)
I close the sql connection using con.close().
It appears that after running the loop for a few times, some resource is getting exhausted somewhere. But this is only a wild guess.
Can anyone suggest what the problem is and how do I fix it ?
Below is a relevant piece of code :
try{
String sql = "select something from somewhere where id = ? and is_deleted = '0';";
System.out.println("\n"+sql + "\n? = " + pID);
PreparedStatement selQuery1 = conn.prepareStatement(sql);
selQuery1.setString(1, pID);
ResultSet rs1 = selQuery1.executeQuery();
//Extract data from result set
while(rs1.next() && i1<6){
//do something
}//end while loop
String sql2 = "select something2 from somewhere2 where id = ? and is_deleted = '0';";
System.out.println("\n"+sql2 + "\n? = " + pjID);
PreparedStatement selQuery2 = conn.prepareStatement(sql2);
selQuery2.setString(1, pjID);
ResultSet rs2 = selQuery2.executeQuery();
//Extract data from result set
while(rs2.next() && i1<6){
//do something
}//end while loop
System.out.println("\nDone.");
conn.close();
}catch (SQLException e) {
flag=false;
}
Please note that no exceptions are thrown anywhere. The window in which the code is running just freezes (that too once in while) after displaying both the query statements.
I forgot to close the query and the resultset. Just closing the connection should implicitly close the query and resultset but it doesn't work always.
I also faced the same problem recently. But in my case the issue was with indexes. I am just pointing out here so that it can be helpful to other folks.
In my case I am fetching the menu items from MenuMaster table from database. So after successfully log in, I am hitting a database to fetch the menu items using MySQL connector driver. Here I need to fetch parent menu with their child menus. In my query, in where clause I have not used any primary key or Unique key. So, it was taking a long time. So just make an index of that key, and it worked as charm...

What is wrong with this GeoTools FeatureId?

Using the GeoTools WFS-T plugin, I have created a new row, and after a commit, I have a FeatureId whos .getId() returns an ugly string that looks something like this:
newmy_database:my_table.9223372036854775807
Aside from the fact that the word "new" at the beginning of "my_database" is a surprise, the number in no way reflects the primary key of the new row (which in this case is "23"). Fair enough, I thought this may be some internal numbering system. However, now I want a foreign key in another table to get the primary key of the new row in this one, and I'm not sure how to get the value from this FID. Some places suggest that you can use an FID in a query like this:
Filter filter = filterFactory.id(Collections.singleton(fid));
Query query = new Query(tableName, filter);
SimpleFeatureCollection features = simpleFeatureSource.getFeatures(query);
But this fails at parsing the FID, at the underscore of all places! That underscore was there when the row was created (I had to pass "my_database:my_table" as the table to add the row to).
I'm sure that either there is something wrong with the id, or I'm using it incorrectly somehow. Can anyone shed any light?
It appears as if a couple things are going wrong - and perhaps a bug report is needed.
The FeatureId with "new" at the beginning is a temporary id; that should be replaced with the real result once commit has been called.
There are a number of way to be aware of this:
1) You can listen for a BatchFeatureEvent; this offers the information on "temp id" -> "wfs id"
2) Internally this information is parsed from the Transaction Result returned from your WFS. The result is saved in the WFSTransactionState for you to access. This was before BatchFeatureEvent was invented.
Transaction transaction = new transaction("insert");
try {
SimpleFeatureStore featureStore =
(SimpleFeatureStore) wfs.getFeatureSource( typeName );
featureStore.setTransaction( transaction );
featureStore.addFeatures( DataUtilities.collection( feature ) );
transaction.commit();
// get the final feature id
WFSTransactionState wfsts = (WFSTransactionState) transaction.getState(wfs);
// In this example there is only one fid. Get it.
String result = wfsts.getFids( typeName )[0];
}
finally {
transaction.close();
}
I have updated the documentation with the above example:
http://docs.geotools.org/latest/userguide/library/data/wfs.html

Categories