How to remove item in JTable also remove from Database? - java

I am pretty basic to link from Java to Database.
I link item from Database (I use MS.Access) to Java table (JTable)
but when I delete in JTable using this code
int numRows = tblweng.getSelectedRows().length;
for(int i=0; i<numRows ; i++ )
((DefaultTableModel)tblweng.getModel()).removeRow(tblweng.getSelectedRow());
it deletes only in Table but not in Database. So how I can remove both of them at the same time I click "Remove Button".
Please guide me but don't go too deep I am just a basic here. Thank in advance.

1. click Jtable // row selected
2. get data form dep_Table like
int a = dep_Table.getSelectedRow();
String b = String.valueOf(dep_Table.getValueAt(a, 1));
3. What data you want in you get and store in String
4. Connect Database
5. use Delete Query and Delete Data From Database
6. Reload Table Again
These Few basic Step is Enough for You.. I Think..

Related

SQLite Java: update table ID's after deleting item automatically

i'm using SQLite to store data and if i delete the last row, ID is 4 and after that add a new row and the ID is 5 and it should be 4. And when trying to view the data it crashes since it can't find data from ID 5.
Database Inspector
In database inspector left of the ID column is a row number. is this accessible since that would solve the problem or is it just a row number indicator in Android studio.
Thank you and have a nice day!
Got it working by adding these:
String strSQL1 = "UPDATE people_table SET id = (id +1) WHERE id < 1";
String strSQL = "UPDATE people_table SET id = (id -1) WHERE id > 1";
db.execSQL(strSQL);
db.execSQL(strSQL1);
The id goes right one even if I delete something between first and last one.
And you need to remove autoincrement from id for this to work.
I think you can use a method to recover the last item that you insert, or the one with the higher id, with that you have the value of the next id that you need to put as an extra. If you need it when there are no values then save it when you delete the last item, or reset the data base so that ir reestart the count of the id

JTable with updated values after a file search is performed

I'm trying to create a program that searches a known file path for some files. If the search finds the file, I'd like for the program to update a JTable so that it removes that file from the table.
The point is for the table to display files that aren't yet in this certain folder.
edit: "Philly" and "L2" columns are server names. The rows are workstations that are on those servers. The two relate such that when the work has been completed on the workstations, the person who has completed that work takes a screen shot with "Snipping Tool", which is then saved as a .PNG file in a designated folder. The files are saved like... If server 1, workstation 1 was completed, it would be saved as "1-1 MyInitials.PNG" inside of the designated folder.
The idea of using an automatic TimerTask is a great idea, although I'm wholly unfamiliar with it.
You could use the DefaultTableModel of the JTable to search what is in table for a file name match.
Iterate through the table rows and read the value within the cell
column that contains the file name. You can use the the
DefaultTableModel#getRowCount() method and the
DefaultTableModel#getValueAt() methods for this.
Once you have found a match you would like to remove from table then
you can use the DefaultTableModel#removeRow() method.
A method to do this sort of thing might look something like this (Not Tested):
public void removeIfExist(JTable yourJTable, String fileName) {
// Get Table Model
DefaultTableModel dtm = (DefaultTableModel) yourJTable.getModel();
// Get Table Row Count
int rowCount = dtm.getRowCount();
// Assuming file names are in column 1
// and not in column 0.
int fileNameColumn = 1;
// Iterate through each table row and get the String
// value for what is within the row cell of column 1.
for (int i = 0; i < rowCount; i++) {
// Grab the string value in column 1 of current row.
String stringValue = dtm.getValueAt(i, fileNameColumn).toString();
// Is the cell's String value the same as the file name?
// If you want to ignore letter case for comparing then
// use equalsIgnoreCase() instead of equals().
if (stringValue.equals(fileName)) {
// Yes it is so remove the row from JTable.
dtm.removeRow(i);
// Found a match so no need to check
// further. Get out of loop.
break;
}
// No it's not so check the cell in next
// table row (if there is one).
}
}
You can of course modify this method to handle a list of file names instead of one file name at a time. Again, the code above is not tested and is just real quick off the top of my head but I think it gives you an idea of how it can be done.

nattable multiple row selection

I am using NatTable and I want to select some rows in my table. Additional I want to select some other rows after this (CTRL + left mouse-click).
There is no problem with the first selection, but when I try to select some additional rows, my first selection got lost. This happens only if I do my second selection in dragmode. When I select every additional row by single clicking everything works fine.
I used the RowSelectionModel with the DefaultRowSelectionLayerConfiguration:
selectionLayer.setSelectionModel(new RowSelectionModel<Entry>(selectionLayer, bodyDataProvider, new IRowIdAccessor<Entry>() {
#Override
public Serializable getRowId(Entry rowObject) {
return rowObject.getStartLine();
}
}));
selectionLayer.addConfiguration(new DefaultRowSelectionLayerConfiguration());
Maybe I just did a silly mistake and you guys can help me.
It looks like an issue in the NatTable code. I created a ticket for this: https://bugs.eclipse.org/bugs/show_bug.cgi?id=494392

How to merge previous database with new version database sqlite - Android

I have an app on playstore which contains a database.
Now in the new release, database is changed, a new table is entered.
Currently i am using sqlite studio and put the database file in the assets which is later copied as an app database. See this example.
I have to delete the previous database to copy new, or if i copy new(overwrite), in both cases user data is lost.
Maybe i can attach a new table to current database, but what if i have to add a column in table in previous database???
How should i handle this???
Edit: If i have to copy the data from database and re-insert, how can i know the tables and columns which currently exist in my database.
Please guide me...
Get All the data from Database into ArrayList or anything else (I will suggest Arraylist) and Delete old database and then Create New database and Insert all The data into New database.
Its not a Proper way but a trick to save your time.
Cheers
-Aman
For a new Table, you just have to add the create table in the update method
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
if(oldVersion < LastVersionWithoutTheTable && newVersion >= LastVersionWithoutTheTable){
db.execSQL(CREATE_THE_TABLE);
}
}
For a new column, copy all your data in an array, recreate your table and insert your data again.
It's maybe possible to used SQL Alter Table but I never try it on a SQlite database.

Sort a table in Jackcess

I work with an MS-Access table in Java using Jackcess:
Database mdb = Database.open(new File(myPath));
Table myTable = mdb.getTable("TableName");
Is there a way to get the table sorted/ordered by one or more column(s)? Couldn't find anything in the docs.
Thanks for any hint.
If you iterate through the Table rows using a Cursor which is backed by an Index, you will get the rows ordered by the relevant Index.
This is an example (using the 1.x API) which iterates the table based on the order of the primary key:
for(Map<String,Object> row : Cursor.createIndexCursor(table, table.getPrimaryKeyIndex())) {
// do something with row here...
}
I was having this same issue here, but it helped.
For the folks that are using the new version of Jackcess (v: 2.1.2) here is the answer:
for (Row row : CursorBuilder.createCursor(table.getIndex("IndexToBeSorted"))){
//Your awesome code with the row here
}
Thanks!

Categories